SelectorsHub helps to write and verify the XPath and cssSelector.
SelectorsHub is the complete new way to write and verify the XPath and cssSelectors.
SelectorsHub auto suggests all attributes, text and everything along with their occurrences to complete Selectors quickly. Now you need not to copy and paste attribute values from DOM anymore to build XPath and cssSelector. It also supports shadowDOM, iframe and SVG elements. It gives the proper error message like what is wrong in your xpath and cssSelector.
Click on the download link for whichever browser you want.
2) Click on Add to Chrome.
3) After adding the extension, it will show in the browser toolbar like this. You can pin to the toolbar by clicking on the pin icon.
4) After adding the extension, restart the browser.
5) Now open DevTools by right clicking on any element and clicking on inspect.
6) On the right side of the Elements tab, SelectorsHub will be the last tab as shown in below image. If not visible, expand the sidebar or click on the two arrow icons as shown in below gif.
7) Now here you start typing your xpath or cssSelector. You will get auto suggest for inspected element.
Why to use SelectorsHub while there are so many other good XPath tools & selectors tools?
Biggest reason is, SelectorsHub helps to improve XPath and cssSelector writing skills.
Not one reason, there are many reasons which makes SelectorsHub the unique and best xpath tool.
SelectorsHub is the only tool which made it possible to write own selectors in less than 5sec with its auto suggest feature without compromising learning skills.
SelectorsHub is the only tool which supports #shadowDOM, in fact even Chrome DevTools doesn’t support shadowDOM.
SelectorsHub is the only tool which gives the proper error message for the missing elements in your selectors.
It helps you to improve your xpath and cssSelectors writing skills.
It has iframe support.
It supports svg elements.
It supports dark theme.
ShadowDOM Support
It was never possible to verify and write the cssSelectors for shadowDOM elements but this amazing innovation made it possible.
Proper error message for missing elements in selectors
Earlier there was nothing which can tell us what’s wrong in our selector. We were never able to understand what’s wrong in our selector until we are not expert in it. Devtools suggests 0 matching node for wrong selector pattern. But now we have the Selector’s guide which helps us with the correct error message and what is wrong or missing in our selector.
Iframe support
It supports iframes as well. It will let you know if the inspected element is inside an iframe or not. Now we can easily write and verify selectors for elements inside an iframe without wasting any time.
SVG element support
Many of us are not aware that svg elements don’t support the standard xpath format. SVG elements support different xpath patterns. SelectorsHub has made it easy and let you know the correct format and helps you to learn how to write selectors for svg elements.
Looking forward to see some more interesting features in SelectorsHub.
About Selenium Online Training Course
With Selenium Training, We help you to learn Selenium. It is one of the top automation testing tool. As part of the training you will learn about Selenium components like Selenium IDE, RC, WebDriver and Grid through hands-on projects and case studies in this Selenium certification training.
What you will learn in this Selenium online Training Course?
Selenium Automated Testing advantages
Advanced study of SIKULI, TestNG Plugin in Eclipse
Learn about Object Repository and Maven
Introduction to Selenium WebDriver
WebDriver programs like Textbox, Checkbox and multiple Windows
Use Selenium Grid for software testing
Jenkins
Who should take this Selenium 3.0 Training Course?
Software Developers, Testers, QA Engineers
System Analysts, Administrators, BI and ETL professionals
What are the prerequisites for taking this Selenium Certification Training Course?
Having a basic knowledge of C or Java is beneficial but not mandatory for taking this Selenium certification training.
In this article, i will talk about how to use Map (Hashmap) with TestNG DataProvider for Data Driven Testing in Selenium WebDriver. Before to this, let us understand little more about Map.
What is Map in Java?
—> Map is a interface in java and can implements various classes in java like Hashmap, Hashtable & many other. They are the part of collections framework and are used to handle. store data. Map interface represent the mapping in between key and value. What that mean it, in Map or hashmap, value is stored in the form of Key & Value pair. In this article i will try to demonstrate the same.
Syntax to define map –> Map mapA = new HashMap();
How to add value in map? –> mapA.put(“key1”, “element 1”);
How to get value from map? –> Object value = mapA.get(key);
–> Value from map can also be retrived by using Iterator. Shown in above screenshot.
Now, how to use Map to with TestNG Dataprovider in Data Driven Testing?
Let us suppose my Test data file is located at: C://Users//Prakash//Desktop//TestData.xlsx & it looks as shown in below image –
Now, our task is to pass value from row 2 onward to our test case using Map. Have a look on below Selenium Java code –
package demoPackage1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Testclass {
@Test(dataProvider = "data")
public void integrationTest(Map<Object, Object> map) {
System.out.println("-------------Test case started ----------------");
System.out.println(map.get("UserName"));
System.out.println(map.get("Password"));
System.out.println(map.get("DoB"));
System.out.println("-------------Test case Ended ----------------");
}
@DataProvider(name = "data")
public Object[][] dataSupplier() throws IOException {
File file = new File("C://Users//Prakash//Desktop//TestData.xlsx");
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
wb.close();
int lastRowNum = sheet.getLastRowNum() ;
int lastCellNum = sheet.getRow(0).getLastCellNum();
Object[][] obj = new Object[lastRowNum][1];
for (int i = 0; i < lastRowNum; i++) {
Map<Object, Object> datamap = new HashMap<>();
for (int j = 0; j < lastCellNum; j++) {
datamap.put(sheet.getRow(0).getCell(j).toString(), sheet.getRow(i+1).getCell(j).toString());
}
obj[i][0] = datamap;
}
return obj;
}
}
Once you run above test case, Console output will looks like –
[TestNG] Running:
C:\Users\Prakash\AppData\Local\Temp\testng-eclipse-1161438956\testng-customsuite.xml
-------------Test case started ----------------
user1
pass1
dob1
-------------Test case Ended ----------------
-------------Test case started ----------------
user2
pass2
dob2
-------------Test case Ended ----------------
-------------Test case started ----------------
user3
pass3
dob3
-------------Test case Ended ----------------
PASSED: integrationTest({UserName=user1, DoB=dob1, Password=pass1})
PASSED: integrationTest({UserName=user2, DoB=dob2, Password=pass2})
PASSED: integrationTest({UserName=user3, DoB=dob3, Password=pass3})
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7c30a502: 47 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@7e0ea639: 15 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3ac3fd8b: 47 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@71be98f5: 16 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@511baa65: 15 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
In Above Console O/P you can clearly see that, test case has been run 3 times with 3 set of test data & is taken from Excel sheet using testNG DataProvider.
Refer below video to understand more about How to use map and hashmap along with testng dataprovider –
Hope above example helps !!!
For more about Test Automation Framework, refer below links –
Before we start with data driven framework, let us understand few important things.
Why not to hard code in Test Automation Framework?
–>Let us take an example of testing web application. In actual, you may need to run test cases in various environments like Unit Testing, System Testing, System Integration Testing, UAT & can be in Production. So for all these regions, URL will be different, username and password can be different. So if you are hard coding these things, you need to change them for each environment. So its not good practice to hard code the things which are used at multiple places.
Data Driven Framework:
It is nothing but to execute one test case with multiple set of data, with multiple conditions. When you wanted to execute similar flow (let us say account opening procedure) with various data combinations, it is not good idea to create separate test case for each data set, so this is how data driven frameworks came in to picture.
Components of Data Driven Framework:
Create a folder Structure for your framework, refer below screenshot in which i have shown a sample folder structure for data driven framework.
1. Package for Page Object –>
This package will contain all page object. For each page there should be one class containing all objects / web-elements from that page. This helps to keep changing web-element away from our actual test cases. This is to achieve Page Object Model (POM) in our Test Automation Framework.
2. Package for Properties file –>
This package will contain all properties file to store data like URL, credentials & other. So that any changes in dynamic parameters like URL, username, passwords need not to change the values in code. Just change in these properties file, should update it in all test cases.
3. Package to keep Test Data –>
This package is to keep test data of data driven testing. This data can be kept in excel file format. Apache POI / JXL api can be used to read this data & use it for test cases during execution. Test data can also be kept in notepad, any database. But preferred one is Excel.
4. Package to write actual Test Cases –>
In a framework, code for actual test cases should be isolated from other commonly used methods & files. So this package is to write the actual test cases. Other classes from other packages can be called while writing test cases.
5. Utilities used for framework –>
As i said above, in framework, only actual test cases should be kept in one package & rest other common methods should be kept in another one. This utilities package will contain all common methods that are required for Test Automation Framework. For Example – Take Screenshot, logging, common skeleton to launch driver, listeners, reporters etc.
6. Create one folder to keep all jar files (If you are not using Maven in your project)
7. Create one folder to store log information (with the help of log4j & listeners)
Now, refer the below code from each of the module for complete Data Driven Framework
Step 1: Create the folder structure as shown above. Step2: Create config.properties file in propFiles package, refer below properties file
Step3: Create Log4j.properties file under src folder which is used for logging information. Refer below code for log4j.properties
// Here we have defined root logger
log4j.rootLogger=ALL,CONSOLE,R
// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
// Here we define log file location
log4j.appender.R.File=./log/testlog.log
// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
Step4: Identify and write page objects which you require for your test case. This will achieve Page Object Model (POM). For my sample example, i have identified objects for login test case. Refer below objects. HomePageObjects:
package com.demowebshop.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class HomePage {
public static String LoginLinkXpath = "//a[@class='ico-login']";
public static WebElement LoginLinkWebElement(WebDriver driver){
return driver.findElement(By.xpath(LoginLinkXpath));
}
}
LoginPageObject:
package com.demowebshop.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
public static String EmailFieldXpath = "//input[@id='Email']";
public static String PasswordFieldXpath = "//input[@id='Password']";
public static String LoginButtonXpath = "//input[@class='button-1 login-button']";
public static WebElement EmailFieldWebElement(WebDriver driver){
return driver.findElement(By.xpath(EmailFieldXpath));
}
public static WebElement PasswordFieldWebElement(WebDriver driver){
return driver.findElement(By.xpath(PasswordFieldXpath));
}
public static WebElement LoginButtonWebElement(WebDriver driver){
return driver.findElement(By.xpath(LoginButtonXpath));
}
}
Step5: Now, its time to write utilities.
First one is to read properties file. Data stored in properties file like URL, browser & other need to be used in test cases. So below class will help to read properties file.
package com.demowebshop.utilities;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertiesFile {
Properties prop = new Properties();
String filePath = "C://Users//Prakash//workspace//ddf//src//com//demowebshop//propFiles//config.properties";
public String ReadPropertiesFileByKey(String key) throws FileNotFoundException{
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
try {
prop.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
return prop.getProperty(key);
}
}
Now, once we get browser value from properties file, select the browser using below class
package com.demowebshop.utilities;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class SelectBrowser {
Logger log = Logger.getLogger("BrowserSelection");
public WebDriver selectBrowser() throws Exception {
ReadPropertiesFile prop = new ReadPropertiesFile();
WebDriver driver = null;
String browser = prop.ReadPropertiesFileByKey("browser");
while (browser == null) {
log.fatal("Browser is not specified in Configuration file. Terminating process !!!");
System.exit(0);
}
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
log.info("Browser selected for testing is : Mozilla Firefox");
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
driver = new ChromeDriver();
log.info("Browser selected for testing is : Google Chrome");
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
log.info("Browser selected for testing is : Internet Explorer");
} else {
log.fatal("Selected browser value should be either firefox or chrome or ie --> Update in Configuration file is required");
System.exit(0);
}
return driver;
}
}
Since we are using data driven testing, our test data is stored in excel file. This excel file need to read & process the test cases based on test data. Use below class to read excel file –
package com.demowebshop.utilities;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.log4testng.Logger;
public class ExcelReader {
static ReadPropertiesFile prop = new ReadPropertiesFile();
Logger log = Logger.getLogger(ExcelReader.class);
static FileInputStream fis = null;
public FileInputStream getFileInputStream() throws FileNotFoundException{
String filepath = prop.ReadPropertiesFileByKey("filepath");
File srcfile = new File(filepath);
try {
fis = new FileInputStream(srcfile);
} catch (FileNotFoundException e) {
log.fatal("TestData File is not found. terminating process !!! Check Configuration file for file path of TestData file");
System.exit(0);
}
return fis;
}
public Object[][] getExceData() throws Exception{
fis = getFileInputStream();
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
int TotalNumberOfRows = (sheet.getLastRowNum()+1);
int TotalNumberOfCols =2;
String[][] arrayExcelData = new String[TotalNumberOfRows][TotalNumberOfCols];
for (int i = 0; i<TotalNumberOfRows; i++){
for (int j=0; j<TotalNumberOfCols; j++){
XSSFRow row = sheet.getRow(i);
// String cellData = row.getCell(j).toString();
arrayExcelData[i][j] = row.getCell(j).toString();
}
}
wb.close();
return arrayExcelData;
}
}
Its time to create a common skeleton (to use driver from another class, which will be common for @BeforeMethod and @AfterMethod annotation. Refer below code –
package com.demowebshop.utilities;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class BaseTest {
SelectBrowser select = new SelectBrowser();
ReadPropertiesFile prop = new ReadPropertiesFile();
protected WebDriver driver = null;
@BeforeMethod
public void launchBrowser() throws Exception{
String url = prop.ReadPropertiesFileByKey("URL");
driver = select.selectBrowser();
driver.navigate().to(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@AfterMethod
public void closeBrowser(){
driver.close();
driver.quit();
}
}
Once all this is done, Its time to write actual test. Use dataprovide annotation to pass the data which we read it from excel reader.
I am using demowebshop.tricentis.com & its login functionality to demonstrate.
Refer below code –
package com.demowebshop.Tests;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.demowebshop.pageObjects.HomePage;
import com.demowebshop.pageObjects.LoginPage;
import com.demowebshop.utilities.BaseTest;
import com.demowebshop.utilities.ExcelReader;
public class LoginTest extends BaseTest{
@Test(dataProvider = "TestData1")
public void LoginTestCase( String username, String password){
System.out.println(username);
System.out.println(password);
HomePage.LoginLinkWebElement(driver).click();
LoginPage.EmailFieldWebElement(driver).sendKeys(username);
LoginPage.PasswordFieldWebElement(driver).sendKeys(password);
System.out.println(driver.getTitle());
}
@DataProvider(name = "TestData1")
public Object[][] LoginTestData() throws Exception{
ExcelReader ER = new ExcelReader();
return ER.getExceData();
}
}
Once this is done, Update your test data file (for Data Driven Framework) with your username and passwords, as shown below –
Now, you are good to run Test. Right click on your actual test case, Run it as TestNG Test.
You should good results.
To learn how the same can be achieved using Map and HashMap, refer –