NightwatchJS never runs the url - selenium

I have been trying to configure nightwatch to perform some unittesting tasks.
Selenium server and chrome are running but I never get the url actually loading in the url bar in chrome, thus nothing appears, I just get a blank white page with "data;" in the url in chrome (using chromedirver)
Here's my configuration if someone can help
{
"src_folders" : ["test"],
"output_folder" : "reports",
"custom_assertions_path" : "",
"globals_path" : "",
"live_output" : true,
"parallel_process_delay" : 10,
"disable_colors": false,
"test_workers" : false,
"selenium" : {
"start_process" : true,
"server_path" : "bin/selenium.jar",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "bin/chromedriver",
"webdriver.firefox.profile" : "bin/geckodriver"
}
},
"test_settings" : {
"default" : {
"launch_url" : "test_link.com",
"selenium_host" : "127.0.0.1",
"selenium_port" : 4444,
"silent" : true,
"disable_colors": false,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities" : {
"browserName" : "chrome",
"javascriptEnabled" : true,
"acceptSslCerts" : true
}
},
"saucelabs" : {
"selenium_host" : "ondemand.saucelabs.com",
"selenium_port" : 80,
"username" : "${SAUCE_USERNAME}",
"access_key" : "${SAUCE_ACCESS_KEY}",
"use_ssl" : false,
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"globals" : {
"myGlobal" : "some_sauce_global"
},
"selenium" : {
"start_process" : false
}
},
"testingbot" : {
"selenium_host" : "hub.testingbot.com",
"selenium_port" : 80,
"apiKey" : "${TB_KEY}",
"apiSecret" : "${TB_SECRET}",
"silent" : true,
"output" : true,
"screenshots" : {
"enabled" : false,
"on_failure" : true,
"path" : ""
},
"desiredCapabilities": {
"name" : "test-example",
"browserName": "firefox"
},
"selenium" : {
"start_process" : false
}
}
}
}
and here's my test
module.exports = {
'Tracking the website': function (client)
client
.url(client.launch_url)
.getValue('#id1', function(result){
console.log("================================");
console.log("value " + result.value);
console.log("================================");
})
.end();
}
};

That's because your launch_url doesn't contain the protocol to use.
Try using "launch_url" : "http://www.google.com" instead of "launch_url" : "www.google.com".

Related

Run nightwatch in jessie through headless chrome

I have started a jessie clean vps and then I installed node.js. After that I installed nightwatch and chromedriver through npm. I made 3 files, they are nightwatch.js, nightwatch.json, and js sample testing.
Here is my nightwatch.json:
{
"src_folders" : [""],
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "./selenium-server-standalone-3.0.1.jar",
"log_path" : "",
"host": "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" :
"./node_modules/chromedriver/lib/chromedriver/chromedriver"
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions" : {
"args" : ["headless", "--no-sandbox"]
}
}
}
}
}
And for sample testing, it's just a simple testing like this:
var chromedriver = require('chromedriver');
module.exports = {
before : function(done) {
chromedriver.start();
},
after : function(done) {
chromedriver.stop();
},
'Trial' : 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();
}
};
Here is what I got:
And everytime I run the test, it seems like the headless browser doesn't work properly. Nightwatch cannot recognize every element. When I do .assert.title('Google'), it returns 'Object not found'. I have tested it on windows and it worked smoothly. Is there something that I miss? Is it necessary to use chrome binary when I run it headlessly?

Issues running nightwatch + selenium on local docker pipelines

