如何使EXPECT条件等待承诺解决(代码包装在一个方法中)?
当我尝试在testing体内使用下面的代码片段时,它似乎工作正常,除了这个块之后的代码仍在执行之后,并且testing在最后失败。
var el = element(by.model('name.first')); el.isDisplayed().then(function(condition) { console.log("Is Element Displayed : " + condition); if(!condition) { fail('Test Failed'); } });
但是,当我把这个代码包装在一个方法(在其他的一些util文件中),如:
isMyElementDisplayed: function (element) { var el = element; el.isDisplayed().then(function(condition) { console.log("Is Element Displayed : " + condition); return condition; }); }
然后尝试在我的testing中调用此方法,如:
expect(elementActions.isMyElementDisplayed(element(by.model('name.first')))).toBe(true);
它会失败,以下例外。
Expected undefined to be true.
但是我看到消息打印在日志中: Is Element Displayed : false
在这种情况下,testing并不是停留在“期望”状态,而是最终失败。 你可以告诉我新量angular器和JavaScript。 我试图自动化QAfunctiontesting,并构build一些可重复使用的方法,可以再次使用,也可以由其他人使用。
-
有没有一种正确的方式来包装在一个像我这样的util方法的代码? 和
-
如何使
expect
条件等待承诺解决?
FYI:npm -version – > 2.14.7和量angular器–version – > 3.0.0
@Artyom指出什么是将return
到isMyElementDisplayed
函数:
isMyElementDisplayed: function (element) { var el = element; // v HERE return el.isDisplayed().then(function(condition) { console.log("Is Element Displayed : " + condition); return condition; }); }
这样, isMyElementDisplayed
函数将返回一个承诺,将被放在控制stream,并expect
在断言之前隐式地解决它。
虽然,我会用等待来解决这个问题:
var EC = protractor.ExpectedConditions; var elm = element(by.model('name.first')); browser.wait(EC.visibilityOf(elm), 5000, "Element is still not visible"); expect(elm.isDisplayed()).toBe(true);