Why are Nightwatch.js tests failing on CircleCI? - nightwatch.js

I have a simple smoketest set up using Nightwatch.js. They pass locally, but they're failing on CircleCI.
Source Code:
const WAIT = 1000;
const NODE_PORT = process.env.NODE_PORT || 3000;
module.exports = {
'Smoketest' (browser) {
browser
.url(`http://localhost:${NODE_PORT}/`)
.waitForElementVisible('body', WAIT)
.pause(300)
.assert.containsText('body', 'Client render')
.end();
}
};
Local output:
> cross-env NODE_PORT=3000 NODE_PATH=source babel-node source/test/e2e.js
Running with config:
{ logDir: '/Users/eric/Dropbox/dev/react-easy-universal/reports',
reportDir: '/Users/eric/Dropbox/dev/react-easy-universal/reports/test-e2e',
nightwatchConfig: '/Users/eric/Dropbox/dev/react-easy-universal/nightwatch.json',
webpackConfig: '/Users/eric/Dropbox/dev/react-easy-universal/webpack.config.js',
port: '3000',
server: [Object] }
[Smoketest] Test Suite
======================
Running: Smoketest
✔ Element <body> was visible after 58 milliseconds.
✔ Testing if element <body> contains text: "Client render".
OK. 2 assertions passed. (2.831s)
Failing CircleCI output:
REPORT_DIR=${CIRCLE_TEST_REPORTS} LOG_DIR=${CIRCLE_ARTIFACTS} npm run test:e2e -s
Running with config:
{ logDir: '/tmp/circle-artifacts.ddv7oJ3',
reportDir: '/tmp/circle-junit.hhJgS6N',
nightwatchConfig: '/home/ubuntu/react-easy-universal/nightwatch.json',
webpackConfig: '/home/ubuntu/react-easy-universal/webpack.config.js',
port: '3000',
server: [Object] }
[Smoketest] Test Suite
======================
Running: Smoketest
✔ Element <body> was visible after 85 milliseconds.
✖ Testing if element <body> contains text: "Client render". - expected "Client render" but got: Untitled
at Object.Smoketest (smoketest.js:10:15)
at Module.call (/home/ubuntu/react-easy-universal/node_modules/nightwatch/lib/runner/module.js:63:34)
at /home/ubuntu/react-easy-universal/node_modules/nightwatch/lib/runner/testcase.js:97:29
at _fulfilled (/home/ubuntu/react-easy-universal/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/home/ubuntu/react-easy-universal/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/home/ubuntu/react-easy-universal/node_modules/q/q.js:796:13)
at /home/ubuntu/react-easy-universal/node_modules/q/q.js:556:49
at runSingle (/home/ubuntu/react-easy-universal/node_modules/q/q.js:137:13)
at flush (/home/ubuntu/react-easy-universal/node_modules/q/q.js:125:13)
at nextTickCallbackWith0Args (node.js:452:9)
FAILED: 1 assertions failed and 1 passed (3.242s)
----------------------------------------------------
TEST FAILURE: 1 assertions failed, 1 passed (3.543s)
✖ smoketest
- Smoketest
Testing if element <body> contains text: "Client render". - Expected "Client render" but got: "Untitled" REPORT_DIR=${CIRCLE_TEST_REPORTS} LOG_DIR=${CIRCLE_ARTIFACTS} npm run test:e2e -s returned exit code 1
Initially, the browser starts with some rendered output from the server. The title from the server render is set to "Untitled". Then some JavaScript loads and runs in the client, and the client render happens, changing the text to "Client render". This test is intended to ensure that the client render is working as expected.
Here's the nightwatch.json:
{
"src_folders" : ["source/test/functional/browser"],
"output_folder" : "./reports/test-e2e",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : false,
"server_path" : "",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.ie.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
I've suspected it could be a race condition, thinking that the assertion runs before CircleCI has a chance to load and process the JavaScript, so I've tried inserting a pause before the assertion.
Any ideas would be appreciated.

I have also this issue and it looks like (still need to recheck) I found a solution.
Make sure you you have as last code in each of your test this at the end.
browser.end();

Related

Cannot find module '#cypress/code-coverage/task'

I'm using cypress to automate browserstack, I try to automate from local first, but when I npx browserstack-cypress run –sync, there's error:
error: Cypress could not run any of the specs in the build since an exception occurred. The stacktrace of the error is given below:
---------------------------------------------------------------------------------------------
Your pluginsFile threw an error from: .\tests\e2e\plugins\index.js
Error: Cannot find module '#cypress/code-coverage/task'
Require stack:
- .\tests\e2e\plugins\index.js
- \cypress_package\9.6.1\app\packages\server\lib\plugins\child\run_plugins.js
- \cypress_package\9.6.1\app\packages\server\lib\plugins\child\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at module.exports (.\tests\e2e\plugins\index.js:17:14)
at \cypress_package\9.6.1\app\packages\server\lib\plugins\child\run_plugins.js:94:12
at tryCatcher (\cypress_package\9.6.1\node_modules\bluebird\js\release\util.js:16:23)
at Function.Promise.attempt.Promise.try (\cypress_package\9.6.1\node_modules\bluebird\js\release\method.js:39:29)
at load (\cypress_package\9.6.1\app\packages\server\lib\plugins\child\run_plugins.js:91:7)
at EventEmitter.<anonymous> (\cypress_package\9.6.1\app\packages\server\lib\plugins\child\run_plugins.js:209:5)
at EventEmitter.emit (events.js:400:28)
at process.<anonymous> (\cypress_package\9.6.1\app\packages\server\lib\plugins\util.js:19:22)
at process.emit (events.js:400:28)
at emit (internal/child_process.js:912:12)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
cypress run process exited with code 1
cypress run failed, exiting with code: 1
And here is my code:
./browserstack.json
{
"auth": {
"username": "XXXX",
"access_key": "XXXX"
},
"browsers": [
{
"browser": "chrome",
"os": "Windows 10",
"versions": [
"latest",
"latest-1"
]
}
],
"run_settings": {
"cypress_config_file": "./cypress.json",
"project_name": "project-name",
"build_name": "build-name",
"exclude": [],
"parallels": 5,
"npm_dependencies": {},
"package_config_options": {},
"headless": true,
"spec_timeout": 10
},
"connection_settings": {
"local": false,
"local_identifier": null,
"local_mode": null,
"local_config_file": null
},
"disable_usage_reporting": false
}
./cypress.json
{
"pluginsFile": "tests/e2e/plugins/index.js",
"viewportWidth": 1280,
"viewportHeight": 800
}
./tests/e2e/plugins/index.js
module.exports = (on, config) => {
on("task", require("#cypress/code-coverage/task"));
on("file:preprocessor", require("#cypress/code-coverage/use-browserify-istanbul"));
return Object.assign({}, config, {
fixturesFolder: "tests/e2e/fixtures",
integrationFolder: "tests/e2e/specs",
screenshotsFolder: "tests/e2e/screenshots",
videosFolder: "tests/e2e/videos",
supportFile: "tests/e2e/support/index.js"
});
};
./tests/e2e/support/index.js
import '#cypress/code-coverage/support'; // I DO import code-coverage here
import "./commands";
require('cypress-xpath');
If I delete pluginsFile in cypress.json, and add test in ./cypress/integration, then everything will be just find.
I don't know that I did anything wrong, please help, thanks.

Nightwatch parallel for two browser run only default for browserstack

I have configured several test settings and I want to run parallel chrome and ie.
When I run
./node_modules/.bin/nightwatch --env chrome
test for chrome runs
./node_modules/.bin/nightwatch --env ie
test for ie runs
./node_modules/.bin/nightwatch --env ie,chrome
test for firefox runs (as I guess this run only default ones and nothing more)
Nightwatch 1.0.18
{
"src_folders": [
"test"
],
"output_folder": "reports",
"live_output" : true,
"custom_commands_path": "config/commands",
"test_runner": {
"type" : "mocha",
"options" : {
"grep": "#acc"
}
},
"test_settings": {
"default": {
"selenium_host": "hub-cloud.browserstack.com",
"selenium_port": 80,
"launch_url": "localhost",
"browserstack.key": "KEY",
"browserstack.user": "USER",
"browserstack.local": "true"
},
"chrome-local": {
"default_path_prefix": "",
"launch_url": "localhost",
"screenshots": {
"enabled": true,
"on_failure": true,
"on_success": true,
"on_error": true,
"path": "./screenshots"
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"chromeOptions": {
"args": [
"--no-sandbox",
"headless",
"window-size=1920,1200"
]
},
"acceptSslCerts": true,
"acceptInsecureCerts": true,
"elementScrollBehavior": 1
}
},
"chrome": {
"desiredCapabilities": {
"browser": "chrome"
}
},
"ie": {
"desiredCapabilities": {
"browserName": "ie",
}
}
}
}
for running parallel tests using Nightwatch refer - https://github.com/browserstack/nightwatch-browserstack
As you need to run parallel tests on Chrome and IE browsers you need to make changes to the line https://github.com/browserstack/nightwatch-browserstack/blob/master/package.json#L10 as follows-
"parallel": "./node_modules/.bin/nightwatch -c conf/parallel.conf.js
-e ie,chrome"

Nightwatch.js: why do I get Unable to create new service: GeckoDriverService?

I am trying to setup the latest nightwatch.js for the first time. This is my nightwatch.json:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "selenium-server-standalone-3.8.1.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "firefox",
"marionette": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
}
}
}
When I run nightwatch I get this error:
Starting selenium server... started - PID: 3176
[Go] Test Suite
===================
Running: Demo test
Error retrieving a new session from the selenium server
Connection refused! Is selenium server started?
{ value:
{ message: 'Unable to create new service: GeckoDriverService\nBuild info: ve
sion: \'3.8.1\', revision: \'6e95a6684b\', time: \'2017-12-01T19:05:32.194Z\'\n
ystem info: host: \'S0026COW1983\', ip: \'10.39.13.81\', os.name: \'Windows Ser
er 2008 R2\', os.arch: \'x86\', os.version: \'6.1\', java.version: \'1.8.0_131\
\nDriver info: driver.version: unknown',
error: 'session not created' },
How can I resolve this?
I will assume that you are launching the default test settings which will try to access geckoDriver.
Looks like you haven't specified the paths for your drivers, "webdriver.chrome.driver" : "", "webdriver.gecko.driver" : "", "webdriver.edge.driver" : ""
You need to point those to your driver executables, e.g. from my project:
'webdriver.gecko.driver':'/.selenium/geckodriver/0.19.0-x64-geckodriver'

Selenium 3.7.1 webdriver Error: Timed out waiting for driver server to start. Windows only

I have the following error on Windows 7 but not a a mac. It is started by nightwatch. Any ideas?
Starting selenium server... started - PID: 17228
[Test] Test Suite
=============================
Running: TestWithSettingSearchValue
Error retrieving a new session from the selenium server
Connection refused! Is selenium server started?
{ value:
{ stacktrace: 'org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.\nBuild info: version: \'3.7.1\', revision: \'8a0099a\', time: \'2017-11-06T21:07:36.161Z\'\nSystem info: host: \'W7D0596\', ip: \'172.21.1.54\', os.name: \'Windows 7\', os.arch: \'amd64\', os.version: \'6.1\', java.version: \'1.8.0_91\'\nDriver info: driver.version: unknown\r\n\tat org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:192)\r\n\tat org.openqa.selenium.remote.service.DriverService.start(DriverService.java:178)\r\n\tat org.openqa.selenium.remote.server.ServicedSession$Factory.apply(ServicedSession.java:207)\r\n\tat org.openqa.selenium.remote.server.ActiveSessionFactory.lambda$apply$11(ActiveSessionFactory.java:167)\r\n\tat
Our package.json
{
"name": "nightwatch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "nightwatch"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"chromedriver": "^2.33.2",
"nightwatch": "^0.9.16",
"selenium-server-standalone-jar": "^3.7.1"
}
}
nightwatch:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : true,
"server_path" : "./node_modules/selenium-server-standalone-jar/jar/selenium-server-standalone-3.7.1.jar",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./node_modules/chromedriver/bin/chromedriver",
"webdriver.ie.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://127.0.0.1:3000/",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
You have to use the file extension for it to work on windows. Update your config and add .exe to the chromedriver path and it should start working.
"cli_args" : {
"webdriver.chrome.driver" : "./node_modules/chromedriver/lib/chromedriver/chromedriver.exe"
}

How to use saucelabs with nightwatch?

I have the following configuration in my nightwatch.json file
"saucelabsChrome": {
"selenium_host": "ondemand.saucelabs.com",
"selenium_port": 80,
"username": "example",
"access_key": "--REDACTED---",
"desiredCapabilities": {
"acceptsSslCerts": true,
"name": "chrome",
"browserName": "chrome",
"platform": "OS X 10.11",
"version": "45.0"
}
},
However, when I run nightwatch with sauce labs
node nightwatch ---args '{"beta": "true", "env": "stage"}' --test tests/example.js -e saucelabsChrome
I get an error
Couldn't find element signUpAdobe
Two questions
Where can I see the nightwatch command running on Saucelabs?
What is the best way to integrate saucelabs with nightwatch?
Here is an example of my nightwatch.json file configured to run parallel tests on Sauce Labs.
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "custom_commands",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : false,
"server_path" : "",
"log_path" : "",
"host" : "127.0.0.1",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.ie.driver" : ""
}
},
"test_workers" : {"enabled" : true, "workers" : "auto"},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 80,
"selenium_host" : "ondemand.saucelabs.com",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"username" : "${SAUCE_USERNAME}",
"access_key" : "${SAUCE_ACCESS_KEY}",
"desiredCapabilities": {
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"chrome": {
"desiredCapabilities": {
"platform": "XP",
"browserName": "chrome",
"version": "41"
}
},
"firefox" : {
"desiredCapabilities": {
"platform": "XP",
"browserName": "firefox",
"version": "33"
}
},
"internet_explorer_10" : {
"desiredCapabilities": {
"platform": "Windows 7",
"browserName": "internet explorer",
"version": "10"
}
}
}
}
Here is a link to the entire project if you would like to take a look:
https://github.com/saucelabs-sample-test-frameworks/JS-Nightwatch.js
As to your second question. You can see the tests running in the Sauce Labs dashboard. Login at www.saucelabs.com and click on the tests tab in the top left corner.
I realize it's been a few years, but when I was searching I came across this post and found a newer guide for how to use SauceLabs with Nightwatch
Hopefully this helps anyone ending up here like I did :D

Resources