I am having issues when running a simple nightwatch test file on my local Docker image. I am trying to figure out why selenium does not want to run the local tests. If anyone has figured this out, any help would be much appreciated. Thanks!
Here is my test file:
nw-example.test.js
module.exports = {
'End-to-end all browsers' : function (browser) {
browser
.init('http://localhost:3000/')
.setValue('#loginForm-username', '')
.setValue('#loginForm-password', '')
.pause(2000)
.click('#loginForm-submit')
.perform(function(done){
console.log('Done testing')
done()
})
.pause(3000)
.assert.containsText('#app','Welcome,');
}
};
Here is my nightwatch.json file:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./usr/local/bin/chromedriver",
"webdriver.gecko.driver" : "./usr/local/bin/geckodriver",
"webdriver.edge.driver" : "",
"webdriver.safari.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost:3000",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName" : "chrome",
"javascriptEnabled" : true,
"marionette" : true,
"acceptSslCerts" : true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions" : {
"args" : ["headless", "--no-sandbox"]
}
}
},
"firefox" : {
"desiredCapabilities": {
"browserName": "firefox"
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
},
"safari" : {
"desiredCapabilities": {
"browserName": "safari",
"javascriptEnabled": true
}
}
}
}
And the errors I am getting:
root#2b755e5a6174:/vital-webapp/src/__tests__# nightwatch nw-example.test.js
[Nw Example Test] Test Suite
================================
Running: End-to-end all browsers
20:13:49.642 INFO - Found handler: org.openqa.selenium.remote.server.commandhandler.BeginSession#2465e6e1
20:13:49.643 INFO - /session: Executing POST on /session (handler: BeginSession)
20:13:49.646 INFO - Capabilities are: Capabilities {acceptSslCerts: true, browserName: chrome, javascriptEnabled: true, marionette: true, name: Nw Example Test}
20:13:49.646 INFO - Capabilities {acceptSslCerts: true, browserName: chrome, javascriptEnabled: true, marionette: true, name: Nw Example Test} matched class org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 4516
Only local connections are allowed.
20:13:59.737 WARN - timeout
java.net.SocketTimeoutException: timeout
The error says it all :
20:13:49.646 INFO - Capabilities are: Capabilities {acceptSslCerts: true, browserName: chrome, javascriptEnabled: true, marionette: true, name: Nw Example Test}
20:13:49.646 INFO - Capabilities {acceptSslCerts: true, browserName: chrome, javascriptEnabled: true, marionette: true, name: Nw Example Test} matched class org.openqa.selenium.remote.server.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 4516
Only local connections are allowed.
20:13:59.737 WARN - timeout
java.net.SocketTimeoutException: timeout
While you configured ChromeDriver you have suplied the following desiredCapabilities :
"desiredCapabilities": {
"browserName" : "chrome",
"javascriptEnabled" : true,
"marionette" : true,
"acceptSslCerts" : true
}
ChromeDriver have no such capability as "marionette" which is set to true.
Remove the capability "marionette" : true and execute your #Test

How do I get Nightwatch to show Chai assertions in the reporter?

I'm using Nightwatch JS v0.9.16 to run selenium / chai tests on my localhost. All the assertions work for nightwatch, but I can't get the chai assertion to show in the reporter.
This issue has been discussed here: https://github.com/nightwatchjs/nightwatch/issues/601 however it's been closed but there was no resolution... can anyone get this to work yet?
Here's my test:
var assert = require('chai').assert;
module.exports = {
'pressing the "Servers" tab should change the URL to #servers' : function (client) {
const pages = client.page,
home_page = pages.home(),
servers_page = pages.servers();
home_page.navigate();
servers_page.expect.element('body').to.be.present.before(1000);
client.pause(1000);
servers_page.click('#nav');
servers_page.expect.element('#servers_div').to.be.present.before(1000);
client.url(function(response){
var currentUrl = response.value.replace(client.launch_url,"");
console.log("url is ",response.value);
//***CHAI ASSERTION doesn't get shown on reporter:***
assert(currentUrl.indexOf('#servers')!=-1);
client.end();
});
}
};
Screenshot of when this test passes shows all the assertions except the chai one:
And when it fails it shows AssertionError: Unspecified AssertionError:
Here's my test settings (nightwatch.json)
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "./config/pages/",
"globals_path" : "./config/globals.js",
"selenium" : {
"start_process" : false,
"server_path" : "./libs/selenium-server-standalone-2-53-1.jar",
"log_path" : "./logs/",
"port" : 4444
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost:8081",
"selenium_port" : 9515,
"selenium_host" : "localhost",
"default_path_prefix" : "",
"screenshots" : {
"enabled" : true,
"path" : "./screens/",
"on_failure": true
},
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions" : {
"args" : ["--no-sandbox"]
},
"acceptSslCerts": true
}
}
}
}
Versions:
"selenium-webdriver": "^3.0.1",
"nightwatch": "^0.9.8",
"chromedriver": "^2.25.2",
"chai": "latest"
According to the doc, assert() takes a message as 2nd arg (http://www.chaijs.com/api/assert/), did you try it out? If so, was the message displayed?
Alternatively, have you tried with simpler tests (one failing, one not) for example: assert.isOk(false) & assert.isOk(true)?
Last, did you try running your chai assertion outside of client.url() 's callback?
I am not familiar with Nightwatch, but that's what I would have tried.

Cannot launch Safari browser with Nightwatch

I am using Nightwatch.js to build/run end to end tests on our web app, and as part of our build process, we want to integrate it with Jenkins. On my local machine, I am able to run my tests, on all three browsers (Firefox, Safari, Chrome) at the same time with no issue. I can also run on an individual browser with no issue. I have the correct and most recent versions of the selenium driver, and am pointing to them in my nightwatch.json file. For some reason, however, I am not able to launch Safari on the dedicated machine that is running these tests when a new Jenkins build comes in. Everything is set up the exact same way on that machine as it is on my on, but I keep getting the error
Error retrieving a new session from the selenium server
Failed to connect to SafariDriver after 10066ms
When this happens, both Chrome and Firefox are able to load with no problem, only Safari has the issue. We made sure to download the most recent version of the standalone selenium driver for Safari, and I did it again just to be sure. I've also made sure to check if there happens to be another instance of the selenium server running, but it's never the case
My nightwatch.json file looks like this....
{
"src_folders" : ["test"],
"output_folder" : "reports",
"custom_commands_path" : "node_modules/nightwatch-custom-commands- assertions/js/commands",
"custom_assertions_path" : "node_modules/nightwatch-custom-commands-assertions/js/assertions",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "lib/selenium-server-standalone-2.53.0.jar",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "lib/chromedriver",
"webdriver.safari.driver" : "lib/selenium-server-standalone-2.53.0.jar"
}
},
"test_settings" : {
"firefox" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : "./screenshots"
},
"desiredCapabilities": {
"browserName": "firefox",
"javascriptEnabled": true,
"acceptSslCerts": true
},
"end_session_on_fail": false,
"skip_testcase_on_fail": false
},
"chrome" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : "./screenshots"
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
},
"end_session_on_fail": false,
"skip_testcase_on_fail": false
},
"safari" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : "./screenshots"
},
"desiredCapabilities" : {
"browserName" : "safari",
"javascriptEnabled" : true,
"acceptSslCerts" : true
},
"end_session_on_fail": false,
"skip_testcase_on_fail": false
}
}
}
If anyone has any insight to why this might be happening I'd appreciate it. Thanks
You would have to manually install SafariDriver. SafariDriver can be found here.
To check if safariDriver is enabled or not go to Safari>Preferences>Extensions>Here enable webDriver extension. It should be enabled by default but just in case if you need to debug. Thanks
UPDATE*****
Turns out there is an extension for Safari that needs to be installed for selenium driver to work.

