使用NightWatchJStesting脚本进行条件testing

我试图在testing脚本中用条件编写一个守夜testing脚本。 我的代码到目前为止

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.presentbrowser.assert.containsText('#main', 'The Night Watch')返回一个对象,实际上并不是我感兴趣的结果用。 但是browser.expect.element('#main').to.be.presentbrowser.assert.containsText('#main', 'The Night Watch')返回一个对象,实际上并不是我感兴趣的结果。

我正在使用页面对象…如果你不使用页面对象,那么只需说browser.elements。 以下解决scheme为我工作

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

以下是我从代码中得到的输出。希望这可以帮助你..虽然代码可以更优化。

显示OutPut

标题

这是非常基本的:

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