|
| 1 | + |
| 2 | +<br /> |
| 3 | + |
| 4 | +# NightwatchJS |
| 5 | +<!-- CONTENTS --> |
| 6 | +## Contents |
| 7 | + |
| 8 | +- [Intro](/nightwatchJS/#Intro) |
| 9 | +- [Installation](/nightwatchJS/#Installation) |
| 10 | +- [Write Tests](/nightwatchJS/#Write-Tests) |
| 11 | +- [How to Run](/nightwatchJS/#How-to-Run) |
| 12 | + |
| 13 | +<br/> |
| 14 | + |
| 15 | +<!-- Intro --> |
| 16 | +## Intro |
| 17 | +This project is to automate Ecosia web application using NightwatchJS framework. |
| 18 | + |
| 19 | +NightwatchJS is E2E testing solution for web applications |
| 20 | + |
| 21 | +<!-- Installation --> |
| 22 | +## Installation |
| 23 | + |
| 24 | +1. Install NodeJS on your machine, go to [NodeJS](https://nodejs.org/en/) for respective platform downloads |
| 25 | +2. Install npm , (Node Package Manager) from [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) this is a package manager helps in installing required dependencies with apt versions. For beginners this is just like Maven in Java projects. |
| 26 | +3. `npm init --y` |
| 27 | + this command will create `package.json` file with default values. You can update project name, version, scripts, licenses. |
| 28 | +4. ` npm install nightwatch` |
| 29 | + this command will install nightwatch dependency with latest stable version available. |
| 30 | +5. `npm install chromedriver` |
| 31 | +this command will install chromedriver dependency and no more configuration needed unlike old way of setting browser path & references. |
| 32 | +6. 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.js` configuration file. Add a file with same name and copy paste below code. |
| 33 | +``` |
| 34 | +module.exports = { |
| 35 | + "src_folders" : ["tests"], |
| 36 | +
|
| 37 | + "webdriver" : { |
| 38 | + "start_process": true, |
| 39 | + "server_path": "node_modules/.bin/chromedriver", |
| 40 | + "port": 9515 |
| 41 | + }, |
| 42 | +
|
| 43 | + "test_settings" : { |
| 44 | + "default" : { |
| 45 | + "desiredCapabilities": { |
| 46 | + "browserName": "chrome" |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | +7. Refer to docs (https://nightwatchjs.org/guide/configuration/settings.html) for complete settings default values. |
| 53 | +8. Global file usage, you can define `globals.js` an external file instead of having them in nightwatch config file. |
| 54 | +you can setup hooks like before, after, beforeEach, afterEach which are mostly re-usable for multiple specs |
| 55 | +copy the below code as its defaults |
| 56 | +``` |
| 57 | +module.exports = { |
| 58 | + 'default' : { |
| 59 | + isLocal : true, |
| 60 | + }, |
| 61 | +
|
| 62 | + 'integration' : { |
| 63 | + isLocal : false |
| 64 | + }, |
| 65 | +
|
| 66 | + // External before hook is ran at the beginning of the tests run, before creating the Selenium session |
| 67 | + before: function(done) { |
| 68 | + // run this only for the local-env |
| 69 | + if (this.isLocal) { |
| 70 | + // start the local server |
| 71 | + App.startServer(function() { |
| 72 | + // server listening |
| 73 | + done(); |
| 74 | + }); |
| 75 | + } else { |
| 76 | + done(); |
| 77 | + } |
| 78 | + }, |
| 79 | +
|
| 80 | + // External after hook is ran at the very end of the tests run, after closing the Selenium session |
| 81 | + after: function(done) { |
| 82 | + // run this only for the local-env |
| 83 | + if (this.isLocal) { |
| 84 | + // start the local server |
| 85 | + App.stopServer(function() { |
| 86 | + // shutting down |
| 87 | + done(); |
| 88 | + }); |
| 89 | + } else { |
| 90 | + done(); |
| 91 | + } |
| 92 | + }, |
| 93 | +
|
| 94 | + // This will be run before each test suite is started |
| 95 | + beforeEach: function(browser, done) { |
| 96 | + // getting the session info |
| 97 | + browser.status(function(result) { |
| 98 | + console.log(result.value); |
| 99 | + done(); |
| 100 | + }); |
| 101 | + }, |
| 102 | +
|
| 103 | + // This will be run after each test suite is finished |
| 104 | + afterEach: function(browser, done) { |
| 105 | + console.log(browser.currentTest); |
| 106 | + done(); |
| 107 | + } |
| 108 | +}; |
| 109 | +``` |
| 110 | +9. Reporting |
| 111 | + |
| 112 | +<!-- Write Tests --> |
| 113 | +## Write Tests |
| 114 | + |
| 115 | +1. 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. |
| 116 | +2. 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. |
| 117 | +``` |
| 118 | +module.exports = { |
| 119 | + 'Demo test ecosia.org' : function(browser) { |
| 120 | + browser |
| 121 | + .url('https://www.ecosia.org/') |
| 122 | + .waitForElementVisible('body') |
| 123 | + .assert.titleContains('Ecosia') |
| 124 | + .assert.visible('input[type=search]') |
| 125 | + .setValue('input[type=search]', 'nightwatch') |
| 126 | + .assert.visible('button[type=submit]') |
| 127 | + .click('button[type=submit]') |
| 128 | + .assert.containsText('.mainline-results', 'Nightwatch.js') |
| 129 | + .end(); |
| 130 | + } |
| 131 | +}; |
| 132 | +``` |
| 133 | +3. BDD example |
| 134 | +... |
| 135 | + |
| 136 | + |
| 137 | +<!-- How to Run --> |
| 138 | +## How to Run |
| 139 | +1. npx nightwatch [source] [options] - in this source is the test file location and options are different capabilities to run |
| 140 | +for our tests run the following command |
| 141 | +`npx nightwatch tests/Demo.js` |
| 142 | +It invokes chrome browser with single test |
| 143 | + |
| 144 | +OR |
| 145 | + |
| 146 | +2. Add "tests/" to the source folder in config file in this way ` "src_folders" : ["tests/"],` |
| 147 | +3. Add nightwatch to the test script in package.json in this way |
| 148 | +``` |
| 149 | +"scripts": { |
| 150 | + "test": "nightwatch" |
| 151 | + }, |
| 152 | +``` |
| 153 | +4. Now run `npm test` |
| 154 | +this command will trigger all tests and its subfolder tests via Chrome browser |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
0 commit comments