Nightwatch setValue method doesn't work

1,926 views
Skip to first unread message

Gilles De Vylder

unread,
Dec 6, 2016, 4:11:52 AM12/6/16
to NightwatchJs
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" >

Deshun Coomer

unread,
Jan 13, 2017, 4:03:46 PM1/13/17
to NightwatchJs
Try using quotes for the selector, just like in the HTML. so..


.setValue('input[name="txtLogin"]', 'login')

.setValue('input[name="txtPassword"]', 'password')

You also might want to try placing a waitForElementVisible (or Present instead of Visible) for your login or password input

Eric Mumford

unread,
Jan 31, 2017, 10:19:15 AM1/31/17
to NightwatchJs
You are waiting for the 'body' to be visible, but that's not a good test. Unfortunately, it's used as an example in many automated code snippets on the internet.

A best practice pattern is to always waitForElementVisible on the *precise element* you are about to manipulate. In your case, wait for your input box to appear visible, then sendKeys to it.

Get rid of your .pause(6000). Your code should not need to pause unless you want to look at the screen to see something.

Get rid of your asserting element present after the click and replace it with a waitForElementVisible.




On Tuesday, December 6, 2016 at 4:11:52 AM UTC-5, Gilles De Vylder wrote:

N Katsikanis

unread,
Dec 1, 2017, 6:15:34 AM12/1/17
to NightwatchJs
Sometimes waitForElementVisible is not ideal, if it is hidden by the size of the screen, I use waitForElementPresent instead. One issue however is this::
Reply all
Reply to author
Forward
0 new messages