Node.js 使用NightWatchJS测试脚本进行条件测试

Node.js 使用NightWatchJS测试脚本进行条件测试,node.js,nightwatch.js,Node.js,Nightwatch.js,我正试图用测试脚本中的条件编写一个夜间监视测试脚本。到目前为止我的代码 module.exports = { tags: ['getting-started'], set_url: function (browser) { browser.url('http://www.google.com'); browser.pause(5000); browser.assert.title('Google'); if (browser.expect.element('#main').to.be.prese

我正试图用测试脚本中的条件编写一个夜间监视测试脚本。到目前为止我的代码

module.exports = {
tags: ['getting-started'],
set_url: function (browser) {
browser.url('http://www.google.com');
browser.pause(5000);
browser.assert.title('Google');
if (browser.expect.element('#main').to.be.present) {
    browser.pause(5000);
    browser.setValue('input[type=text]', ['Night Watcher', browser.Keys.ENTER]);
    browser.pause(5000);
    if(browser.assert.containsText('#main', 'The Night Watch')){
    console.log('search has the right result'); // for example
    }else{
    console.log('No result found');
    }
}
browser.end();
}
} 但是
browser.expect.element('#main').to.be.present
browser.assert.containsText('#main',the Night Watch')
返回一个对象,实际上不是我感兴趣的结果。
但是
browser.expect.element('#main').to.be.present
browser.assert.containsText('#main',the Night Watch')
返回一个对象,实际上不是我感兴趣的结果。

我使用的是页面对象……如果你不使用页面对象,就说browser.elements。以下解决方案对我有效

      .waitForElementPresent('#main', 1000)
  .api.elements('css selector','input[type=text]',function(result){
    console.log("123");
    if(result.value) {
      console.log("======================================");
      this.setValue('input[type=text]', ['Night Watcher', this.Keys.ENTER]);
      }
  })
  .waitForElementPresent('#main', 1000, function(res) {
    if(res.value) {
      console.log('search has the right result');
    }else{
        console.log('No result found');
        }
  })
下面是我从代码中得到的输出。。希望这对你有帮助。。不过,代码可以进行更优化

标题这是非常基本的:

module.exports = {
    tags: ['getting-started'],
    set_url: function(browser) {
        browser.url('http://www.google.com')
            .pause(5000)
            .assert.title('Google')
            .waitForElementPresent('#main', 3000, function(result) {
                if (result.value === true) { // '#main is present
                    this
                        .setValue('input[type=text)', 'Night Watcher')
                        .click('#search') //instead of enter, you can click search button
                        .getText("#main", function(result) {
                            console.log(result.value);
                            // You can continue here
                        });
                }
            })
            .end();
    }
};