量角器从承诺中返回一个值 [英] Protractor returning a value from a promise
问题描述
我正在编写一个测试,通过查看屏幕上包含关卡计数的文本值来查看我的代码是否正在删除关卡.
I'm writing a test to see if my code is removing a level by looking at a text value on the screen which holds the count of levels.
it 'allows deleting level versions', ->
browser.get('/api#/costings')
element(By.id("edit")).click()
startCount = element(By.id("versions_count")).getText().then( (count) ->
return count
)
element(By.id("versions")).click()
first=element.all(By.id("listing")).first()
first.element(By.id("delete")).click()
helper.accept_dialog()
element(By.id("back")).click()
expect(element(By.id("versions_count")).getText()).toEqual(startCount - 1)
这里的问题是 startCount 结果是一个函数.我似乎无法将 startCount 转换为整数,以便查看计数是否减少了 1 项.
Problem here is startCount results in a function. I cannot seem to get startCount into an integer so that I can see if the count has gone down by 1 item.
它给了我错误;
1) edit an existing costing allows deleting level versions
Message:
Expected '1' to equal NaN.
如果我尝试 parseInt(startCount),我会得到同样的错误.
If I try parseInt(startCount) I get the same error.
推荐答案
变量 startCount
是一个 promise,所以 startCount - 1
没有意义:不是从 promise 到它的解析值的自动类型转换,所以你不能从中减去一个.
The variable startCount
is a promise, and so startCount - 1
doesn't make sense: there is no automatic type conversion from a promise to its resolved value, so you can't subtract one from it.
你可以做的是创建一个 promise,它的解析值是预期的版本计数:
What you can do, is create a promise whose resolved value is the expected versions count:
expectedCount = element(By.id("versions_count")).getText().then( (count) ->
return (count - 1).toString();
)
然后你可以将这个promise传递给toEqual
,因为它会在控制流的适当位置自动解开promise
and then you can pass this promise to toEqual
, as it automatically unwraps promises at the appropriate point in the control flow
expect(element(By.id("versions_count")).getText()).toEqual(expectedCount)
这篇关于量角器从承诺中返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!