我将 NightwatchJS 与 NodeJS 结合使用: http://nightwatchjs.org/api

我有一个模式对话框,它可能会出现,也可能不会出现。它有一个 #close_button 需要单击(如果模式确实出现)才能继续。

我设置了 abortOnFailure 的参数 waitForElementPresentfalse 因此,如果模式未出现,脚本将继续。但是我无法让它工作。

有什么建议么?

module.exports = {
    "Test" : function (browser) {
        browser
            .url("http://domain.com/")
            .waitForElementPresent('#close_button', 5000, false, function() {
                this.click('#close_button')
            })
            .setValue('#username', 'test@email.com')
            //more code here
            .end(); //does end() go here or inside .waitForElementPresent() above?
    }
}
有帮助吗?

解决方案

abortOnFailure 不过效果很好 waitForElementPresent 现在有一个错误,您传递的回调没有在正确的上下文中调用。这将会得到解决。

同时,您可以像这样编写测试,并将 click 外面是一样的,而且看起来更干净:

module.exports = {
  "Test" : function (browser) {
    browser
      .url("http://domain.com/")
      .waitForElementPresent('#close_button', 5000, false)
      .click('#close_button')
      .setValue('#username', 'test@email.com')
      //more code here
      .end(); // end() goes here
  }
}

其他提示

我遇到了类似的事情,我正在等待 iframe 出现。我创建了一个函数来实际关闭它:

页面对象功能:

Home.prototype.closeIframe = function(browser) {
var self = this;
console.log('Checking for iframe');
this.browser
        .isVisible(iframeSelectors.iframe, function(result) {
            if (result.value === true) {
                self.browser
                        .log('iframe visible')
                        .frame(iframeSelectors.name)
                        .waitForElementVisible(iframeSelectors.closeLink)
                        .click(iframeSelectors.closeLink)
                        .assert.elementNotPresent(iframeSelectors.iframe)
                        .frame(null)
                        .pause(2000); //allow for proper frame switching
            } else {
                console.log('iframe is not visible');
            }
        });

return this;

在我的测试中,我等待页面完全加载,然后再执行上述函数。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top