量角器从一个承诺返回值 [英] Protractor returning a value from a promise

查看:136
本文介绍了量角器从一个承诺返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个测试,看看我的code是通过看屏幕上的文本价值持有水平的计数去除的水平。

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 是一个承诺,所以 startCount - 1 无厘头:有从承诺到它的解析值没有自动类型转换,所以你不能减去一个从它

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.

你能做什么,是创建一个承诺,其解析值是预期的版本数:

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();
)

然后就可以通过这个承诺 toEqual ,因为它会自动在控制流的相应位置解开承诺

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