Welcome to Selenium Tutorials, in this Selenium Data Driven Framework step by step giving clear details about Data Driven Framework with selenium with real time examples.Most of the frameworks data driven is most important in order to run the script for positive and negative test cases and handling validation message.Framework can be finalized based upon web application (AUT) you are testing.If you are doing testing of big applications then you can go with Data Driven framework and keyword driven framework.
What is Data driven?
Data driven testing is nothing but testing the application with multiple set of data in order to verify how application behaves with positive and negative scenarios. Data driven testing can be done with the help of Excel,Text files,Csv files and Databases.In this post i am using Excel files to drive the application with multiple set up test data.Please follow below simple steps to create Data Driven Framework in Selenium WebDriver.Pre-requisites:
Below are the important pre requisite for creating Selenium Data Driven Framework step by step,please make sure you should have below details.1.Selenium Webdriver
2.jxl jar
3.Excel files as a format of xls
4.Dataprovider in TestNG
5.TestNG Library
Excel file:
Here i'm using excel file for multiple set of data to drive the application for positive and negative scenarios and created the Test Scripts with TestNG annotations,those are1.@Test
2.@DataProvider
Please read TestNG Tutorials for good understanding of TestNG annotations. Please find below Selenium Script for Data driven framework creation.
Selenium Script:
package simple_package; import java.io.FileInputStream; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class Customer_Registration { WebDriver driver; @Test(priority = 1) public void login() throws InterruptedException { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://www.wikishown.com"); System.out.println("Browser is opened and wikishown url in entered."); driver.manage().window().maximize(); Thread.sleep(2000); driver.findElement(By.id("userId")).sendKeys("xxxxxxxxx"); driver.findElement(By.id("passId")).sendKeys("xxxxxxxx"); driver.findElement(By.id("submit")).click(); System.out.println("Wikishown My Account page is loaded"); } //Calling Excel file input column names using String @Test(priority = 2, dataProvider = "Cust_Reg") public void customer_regis(String Mobileno, String Firstname, String Lastname) throws Exception, BiffException, IOException { driver.findElement(By.name("searchMobileNo")).clear(); driver.findElement(By.name("searchMobileNo")).sendKeys(Mobileno); Thread.sleep(3000); driver.findElement(By.id("searchBtn")).click(); System.out.println("search button is clicked"); Thread.sleep(2000); driver.findElement(By.id("fname")).sendKeys(Firstname); driver.findElement(By.id("lname")).sendKeys(Lastname); driver.findElement(By.id("validateBtn")).click(); System.out.println("Validate Button is clicked"); } //Identification of Excel file @DataProvider(name = "Cust_Reg") public Object[][] CustRegistration() throws Exception { FileInputStream filepath = new FileInputStream("E:\\Automation_Testing\\TestData.xls"); Workbook wb = Workbook.getWorkbook(filepath); Sheet sheet = wb.getSheet("Cust_Data"); int row = sheet.getRows(); System.out.println("number of rows" + row); int column = sheet.getColumns(); System.out.println("number of columns" + column); String Testdata[][] = new String[row - 1][column]; int count = 0; for (int i = 1; i < row; i++) { for (int j = 0; j < column; j++) { Cell cell = sheet.getCell(j, i); Testdata[count][j] = cell.getContents(); } count++; } filepath.close(); return Testdata; } }
Must Read
Launch Firefox Browser using GeckoDriverSelenium Xpath Tutorial
Verify element is enable in Selenium
Read Data From properties file using Selenium
Selenium-Testng examples to execute multiple classes
Selenium WebDriver Methods
Generate HTML Reports using Selenium
In above script i have usesd below parameters
1.String Mobileno
2.String Firstname
3.String Lastname
these are the column names in the excel file which are using for Test Data as above image and we are passing these parameters in "customer_regis" method with the help of Dataprovider method as used in "public Object[][] CustRegistration()" method where i am calling Test data excel file with the help of FileInputStream filepath = new FileInputStream("filepath") and getting particular worksheet where data is stored ,calling that sheet in order to count no of rows ,no of columns to run until the end of the test data in file with the help of for loop as seen in CustRegistration method.
Now calling CustRegistration method i have declared Dataprovider name as above and using this name in public void customer_regis() method in order to pass the values in place of static data as below.
driver.findElement(By.id("fname")).sendKeys(Firstname);
driver.findElement(By.id("lname")).sendKeys(Lastname);
1.Save above script and replace with your AUT application URL and replace all your elements.
2.Close the excel sheet
3.Run the script with TestNG Test.
You will see application will run with multiple set up test data i.e Data driven Testing.Thank you for reading,Please provide your valuable comments on this topic so that i will modify the details more efficient.Please implement for your web application here i have given the code.
Thanks for sharing a great article on Selenium!!
ReplyDeleteReally it will be helpful for both beginners and experienced professional. Your information is very clear.
Along with your post, I put my input here.
To test this you have three different approaches:
1) Create 100 scripts one for each dataset and execute each test one by one.
2) Change the data in the script and execute it multiple times.
3) Import the data from the excel sheet and execute the script multiple times with different data.
Check out some of the best selenium concepts which can help you as well.Selenium training in Chennai
HI am using your code to read data from my excel and am getting some error can u help solve that error.
ReplyDelete[RemoteTestNG] detected TestNG version 6.14.2
[Utils] [ERROR] [Error] org.testng.TestNGException:
Cannot inject @Test annotated Method [Login_To_SIWI] with [class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection
at org.testng.internal.Parameters.checkParameterTypes(Parameters.java:407)
at org.testng.internal.Parameters.createParametersForMethod(Parameters.java:356)
at org.testng.internal.Parameters.createParameters(Parameters.java:635)
at org.testng.internal.Parameters.handleParameters(Parameters.java:769)
at org.testng.internal.ParameterHandler.handleParameters(ParameterHandler.java:49)
at org.testng.internal.ParameterHandler.createParameters(ParameterHandler.java:37)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:923)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)