This project is to automate Ecosia web application using NightwatchJS framework.
NightwatchJS is E2E testing solution for web applications
- Install NodeJS on your machine, go to NodeJS for respective platform downloads
- Install npm , (Node Package Manager) from npm this is a package manager helps in installing required dependencies with apt versions. For beginners this is just like Maven in Java projects.
npm init --y
this command will createpackage.jsonfile with default values. You can update project name, version, scripts, licenses.npm install nightwatch
this command will install nightwatch dependency with latest stable version available.npm install chromedriver
this command will install chromedriver dependency and no more configuration needed unlike old way of setting browser path & references.- Current test automation frameworks run based on configuration starting from test runner, browser capabilities, environments, features/specs/steps/pages references, reporting etc. Similarly Nightwatch also used
nightwatch.conf.jsconfiguration file. Add a file with same name and copy paste below code.
module.exports = {
"src_folders" : ["tests"],
"webdriver" : {
"start_process": true,
"server_path": "node_modules/.bin/chromedriver",
"port": 9515
},
"test_settings" : {
"default" : {
"desiredCapabilities": {
"browserName": "chrome"
}
}
}
}
- Refer to docs (https://nightwatchjs.org/guide/configuration/settings.html) for complete settings default values.
- Global file usage, you can define
globals.jsan external file instead of having them in nightwatch config file. you can setup hooks like before, after, beforeEach, afterEach which are mostly re-usable for multiple specs copy the below code as its defaults
module.exports = {
'default' : {
isLocal : true,
},
'integration' : {
isLocal : false
},
// External before hook is ran at the beginning of the tests run, before creating the Selenium session
before: function(done) {
// run this only for the local-env
if (this.isLocal) {
// start the local server
App.startServer(function() {
// server listening
done();
});
} else {
done();
}
},
// External after hook is ran at the very end of the tests run, after closing the Selenium session
after: function(done) {
// run this only for the local-env
if (this.isLocal) {
// start the local server
App.stopServer(function() {
// shutting down
done();
});
} else {
done();
}
},
// This will be run before each test suite is started
beforeEach: function(browser, done) {
// getting the session info
browser.status(function(result) {
console.log(result.value);
done();
});
},
// This will be run after each test suite is finished
afterEach: function(browser, done) {
console.log(browser.currentTest);
done();
}
};
- Reporting
- Create a separate folder for tests in your project, e.g.: tests. Each file inside it will be loaded as a test suite by the Nightwatch test runner.
- Here's a basic test suite example which opens the search engine Ecosia.org, searches for the term "nightwatch", then verifies if the term first result is the Nightwatch.js website.
module.exports = {
'Demo test ecosia.org' : function(browser) {
browser
.url('https://www.ecosia.org/')
.waitForElementVisible('body')
.assert.titleContains('Ecosia')
.assert.visible('input[type=search]')
.setValue('input[type=search]', 'nightwatch')
.assert.visible('button[type=submit]')
.click('button[type=submit]')
.assert.containsText('.mainline-results', 'Nightwatch.js')
.end();
}
};
- BDD example ...
- npx nightwatch [source] [options] - in this source is the test file location and options are different capabilities to run
for our tests run the following command
npx nightwatch tests/Demo.js
It invokes chrome browser with single test
OR
- Add "tests/" to the source folder in config file in this way
"src_folders" : ["tests/"], - Add nightwatch to the test script in package.json in this way
"scripts": {
"test": "nightwatch"
},
- Now run
npm test
this command will trigger all tests and its subfolder tests via Chrome browser
Install the latest Docker engine on the local machine following instructions here.
Run following in nightwatchJSfolder:
docker build -t modus/nightwatchjs:latest .Run tests locally:
docker run -it modus/nightwatchjs:latestTo push the image to a private repo, we have to retag the image to point on the target docker repository. Otherwise, the default is Dockerhub.
Push to DockerGub (need credentials for DockerHub)
docker push modus/nightwatchjs:latestPush to private repository:
docker tag modus/nightwatchjs:latest path-to-private-repo.com/repo/nightwatchjs:latestWhere path-to-private-repo.com/repo/nightwatchjs:latestis private target docker repository.
Credentials for the target docker repository are needed for the push.
Push image with:
docker push path-to-private-repo.com/repo/nightwatchjs:latestDocker image is built and pushed in the target repository. Install kubectl tool and configure access to Kubernetes cluster.
Installation instructions are here.
Alternatively, run on a local machine mini cube - follow instructions here.
Save following YAML as modus-test-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: modus-nightwatch-test-job
spec:
template:
spec:
containers:
- name: modus-nightwatch-test-job
image: modus/nightwatchjs:latest Previous YAML assume that docker image is pushed on DockerHub.
Deploy job on kubernetes:
kubectl apply -f modus-test-job.yamlFind exact name of the jobs pod with:
kubectl get podsUsing kubectl logs modus-nightwatch-test-job-XXX get logs/results where modus-nightwatch-test-job-XXXis the name of the job.
On development/testing, cluster logs could be redirected to the central logging system, and results could be visible there.