1

我正在尝试使用 Leadfoot 模块对实习生和硒进行功能测试。

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

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

我发现使它工作的唯一方法是链接 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
            });
        });

    });

我确信我做错了很多事情,所以任何和所有的建议都值得赞赏。

4

1 回答 1

4

this.remote对象是Command对象,而不是SessionElement对象。如果你想要一个 Session ,你可以从中得到它,this.remote.session但它通常不是必需的,而且 Session 对象是不可链接的。

你的第二个findById不工作的原因是因为你没有end过滤你在以前的findBy电话中添加的内容。当您在查找操作后不调用end时,任何后续查找操作都将使用前一个查找中的元素作为要搜索的根元素。

换句话说,当您运行时this.remote.findById('a').findById('b'),它将在元素“a”内搜索元素“b”,而不是在整个文档内this.remote.findById('a').end().findById('b')搜索“a”和“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
    });
于 2015-02-24T16:35:52.953 回答