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

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.

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?

Nightwatch : Error retrieving a new session from the selenium server

I have a simple test to open the launch_url in chrome. But I am getting error as cannot retrieve any new session.
Also I would like to know how can I use nightwatch without running in grid. Just a standalone instance.
Below is the configuration I have used.
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : false,
"server_path" : "./bin/selenium-server-standalone-3.4.0.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./bin/chromedriver.exe",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://127.0.0.1",
"selenium_port" : 4444,
"selenium_host" : "127.0.0.1",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "firefox",
"marionette": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
}
}
}
When I run nightwatch using the command nightwatch --env chrome or simply nightwatch, it gives me the below error
[Test1] Test Suite
======================
Running: Demo test
http://127.0.0.1
Error retrieving a new session from the selenium server
Connection refused! Is selenium server started?
{ status: 13,
value:
{ message: 'Error forwarding the new session Empty pool of VM for setup Capabilities [{acceptSslCerts=true, marionette=true, name=Test1, browserName=chrome, javascriptEnabled=true, platform=ANY}]',
class: 'org.openqa.grid.common.exception.GridException' } }
My Test looks something like
module.exports = {
'Demo test' : function (browser) {
console.log(browser.launchUrl);
browser
.url(browser.launchUrl)
.end();
}
};
I can see that the launch url is been logged into the console, but the browser is not starting. I am using the latest jar file and the chromedriver binary.
I just now gave this a try and here's what I have figured out
In your selenium configuration section I am seeing that you have set start_process as false.
"selenium" : {
"start_process" : false,
"server_path" : "./bin/selenium-server-standalone-3.4.0.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./bin/chromedriver.exe",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
When you have the value as false, you can essentially get rid of this section itself (because its not going to be used at all per your configuration)
You are essentially telling nightwatch that it shouldn't try and start a selenium-server by itself but it should just connect to the selenium server running on port 4444 (These values are obtained from default section of your test_settings section
"default" : {
"launch_url" : "http://www.google.com",
"selenium_port" : 4444,
"selenium_host" : "127.0.0.1",
"silent": false,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "firefox",
"marionette": true
}
},
So far we are good. So before you ran nightwatch command am guessing you started the selenium server but in the incorrect mode.
I think you started the server using the below command
java -jar selenium-server-standalone-3.4.0.jar -role hub
This causes a Hub to be started. A Hub is like a manager. It cannot do the work (of launching browsers, opening urls, typing texts, clicking links etc) on its own. It needs a node to be available so that it can route all of its work to the node.
The error Error forwarding the new session Empty pool of VM for setup Capabilities [{acceptSslCerts=true, marionette=true, name=Nightwatchtest, browserName=firefox, javascriptEnabled=true, platform=ANY}]' is essentially the Selenium Grid's way of telling you, that you haven't wired in any nodes for it to route the traffic to.
So in order to fix your issue, you can do one of the following:
Start the selenium server in the standalone mode [neither as a Hub or as a node], using the command java -jar selenium-server-standalone-3.4.0.jar (or)
Flip the flag start_process to true in your configuration file, which will cause nightwatch to start and stop the server on its own.

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