Leadfoot会话对象返回承诺
问题描述:
我试图使用leadfoot模块进行实习生和硒的功能测试。Leadfoot会话对象返回承诺
对于这个测试,我试图点击一个地方的按钮,然后检查页面上其他地方的元素的显示属性。
我找不到扩大findById调用搜索的方法,所以我尝试使用会话属性,这似乎工作,但结果在一切都返回一个承诺。
我发现这将使它的工作的唯一方法是链接然后功能。 什么使得会话(和其函数返回的元素)不同?
return this.remote
.findById('buttonContainer')
.findByClassName('buttonClass')
.click()
.session
.findById('stagePanel')
.then(function(element) {
element.findByClassName('itemList')
.then(function(element) {
element.getComputedStyle('display')
.then(function (display) {
// check display property
});
});
});
我确定我做了很多错误的事情,所以任何和所有的建议表示赞赏。
答
this.remote
对象是Command对象,而不是Session或Element对象。如果你想要一个会话,你可以从this.remote.session
得到它,但它通常不是必需的,并且会话对象不可链接。
您的第二个findById
不工作的原因是因为您不是end
ing过滤您添加了以前的findBy
调用。如果在查找操作后没有调用end
,则后续查找操作将使用前一个查找的元素作为搜索的根元素。
换句话说,当您运行this.remote.findById('a').findById('b')
时,它会在元素'a'内搜索元素'b',而不是在整个文档this.remote.findById('a').end().findById('b')
内搜索整个文档中的'a'和'b'。
此外,任何时候在回调中执行异步操作时,都需要return
操作的结果。如果不这样做,测试不会知道它需要等待更多操作才能完成。返回链接还可以防止callback pyramids:
return this.remote
.findById('buttonContainer')
.findByClassName('buttonClass')
.click()
.end(2)
.findById('stagePanel')
.then(function(stagePanel) {
return stagePanel.findByClassName('itemList');
}).then(function(itemList) {
return itemList.getComputedStyle('display');
}).then(function (display) {
// check display property
});