Nightwatch setValue方法不起作用

时间:2016-12-06 08:28:45

标签: javascript testing nightwatch.js e2e-testing

我正在尝试设置一个Nightwatch项目,看看它是否有用。我现在正在关注This tutorial。本教程中的内容有效,但是当我尝试稍微修改它时,它就不再起作用了。我查看了Developer API guide,但我想我还在遗漏一些东西?我使用的代码粘贴在下面:

var conf = require('../../nightwatch.conf.js');

module.exports = {
  'Demo test' : function (browser) {
    browser
      .url('http://localhost/myWebsite?newwindow=0')
      .waitForElementVisible('body', 6000)
      .setValue('input[name=txtLogin]', 'login')
      .setValue('input[name=txtPassword]', 'password')
      .waitForElementVisible('input.btnLogin', 2000)
      .click('button[id=btnLogin]')
      .pause(6000)
      .assert.elementPresent("#selectTitle")
      .assert.containsText('#selectTitle', 'schedules')
      .assert.urlContains('login/login_start.asp')
      .saveScreenshot(conf.imgpath(browser) + 'titleScreen.png')
      .end();

  }
};

cmd中的错误:

Running:  Demo test 
 √ Element <body> was visible after 41 milliseconds.
ERROR: Unable to locate element: "input[name=txtLogin]" using: css selector
at Object.Demo test (C:\Workspace\myWebsite\learn-nightwatch\test\e2e\My_Test.js:8:8)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
ERROR: Unable to locate element: "input[name=txtPassword]" using: css selector
at Object.Demo test (C:\Workspace\myWebsite\learn-nightwatch\test\e2e\My_Test.js:9:8)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
 × Timed out while waiting for element <input.btnLogin> to be present for 2000 milliseconds.  - expected "visible" but got: "not found"
at Object.Demo test (C:\Workspace\myWebsite\learn-nightwatch\test\e2e\My_Test.js:10:8)
at _combinedTickCallback (internal/process/next_tick.js:67:7)

最后,html只是为了完成:

<input type="text" class="inputText" id="txtLogin" name="txtLogin" >

<input type="password" class="inputText" id="txtPassword"  name="txtPassword" >

3 个答案:

答案 0 :(得分:2)

setValue对我来说也很棘手。有时它会放置值但随后清除它们并且当前(10/10/2017)无处不在,每次我使用setValue函数时都会打开Chrome设置标签。

我现在所做的,首先是定义一个类似

的函数
var setValue =  function(sel, value) {
    $(sel).val(value).change();
};

然后从Nightwatch链函数中调用它

browser.url('https://google.com')
  .execute(setValue, ['#search', 'keyword'])
  .click('#search-btn')
  .waitForElementVisible('an example of selector')
  .assert.containsText('selector', '100000xxxx results')

答案 1 :(得分:1)

在setValue之前尝试.waitForElementVisible('input[name=txtLogin]',6000)。 IT解决了我所有的问题。 <body>会出现在input[name=txtLogin]var conf = require('../../nightwatch.conf.js'); module.exports = { 'Demo test' : function (browser) { browser .url('http://localhost/myWebsite?newwindow=0') .waitForElementVisible('body', 6000) .waitForElementVisible('input[name="txtLogin"]', 6000) .setValue('input[name="txtLogin"]', 'login') .setValue('input[name="txtPassword"]', 'password') .waitForElementVisible('input.btnLogin', 2000) .click('button[id="btnLogin"]') .assert.elementPresent("#selectTitle") .assert.containsText('#selectTitle', 'schedules') .assert.urlContains('login/login_start.asp') .saveScreenshot(conf.imgpath(browser) + 'titleScreen.png') .end(); } }; 需要一些时间才会出现。

@EDIT:

            1)Because ignite services are running with different configuration files in same server(it may be one reason)

尝试上面只需复制/粘贴

答案 2 :(得分:0)

我遇到了这个问题并最终回到了一个测试脚本,该脚本已经确认它不是我。

setValue和Selenium驱动程序https://github.com/nightwatchjs/nightwatch/issues/1147

似乎存在问题

我的工作是使用.execute函数并使用jQuery或vanilla javascript更新表单字段。

.execute(function(){document.getElementById('_EmailAddress').value = 'email@address.co.uk';})
.execute(function(){$('#_Password').val('PasswordString');})