Run web server and the selenium server with Gulp + nightwatch.js

I'm trying to run some nightwatch.js e2e tests using Gulp.
At the moment I have to do the following:
Run the selenium server manually
Set the path of selenium-server-standalone-2.47.1.jar and phantomjs.exe
Run the web server manually
My gulp task looks as follows:
gulp.task("run-e2e-tests", function () {
return gulp.src('')
.pipe(nightwatch({
configFile: "nightwatch.json",
cliArgs: {
env: "phantomjs"
}
}));
});
My nightwatch.js configuration looks as follows:
{
"src_folders" : [
"bundle/e2e_test/"
],
"output_folder": false,
"selenium" : {
"start_process" : false,
"server_path" : "./selenium-binaries/selenium-server-standalone-2.47.1.jar"
},
"test_settings" : {
"default" : {
"silent": true,
"screenshots" : {
"enabled" : false
},
"desiredCapabilities": {
"browserName": "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : false
}
},
"phantomjs" : {
"desiredCapabilities": {
"browserName": "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : false,
"phantomjs.binary.path" : "phantomjs.exe"
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": false
}
}
}
}
If I change "start_process" to true I get the following error:
[17:10:04] Using gulpfile ~\Desktop\CPIC.UI.Web\gulpfile.js
[17:10:04] Starting 'run-e2e-tests'...
[17:10:04] log file
[17:10:04] Starting nightwatch...
[CPIC E2e Test] Test Suite
==========================
Running: CPIC integration
Error retrieving a new session from the selenium server
Error: connect ECONNREFUSED 127.0.0.1:4444
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
Connection refused! Is selenium server started?
[17:10:06] 'run-e2e-tests' errored after 1.5 s
[17:10:06] Error in plugin 'gulp-nightwatch'
Message:
nightwatch exited with code 1
The e2e test run as expected but I would like to run both the web server and the selenium server from Gulp automatically as part of my CI process.
I got it to work! Basically if you install selenium and phantomjs via npm something is wrong so you have to manually download them and set the paths int the nightwatch.js configuration file:
{
"src_folders" : [
"bundle/e2e_test/"
],
"output_folder": false,
"selenium" : {
"start_process" : true,
"server_path" : "./selenium-binaries/selenium-server-standalone-2.47.1.jar"
},
"test_settings" : {
"default" : {
"silent": true,
"screenshots" : {
"enabled" : false
},
"desiredCapabilities": {
"browserName": "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : false
}
},
"phantomjs" : {
"desiredCapabilities": {
"browserName": "phantomjs",
"javascriptEnabled" : true,
"acceptSslCerts" : false,
"phantomjs.binary.path" : "./selenium-binaries/phantomjs.exe"
}
}
}
}
To run the web server and kill it I used 3 tasks ans run-sequence:
gulp.task("run-e2e-tests", function (cb) {
runSequence(
"run-http-server",
"run-nightwatch",
"kill-http-server",
cb);
});
gulp.task("run-nightwatch", function () {
return gulp.src('')
.pipe(nightwatch({
configFile: "nightwatch.json",
cliArgs: {
env: "phantomjs"
}
}));
});
gulp.task("run-http-server", function () {
return connect.server({
port: 8888
});
});
gulp.task("kill-http-server", function () {
return connect.serverClose();
});