Selenium Data Driven Testing

Selenium Data Driven Testing, Read Test Data from an external file (excel/text), Create Selenium Test case and connect Test data to the Test case.

Selenium Data-Driven Testing

Write Selenium Data-Driven Test Case, Inspect HTML Elements, Identify unique locators for Web elements, and perform actions on the web elements using Selenium Webdriver methods.

Prerequisites for Writing Selenium Test Cases, Manual Test Case, Element Locator for locating elements in web pages, Selenium WebDriver API commands for performing actions on the AUT, and programming features for enhancing Selenium test cases.

What is Data-Driven Testing?, Why Data-Driven Testing, How to conduct data-driven testing, and Writing Selenium Data-Driven Test cases.

What is Data-Driven Testing?
Data-driven testing is for conducting Positive and Negative Testing.

Why Data-Driven Testing?
Data-driven testing is for conducting Positive and Negative Testing.

Difference between Parameterization and Data-Driven Testing?
For passing a single value or multiple values we use Parameterization, we use multiple values only for Data-driven testing.

How to conduct Data-Driven Testing?
By using any resource (Test Data file) we can conduct Data-driven testing.

Steps for Data-Driven Testing:
1. Read Test Data from an external file
2. Create a Test case
3. Connect Test data with the Test case

Manual Test Case:

Test Case Name: Data-Driven Testing for admin login functionality by reading test data from a text file

Test Steps:

1. Launch Browser
2. Navigate to gcrShop application Admin interface home page
3. Enter Username
4. Enter Password
5. Click OK Button

Verification Point:
Capture the current URL after click on the Login Button, and compare it with expected.

Input/Test data:
Ref: input.txt

Selenium Data-Driven Test Case

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);

FileReader fileObject = new FileReader(“C:\\Users\\user\\Desktop\\input.txt”);
BufferedReader br = new BufferedReader(fileObject);
String line;
int lineCount=0;
int Iteration=0;

while ((line = br.readLine()) !=null) {
String inputData [] = line.split(“, “, 2);
lineCount=lineCount+1;

if (lineCount>1) {
Iteration=Iteration+1;

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“http://gcreddy.com/project/admin/login.php”);
driver.findElement(By.name(“username”)).sendKeys(inputData[0]);
driver.findElement(By.name(“password”)).sendKeys(inputData[1]);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(2000);

String url= driver.getCurrentUrl();

if (url.equals(“http://gcreddy.com/project/admin/index.php”)) {
System.out.println(Iteration + ” Admin Login is Successful – Passed”);
}
else {
System.out.println(Iteration+” Admin Login is Unsuccessful – Failed”);
}
driver.close();
}
}
br.close();
fileObject.close();
}
}

 

Data-Driven Testing using TestNG

Read Test Data (String type data) from an excel file and conduct Data-Driven testing for Admin Login Functionality

Test Steps:
1. Launch Browser
2. Navigate to gcrShop Admin interface (http://www.gcrit.com/build3/admin/)
3. Enter “Username”
4. Enter “Password”
5. Click “Login” Button

Expected URL/Result:
http://www.gcrit.com/build3/admin/index.php

Verification Point:
Capture the URL after click Login and compare with expected result

Input / Test Data:
Ref: input.xls

Selenium WebDriver Test:

public class DataDriven {
static WebDriver driver;

@Test (dataProvider =”testdata”)
public void adminLogin(String User, String Pwd) throws InterruptedException{
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get(“http://www.gcrit.com/build3/admin/”);
driver.findElement(By.name(“username”)).sendKeys(User);
driver.findElement(By.name(“password”)).sendKeys(Pwd);
driver.findElement(By.id(“tdb1”)).click();
Thread.sleep(4000);
String url = driver.getCurrentUrl();

/if (url.contains(“http://www.gcrit.com/build3/admin/index.php”)){ System.out.println(“Admin Login is Successful – Passed”); } else { System.out.println(“Admin Login is Unsuccessful – Failed”); }/
Assert.assertEquals(url, “http://www.gcrit.com/build3/admin/index.php”);
}

@AfterMethod
public void closeBrowser(){
driver.close();
}

@DataProvider (name = “testdata”)
public Object[][]readExcel()throws BiffException, IOException {
File f = new File (“C:\Users\admin\Desktop\input.xls”);
Workbook w = Workbook.getWorkbook(f);
Sheet s= w.getSheet(0);
int columns = s.getColumns();
int rows = s.getRows();
//System.out.println(columns);
//System.out.println(rows);

String inputData [] [] = new String [rows][columns];

for (int i=0; i<rows; i++){
for (int j=0; j<columns; j++){
Cell c= s.getCell(j,i);
inputData[i][j]=c.getContents();
//System.out.println(inputData[i][j]);
}
}
return inputData;
}
}



Manual Testing Tutorial

Java Tutorial

Python Tutorial

SQL Tutorial

Selenium, Java, and Testing Videos

Follow me on social media: