Overview
In this tutorial, we will create a test in Javascript using Nightwatch.js for E2E tests, web based, running either locally or in the cloud by using Sauce Labs.
Requirements
- Install NodeJS
- Install all dependencies using "npm"
Description
This tutorial is based on some examples from this project.
You may start by cloning the project's repository.
git clone https://github.com/dwyl/learn-nightwatch
Nightwatch.js configuration is managed in a configuration file, similar to the following one.
The capabilities (devices/browsers) we want, along with the reporter/JUnit related configurations, are all defined in the previous configuraiton file.
Nightwatch.js provides a built-in JUnit reporter, which we'll use.
Please note
The tests below don't use the Page Objects pattern. However, it would be a good recommendation to do so as mentioned by Nightwatch.js.
You may find other test examples, using the Page Objects pattern, in nightwatch.js repository here.
Test 1: Verify the content of a page on GitHub
Test 2: Verify the title of some page
Please note
Beware that some Javascript files, even though in the "tests" folder, may not be actual/real test cases (e.g. the file test/e2e/upload_screenshots_to_s3.js provided in the upstream project).
Test 3: upload screenshots to S3
Running tests locally
Test(s) then can be run using NPM "test" task.
npm test
After successfully running the tests, JUnit XML files will be created, under the node_modules/nightwatch/reports/
folder (e.g. CHROME_67.0.3396.99_Mac OS X_github.xml, CHROME_67.0.3396.99_Mac OS X_guineaPig.xml).
Reports can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).
Tests are mapped to Generic Tests in Jira, and the Generic Test Definition field contains the name of the file along with the title of the function implementing the automate test.
The execution screen details will provide information on the overall test run result.
Running in the cloud using SauceLabs
Before running the test(s), you need to export some environment variables with your Sauce Lab's username along with the respective access key, which you can obtain from within the User Settings section in your Sauce Lab's profile page.
export SAUCE_USERNAME=<your Sauce Labs username> export SAUCE_ACCESS_KEY=<your Sauce Labs access key>
Test(s) then can be run in parallel using NPM.
npm run sauce
Multiple JUnit XML files will be created, one per capability (device+browser), under the node_modules/nightwatch/reports/
folder.
Please note
Since the previous command generates multiple JUnit XML files, we may need to merge them into a single XML file so it can be submitted into a Test Execution more easily. That can be achieved by using the junit-merge utility. In this case, we'll loose the visibility of results on a per Test Environment basis though.
junit-merge -o results.xml -d node_modules/nightwatch/reports/
If we want to have visibility of the results per Test Environment, then we need to separately upload them and identify the Test Environment on each REST API request.
In Sauce Labs you can see some info about it.