javascript - Nightwatch setValue method doesn't work - Stack Overflow

I'm currently trying to set up an Nightwatch project, to see if it's any good. I'm follo

I'm currently trying to set up an Nightwatch project, to see if it's any good. I'm following This tutorial right now. The stuff from the tutorial works, but when I try to modify it a little bit, it doesn't work anymore. I looked over the Developer API guide, but I guess I'm still missing something? Code I use is pasted below:

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

  }
};

Error in 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 _binedTickCallback (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 _binedTickCallback (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 _binedTickCallback (internal/process/next_tick.js:67:7)

And finally, html just to be plete:

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

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

I'm currently trying to set up an Nightwatch project, to see if it's any good. I'm following This tutorial right now. The stuff from the tutorial works, but when I try to modify it a little bit, it doesn't work anymore. I looked over the Developer API guide, but I guess I'm still missing something? Code I use is pasted below:

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

  }
};

Error in 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 _binedTickCallback (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 _binedTickCallback (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 _binedTickCallback (internal/process/next_tick.js:67:7)

And finally, html just to be plete:

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

<input type="password" class="inputText" id="txtPassword"  name="txtPassword" >
Share Improve this question edited Dec 6, 2016 at 9:00 GillesDV asked Dec 6, 2016 at 8:28 GillesDVGillesDV 2931 gold badge3 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

The setValue is tricky for me as well. Sometimes it puts the values but then clears them and currently (10/10/2017) out of nowhere it started opening the Chrome Settings tab every time I use the setValue function.

What I do now, is first define a function like that

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

Then call it from the Nightwatch chain functions like that

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

Try to .waitForElementVisible('input[name=txtLogin]',6000) before setValue. IT fixed all my problems. <body> will appear immidietly and input[name=txtLogin] will need some time to appear.

@EDIT:

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

  }
};

Try like above just copy/paste

I was having problems with this and ended up going back to a test script that had worked to confirm that it wasn't me.

There appears to be a problem with setValue and the Selenium drivers https://github./nightwatchjs/nightwatch/issues/1147

My work around was to use the .execute function and update the form fields with jQuery or vanilla javascript.

.execute(function(){document.getElementById('_EmailAddress').value = '[email protected]';})
.execute(function(){$('#_Password').val('PasswordString');})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745084367a4610315.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信