Run nightwatch in jessie through headless chrome - selenium

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?

Related

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.

NightwatchJS never runs the url

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".

microsoft edge and nightwatch

for some reason, I have not been able to get tests working in Microsoft Edge Windows 10.
Here's my nightwatch config
"edge": {
"use_ssl": false,
"silent": true,
"output": true,
"desiredCapabilities": {
"browserName": "MicrosoftEdge",
"platform": "Windows 10",
"version": "13.10586",
"screenResolution": "1280x1024",
"avoidProxy": true
}
}
Has anyone been able to get their tests working in Microsoft Edge?
If so, what version of selenium do you use? I use 2.52
What version of the edge driver do you use?
First of all you need Microsoft Edge Webdriver. You may download it from here: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Then in nigthwatch.js config you need to specify path to you edge webdriver (webdriver.edge.driver arg). This is how my config looks like:
"selenium": {
"start_process": true,
"server_path": "./node_modules/file_dependencies/selenium-server-standalone.jar",
"log_path": "",
"host": "127.0.0.1",
"port": seleniumPort,
"cli_args": {
"webdriver.chrome.driver": "./node_modules/file_dependencies/chromedriver.exe",
"webdriver.ie.driver": "./node_modules/file_dependencies/IEDriverServer.exe",
"webdriver.edge.driver": "C:/Program Files (x86)/Microsoft Web Driver/MicrosoftWebDriver.exe",
"webdriver.gecko.driver": "./node_modules/file_dependencies/geckodriver.exe",
"webdriver.firefox.profile": ""
}
}
Rest of your config looks fine
I had issues getting edge to work using Selenium 3.9.1 where Selenium was attempting to use the geckodriver to run tests against Edge.
My configuration looked as follows (snippet to keep to the point):
"selenium" : {
"cli_args" : {
"webdriver.chrome.driver" : "bin\\chromedriver.exe",
"webdriver.edge.driver" : "bin\\MicrosoftWebDriver.exe",
"webdriver.gecko.driver" : "bin\\geckodriver.exe",
"webdriver.firefox.profile": ""
}
},
"test_settings" : {
"default" : {
"desiredCapabilities": {
"browserName": "edge",
"marionette": true
}
}
}
I was able to get around this by changing "edge" to "ie" and the browser name to "internet explorer" - see the updated configuration:
"selenium" : {
"cli_args" : {
"webdriver.chrome.driver" : "bin\\chromedriver.exe",
"webdriver.ie.driver" : "bin\\MicrosoftWebDriver.exe",
"webdriver.gecko.driver" : "bin\\geckodriver.exe",
"webdriver.firefox.profile": ""
}
},
"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
}
}
}
The auto-generated nightwatch.conf.js already includes configuration for Edge browser, so you can just run by flagging the edge environment:
nightwatch --env edge

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