Leadfoot会话对象返回promise

Leadfoot session object returns promises

本文关键字:promise 返回 对象 会话 Leadfoot      更新时间:2024-04-18

我正试图使用引线脚模块对intern和selenium进行功能测试。

对于这个测试,我试图单击一个地方的按钮,然后在页面的其他地方检查元素的显示属性。

我找不到扩展findById调用搜索的方法,所以我尝试使用session属性,它似乎有效,但结果一切都返回了promise。

我发现的唯一能让它工作的方法是链接then函数。是什么使会话(及其功能返回的元素)不同?

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对象。如果您想要一个Session,您可以从this.remote.session获取它,但这通常不是必需的,而且Session对象是不可链接的。

您的第二个findById不起作用的原因是,您没有对之前添加的findBy调用进行end筛选。如果在查找操作后未调用end,则任何后续的查找操作都将使用上一次查找中的元素作为要在中搜索的根元素。

换句话说,当您运行this.remote.findById('a').findById('b')时,它将在元素"a"内部搜索元素"b",而不是在整个文档内部。this.remote.findById('a').end().findById('b')将在整个文档中搜索"a"answers"b"。

此外,任何时候从回调中执行异步操作时,都需要return操作的结果。如果你不这样做,测试将不知道它需要等待更多的操作才能完成。返回链接还防止回调金字塔:

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