How to set up Nightwatch framework Test in windows?
Nightwatch.js is an easy to use with Node.js based end-to-end testing for browser based applications and websites Nightwatch uses the WebDriver API to perform browser automation related tasks, opening windows and clicking links for instance.
WebDriver is now call W3C specification aiming to standardize browser automation. Web Driver is a remote control interface that enables control of user agents. It provides a platform and a restful HTTP api’s as a way for web browsers to be remotely controlled.
Maximum time Nightwatch needs to send at least 2 requests to the WebDriver server in order to perform a command or assertion as below mentioned:-
1.To request to locate an element given a CSS selector (or Xpath expression).
2.To perform the actual command/assertion on the given element.
2. Download Node.js from – https://nodejs.org/download/and install latest version
1.Open visual studio code and clicked on open folder then browse the path where you want to make project structure(New Folder).
2.Go to view option on menu bar then select terminal option as screenshot:-
3.Create file called — nightwatch.js in project structure and paste this line in it
require(‘./node_modules/nightwatch/bin/runner.js’);
package.json file in your project structure.
Press Enter for all questions and in last enter yes
5.Download the latest version of Selenium Standalone jars—
7.Copy following nightwatch.json and paste into your project . (source http://nightwatchjs.org/gettingstarted)
{
“src_folders”: [“tests”],
“output_folder”: “reports/XMLReports”,
“custom_commands_path”: “”,
“custom_assertions_path”: “”,
“page_objects_path”: “pages”,
“selenium”: {
“start_process”: true,
“server_path”: “lib/drivers/selenium-server-standalone-3.141.9.jar”,
“start_session”: true,
“log_path”: “log/”,
“host”: “127.0.0.1”,
“port”: 4444,
“cli_args”: {
“webdriver.chrome.driver”: “lib/drivers/chromedriver.exe”
}
},
“test_settings” : {
“chrome”: {
“launch_url”: “http://localhost”,
“selenium_port”: 4444,
“selenium_host”: “localhost”,
“silent”: true,
“screenshots”: {
“enabled”: false,
“path”: “screenshots/Chrome/”
},
“desiredCapabilities”: {
“browserName”: “chrome”,
“chromeOptions”: {
“args”: [
“disable-web-security”,
“ignore-certificate-errors”,
“–test-type”
]
}
}
},
“edge” : {
“desiredCapabilities”: {
“browserName”: “MicrosoftEdge”
}
}
}
}8.Set path for selenium and chrome driver in nightwatch.json.
9.Change the start_process:true
Create a example file let’s call it test.js
10.In your visual code terminal you can run
— node nightwatch -e chrome -a test.js
output:-
Source code:-
1.Pages—>login.js
var loginCommands = {
login: function() {
return this.waitForElementVisible(‘body’, 1000)
.maximizeWindow()
.verify.visible(‘@userName’)
.verify.visible(‘@password’)
.verify.visible(‘@submit’)
.setValue(‘@userName’, ‘qa@pawlytics.com’)
.setValue(‘@password’, ‘test@123’)
.pause(1000)
.click(‘@submit’)
.pause(1000);
browser
.setCookie({name:’mycokkies’,value:’qa@pawlytics.com’})
.getCookie(‘mycokkies’,function callback(result) {
console.log(result);
})
browser
.setCookie({name:’mycokkiesjwt’,value:’jwt’})
.getCookie(‘mycokkies’,function callback(result) {
console.log(result);
})
}
};
module.exports = {
commands: [loginCommands],
url: function() {
return ‘https://app.pawlytics.com/login’ },
elements: {
userName: {
selector: ‘//*[@id=”__next”]//div[1]//div[2]//div[1]//div[2]//form//div[1]//div//input’,
locateStrategy: ‘xpath’
},
password: {
selector: ‘//*[@id=”__next”]//div[1]//div[2]//div[1]//div[2]//form//div[2]//div//input’,
locateStrategy: ‘xpath’
},
submit: {
selector: ‘//*[@id=”__next”]//div[1]//div[2]//div[1]//div[2]//form//button’,
locateStrategy: ‘xpath’
}
}
};
Test–>test.js
module.exports = {
‘Pawlytics_pets_world’ : function (browser) {
//create an object for login
var login = browser.page.login();
//execute the login method from //tests/pages/login.js file
login.navigate().login();
//You can continue with your tests below:
// Also, you can use similar Page objects to increase reusability
browser
.pause(3000)
.end();
}
};
————————*—————————Good Luck————————-*————————————–*——-