Selenium
Mouse Click & Keyboard Event: Action Class in Selenium Webdriver
In this tutorial, we will learn handling Keyboard and Mouse Event in Selenium Webdriver Action...
An object repository is a common storage location for all objects. In Selenium WebDriver context, objects would typically be the locators used to uniquely identify web elements.
The major advantage of using object repository is the segregation of objects from test cases. If the locator value of one webelement changes, only the object repository needs to be changed rather than making changes in all test cases in which the locator has been used. Maintaining an object repository increases the modularity of framework implementation.
In this tutorial, you will learn-
Selenium WebDriver does not offer an in-built object repository by default. However, object repositories can be built using the key-value pair approach wherein the key refers to the name given to the object and value refers to the properties used to uniquely identify an object within the web page.
The following are the types of object repositories that can be created in Selenium WebDriver.
In this approach, properties file is a text file wherein data is stored on the form of key-value pairs. The below tutorial will address the following topics.
MobileTesting=//a[text()='MOBILE TESTING'] EmailTextBox = philadelphia-field-email SignUpButton = philadelphia-field-submit
4) For this tutorial, the following demo website is being used: http://demo.gtupapers.com/test/gtupapershome/. Here is Test scenario:
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties");
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties");
obj.load(objfile);
String mobileTesting = obj.getProperty("MobileTesting");
The string 'mobileTesting' will contain the XPATH to identify the Mobile Testing link within the webpage.
Properties file can be used in test scripts by reading data from a properties file and passing the data as a parameter to the findElement method. The below code demonstrates the usage of data read from properties file in test scripts.
driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click();
driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it.");
driver.findElement(By.id(obj.getProperty("SignUpButton"))).click(); The below is the complete code used for the above test scenario.
package com.objectrepository.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DemoOR {
public static void main(String[] args) throws IOException {
// Create WebDriver Instance
WebDriver driver;
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://demo.gtupapers.com/test/gtupapershome/");
driver.manage().window().maximize();
// Load the properties File
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties");
obj.load(objfile);
// Nagigate to link Mobile Testing and Back
driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click();
driver.navigate().back();
// Enter Data into Form
driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it.");
driver.findElement(By.id(obj.getProperty("SignUpButton"))).click();
}
}
XML stands for Extensible Markup Language. An XML File uses Document Object Model(DOM) as the basic structure. XML File format will replicate the HTML format upon which the webpage is constructed. Below is the list of topics that will be covered.
Data can be stored in XML file in the form of Document Object Model (DOM). For simplicity sake, we can use the below test scenario as an example.
The below is the format of XML File to be used.
<menu>
<mobiletesting>//a[text()='MOBILE TESTING']</mobiletesting>
<email> philadelphia-field-email</email>
<signup> philadelphia-field-submit </signup>
</menu>
Store the above XML code in properties.xml
In the design tab you will see
1. Reading data from XML file can be accomplished using the built-in 'dom4j' class in java. Please note that you need to add the below JAR files into the buildpath of your project before proceeding with the code.
2. Below is the code to read data from XML file.
File inputFile = new File(System.getProperty("user.dir") +"\\properties.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputFile);
String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText();
String emailTextBox = document.selectSingleNode("//menu/email").getText();
String signUpButton = document.selectSingleNode("//menu/signup").getText();
3. Initially, we need to create a File object and pass it as a parameter to the 'read' method of SAXReader class. Once the XML file data is read successfully, we can access individual nodes of XML document using the 'selectSingleNode' method.
XML file can be used in test scripts by reading data from XML file and passing the data as parameter to the findElement method. The below code demonstrates the usage of data read from XML file in test scripts.
driver.findElement(By.xpath(mobileTesting)).click();
driver.findElement(By.id(emailTextBox)).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it.");
driver.findElement(By.id(signUpButton)).click();
The below code demonstrates the use of XML file in selenium WebDriver
package com.objectrepository.demo;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DemoORXML {
public static void main(String[] args) throws DocumentException {
// Creating WebDriver Instance
WebDriver driver;
System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://demo.gtupapers.com/test/gtupapershome/");
driver.manage().window().maximize();
// Reading XML File
File inputFile = new File(System.getProperty("user.dir") +"\\properties.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputFile);
String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText();
String emailTextBox = document.selectSingleNode("//menu/email").getText();
String signUpButton = document.selectSingleNode("//menu/signup").getText();
//Navigating to Mobile Testing and back
driver.findElement(By.xpath(mobileTesting)).click();
driver.navigate().back();
//Entering Form Data
driver.findElement(By.id(emailTextBox)).sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it.");
driver.findElement(By.id(signUpButton)).click();
}
}
Download the WebDriver Eclipse Project
In this tutorial, we will learn handling Keyboard and Mouse Event in Selenium Webdriver Action...
Selenium supports Python and thus can be utilized as Selenium WebDriver with Python for testing....
We will use the Mercury Tours website as our web application under test. It is an online flight...
XSLT Report The XSLT Report in the Selenium framework is a very important feature that is used to...
In this tutorial, you will learn how to integrate Cucumber with Selenium Webdriver. What is...
SoapUI is the most popular open source functional Testing tool for Api Testing . It provides...