Reputation: 978
I'm trying to get nightwatch to call the browser.url('http://www.google.com')
function call but it comes up as undefined (the following code was taken from the official docs at http://nightwatchjs.org/guide)
module.exports = {
'Demo test Google' : function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end();
}
};
I have a project set up with something more simple, but my issue is any of the browser
arguments that are referenced in nightwatch.js's docs come up as undefined
If I do
console.log(JSON.stringify(browser))
I get:
{"capabilities":{},"globals":{},"sessionId":null,"options":{"screenshots":false,"skip_testcases_on_fail":false,"log_screenshot_data":true,"desiredCapabilities":{"browserName":"internet explorer","javascriptEnabled":true,"acceptSslCerts":true,"platform":"ANY","marionette":true,"name":"MARSBPCHART"}},"launchUrl":"http://localhost","launch_url":"http://localhost","Keys":{"NULL":"","CANCEL":"","HELP":"","BACK_SPACE":"","TAB":"","CLEAR":"","RETURN":"","ENTER":"","SHIFT":"","CONTROL":"","ALT":"","PAUSE":"","ESCAPE":"","SPACE":"","PAGEUP":"","PAGEDOWN":"","END":"","HOME":"","LEFT_ARROW":"","UP_ARROW":"","RIGHT_ARROW":"","DOWN_ARROW":"","ARROW_LEFT":"","ARROW_UP":"","ARROW_RIGHT":"","ARROW_DOWN":"","INSERT":"","DELETE":"","SEMICOLON":"","EQUALS":"","NUMPAD0":"","NUMPAD1":"","NUMPAD2":"","NUMPAD3":"","NUMPAD4":"","NUMPAD5":"","NUMPAD6":"","NUMPAD7":"","NUMPAD8":"","NUMPAD9":"","MULTIPLY":"","ADD":"","SEPARATOR":"","SUBTRACT":"","DECIMAL":"","DIVIDE":"","F1":"","F2":"","F3":"","F4":"","F5":"","F6":"","F7":"","F8":"","F9":"","F10":"","F11":"","F12":"","COMMAND":"","META":""},"assert":{},"currentTest":{"name":"Demo test Google","module":"MARSBPCHART","results":{"steps":[],"passed":0,"failed":0,"errors":0,"skipped":0,"tests":0,"testcases":{},"timestamp":"Mon, 12 Mar 2018 18:00:40 GMT","time":0},"group":""}}
I was trying to get pageObjects set up working by making a pages/
folder in my root directory and inside there I was trying to defined the url
method/function but Im not quite sure if Im the one who should be creating this method/function to call or if the nightwatch.js's underlying library creates this for me
browser.navigate
is also undefined for me.
again, is this something I need to implement myself or is it already included with nightwatch?
below is my nightwatch.json
file:
{
"src_folders" : ["./tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "./pages",
"globals_path" : "",
"selenium" : {
"start_process" : false,
"start_session": false,
"server_path" : "./bin/selenium-server-standalone-3.9.1.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : "./bin/MicrosoftWebDriver.exe"
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "internet explorer",
"marionette": true,
"javascriptEnabled": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
}
}
}
and here is my tests/MARSBPCHART.js file:
var searchCommands = {
submit: function () {
this.waitForElementVisible('@submitButton', 3000)
.click('@submitButton')
.api.pause(1000);
return this; // Return page object for chaining
}
};
module.exports = {
testMe: function() {
console.log('in testMe')
},
url: 'http://google.com',
commands: [searchCommands],
elements: {
searchBar: { selector: 'input[name=q]' },
submitButton: { selector: '[type=submit]' }
},
page: function() {
console.log('in page of MARSBPCHARTPage');
},
MARSBPCHART: function() {
console.log('in MARSBPCHARTPage.js\' this.MARSBPCHARTPage')
}
};
here is the whole project:
https://github.com/cotyembry/UITests
Edit 1: I failed to mention that I was running java -jar ./bin/selenium-server-standalone-3.9.1.jar
manually in another terminal window
Thanks for all of your time and help, I really appreciate it
Upvotes: 1
Views: 2425
Reputation: 2858
It is because you are not starting the selenium process and session. All I did was copy your code and config and switched the values for start_process
and start_session
to true and it started working. There are other problems with your test but I did get it to run. To get browser.url()
or .navigate()
to work you need to start the selenium process and session. You can do that manually or by setting these values in the config to true
.
Upvotes: 3