How to do Data Driven Testing using Selenium 2 (WebDriver)?

Many of us would be wondering if data driven testing is possible using Selenium 2 (WebDriver). The answer is yes, it can be done and again with the help of testNG. Below is a simple example where I’m passing data to the google search box using a 2×2 table.

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import jxl.*;

@SuppressWarnings("unused")
public class FirstTestusingWebDriver {
private WebDriver driver;
private String baseUrl="http://www.google.co.in";
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@DataProvider(name="DP")
Object[][] createData(){
Object[][] testData = {{"Cheese", "Sudeep"}};
System.out.println("Data is getting created");
return testData;
}

@Test(dataProvider = "DP")
public void testUntitled(String str1, String str2) throws Exception {
driver.get("http://www.google.co.in/");
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys(str1);
driver.findElement(By.name("btnG")).click();
driver.findElement(By.id("lst-ib")).clear();
driver.findElement(By.id("lst-ib")).sendKeys(str2);
driver.findElement(By.name("btnG")).click();
}

@AfterClass
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}

If you are following the hybrid framework explained in earlier post, the same thing can be replicated with Selenium 2 also, of course with some changes in the code. Why do that? Because it’s the selenium version that’s going to be supported in future, has better ajax handling, emulates the test as a normal user would interact with the browser. And more over as per the selenium developers, the Selenium 1 is now deprecated and will be supported as maintenance only. šŸ™‚

26 thoughts on “How to do Data Driven Testing using Selenium 2 (WebDriver)?

  1. Could you give me an example of how to use data from an excel file? So basically, instead of using 2×2 array, I’d like to pull data from an excel file. I’m new to Selenium and have very little programming experience.

  2. I’m starting from scratch. Want to start using webdriver but long time QTP user
    so: I would like to know if you can use intellijIDEA as your ide? and if so how would
    I set up the above code within it.
    2: I would then want to modify the above to make it data driven using excel (you have the code in another post so i will use that.

    Can you help in setting up the code in intellijIDEA? or do I need to use eclipse?

    Thanks
    Joe

  3. Hi Joe,

    Yes you can use intellijIDEA as your ide. How to setup the environment is described pretty well in the video available in the below mentioned link.

    If you have difficulty following the same, please reply here so that i will post a step by step guide to set up intelllij.

    You can use the code mentione here to make it data driven. The other post has the approach for selenium RC.

  4. while this may be a silly question how do I use the code above? I don’t even know how to get that code and put it into intellij

  5. You have to create class in src folder…. the you can use this code as new class…. just change the class name in the code to the file name you have given for the new class…

    • First start with building classes for different objects, like links, textboxes, images, radio buttons, check boxes, buttons, etc. In those classes define some methods which does some operation on that kind of object. Now build a driver class that runs your test which takes the keyword like link, textboxes, images, radio buttons, check boxes, etc. and perform the test. The same thing can be achieved by using a table and an if else block where you can specify your logic. The example in my most recent post will help you how to go about testing your application using this approach.

  6. hi.. i had a task as i am new to selenium i need help.. the task is i need to type apple in google search.
    i need to collect all the view states which are avaliable into excel sheet..

    • Hi Vinay,

      To achieve this, you need to get all elements which are links and available in the page. Store them in a List object of Strings. Then loop the list to print each object in the List to an excel sheet using jxl. You will find sample code in jxl site on how to print any object.

    • Hi Vinay,

      To achieve this, you need to get all elements which are links and available in the page. Store them in a List object of WebElements.

      List list = driver.findElements(By.xpath(“//a”));

      Then get attribute you want to store (for example href) and print each object in the List to an excel sheet using jxl. You will find sample code in jxl site on how to print any object.

      for (WebElement val:list){
      String attr = val.getAttribute(“href”);
      //write the code to store the attribute in excel
      }

  7. This may be helpful for Testdata retrieval,

    Fillo fillo=new Fillo();
    Connection connection=fillo.getConnection(“C:\\Test.xlsx”);
    String strQuery=”select * from [Sheet1] where [ID]=’100′”;
    Recordset recordset=connection.executeQuery(strQuery);

    while(recordset.hasNext()){
    System.out.println(recordset.getField(“Details”));
    recordset.moveNext();
    }

    recordset.close();

    http://www.codoid.com/products/view/2/29

  8. Hi,

    Thanks for your post. It really helped me. I’m just thinking on how I’d be able to run a spreadsheet with multiple Test Suites. In other words, I have many Test Groups and each Test Group with many Test Cases, where each Test Case contains many steps. Is it possible to use your solution in this case?

    Thanks

  9. I’m using firefox , can you please explain me how to set the proxy settings.

    As while i ran my test class in eclipse IDE the firfox is opening but it is not connecting to baseurl which i had given in the class.

    • It is nothing related to proxy setting. I guess you are using the latest firefox(version 26). You need to upgrade you selenium package to the latest version to get this working.

  10. Please can any one provide how to setup a data driven frame work …. from core level with step by step procedure

  11. Hi Sudeep
    I am trying to use excel sheet for survey page which has multiple radio buttons.How would I need to implement that in my code.One question can have five options (radio buttons) and user can click one of those.What would I need to pass from excel sheet?Radio button ids /values? and would I need to find element to each radio button before that?

Leave a comment