GillesDV
GillesDV

Reputation: 293

Nightwatch setValue method doesn't work

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 _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)

And finally, html just to be complete:

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

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

Upvotes: 0

Views: 9960

Answers (3)

NikoKyriakid
NikoKyriakid

Reputation: 630

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.com')
  .execute(setValue, ['#search', 'keyword'])
  .click('#search-btn')
  .waitForElementVisible('an example of selector')
  .assert.containsText('selector', '100000xxxx results')

Upvotes: 2

TimSmith-Aardwolf
TimSmith-Aardwolf

Reputation: 544

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

Upvotes: 0

Hikaryu
Hikaryu

Reputation: 317

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

Upvotes: 1

Related Questions