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. 🙂

How to Handle Window based Pop up using Selenium and AutoIT?

Some times while doing testing with selenium, we get stuck by some interruptions like a window based pop up.  I have faced similar situation while doing my analysis. Since we were using a proxy to connect to internet, any page we open will ask for proxy authentication in a window based pop up. But selenium fails to handle this as it has support for only web based application. To overcome this problem we need to use AutoIT along with selenium script. AutoIT is a third party tool to handle window based applications. The scripting language used is VBScript.

So let’s take the example of the problem I was facing. Every time you open a page you get a pop up asking for authentication. To overcome the problem let create an AutoIT script first. To do so let’s create one file with any name and .au3 extension in any location. Let’s say the file name is Authentication.au3 and it’s created in the example folder of the AutoIT installation folder. So we need to know the window title of the window that is disturbing our test. Taking an example of Firefox let’s consider the window title as “Authentication Required”. The script to handle this pop up is as below which is written inside Authentication.au3. You need to write click on the file and click edit and write the following.

WinWaitActive("Authentication Required","","10")
 If WinExists("Authentication Required") Then
 Send("userid{TAB}")
 Send("password{Enter}")
 EndIf

Now save the file and click on tools and compile it. One more file with same name but with .exe extension will be created in the same location. This file needs to be used inside selenium script. How to use it. It’s pretty simple. We need to add one statement in the setUp method before calling the recursive setUp method with the URL. Below is the statement that needs to be added.

Runtime.getRuntime().exec("C:\\Program Files\\AutoIt3\\Examples\\authenticationFF.exe");

The whole path along with the file name has to be passed to exec method. Now when you fire your script your autoIT script will be fired first and the initial pop up will be handled.