Selenium + Node
Introduction to Selenium Webdriver Introduction to Node.js Installation and Environment set up NPM – Package Manage for Node.js Directory layout of Selenium Webdriver Node.js package Chrome Chrome with options Chrome in Mobile Emulation IE Firefox Element identification methodsAssertions in Selenium in Node.js Interacting with elements in Selenium in Node.js Basic Browser window automation Sending keys in Selenium in Node.js Synchronization in Selenium Check if Element exists Working with Tables using Selenium Performing advanced actions using Selenium in Node.js Executing JavaScript in Selenium in Node.js Working with multiple Browser Windows or tabs Working with multiple frames Handling alerts Common exceptions in Selenium Taking a screenshot in selenium Mocha – Unit testing framework Selenium gridExecuting JS in Selenium in node
Below example explains how to execute JavaScript in Selenium in Node.js
//You can execute the Java Script using below syntax.
var assert = require(‘assert’);
var webdriver = require(‘selenium-webdriver’),
By = webdriver.By,
until = webdriver.until;
var chrome = require(“selenium-webdriver/chrome”);
var options = new chrome.Options();
var driver = new webdriver.Builder().
withCapabilities(options.toCapabilities()).build();
driver.get(‘https://www.softpost.org/selenium-test-page/’);
//simple javascript
driver.executeScript(“return document.body.innerHTML;”).then(function (returnValue) {
console.log(“Return Value ->” + returnValue);
});
//using javascript function
var myfunction = function () {
return document.body.innerHTML;
}
driver.executeScript(myfunction).then(function (returnValue) {
console.log(“Return Value by myfunction -> ” + returnValue);
});
//passing arguments to javascript function
var f1 = function (element) {
element.click();
}
var e1 = driver.findElement(By.xpath(“//input[@value=’female’]”))
driver.executeScript(f1,e1).then(function (returnValue) {
console.log(“Return Value of f1 ->” + returnValue);
});
driver.sleep(5000);
driver.findElement(By.id(‘fn’)).sendKeys(‘Shaun’);
//Returning value from javascript function
var f2 = function (element) {
return element.value;
}
var e2 = driver.findElement(By.id(‘fn’));
driver.executeScript(f2,e2).then(function (returnValue) {
console.log(“Return Value of f2 ->” + returnValue);
});
driver.quit();
Web development and Automation testing
solutions delivered!!