Javascript 如何构造量角器承诺
在本例中,我正在测试元素是否位于页面底部。我在使用量角器/Webdriver中的承诺,但我认为我使用它是错误的,因为我不认为它应该看起来这么凌乱Javascript 如何构造量角器承诺,javascript,selenium-webdriver,promise,protractor,Javascript,Selenium Webdriver,Promise,Protractor,在本例中,我正在测试元素是否位于页面底部。我在使用量角器/Webdriver中的承诺,但我认为我使用它是错误的,因为我不认为它应该看起来这么凌乱 describe('Element', function(){ it('should be placed at bottom', function(){ element(by.id('page').getSize().then(function(dimP){ element(by.id('element'
describe('Element', function(){
it('should be placed at bottom', function(){
element(by.id('page').getSize().then(function(dimP){
element(by.id('element').getLocation().then(function(locE){
element(by.id('element').getSize().then(function(dimE){
expect(locE.y + dimE.height).toEqual(dimP.height);
})
});
});
})
});
我怎样才能以更干净的方式做这件事?我使用的是量角器2.1.0版
我试过这样做:
expect(element(by.id('page').getSize().height).toEqual(1000);
但它说,未定义的预期值等于1000。因此,我似乎无法使用此处解释的返回值:
(我在实际测试中使用了一个页面对象,因此变量没有示例中那么难看。)因为getSize()
和getLocation()
都返回承诺,并且需要实际值,所以必须解析所有承诺,要么使用then()
显式解析,要么让expect()解析)
隐式地为您执行此操作
我实际上理解你的问题是“如何在量角器中展平承诺链?”。这里有promise.all()
将有助于一次解决多个承诺,从而避免编写多个嵌套的then()
回调:
var elm = element(by.id('element')),
page = element(by.id('page'));
var promises = [elm.getSize(), elm.getLocation(), page.getSize()];
protractor.promise.all(promises).then(function (values) {
expect(values[0].height + values[1].y).toEqual(values[2].height);
});
另见:
元素(by.id('page')).getSize().height
)中获取属性值。基本上,从我的理解来看,你的问题是关于拉平承诺链。我会用更多的信息更新答案。敬请期待。谢谢