Wednesday, 19 February 2014

Datadriven + JXL API | TestNG | Method 2

JXL API
Download Jxl.jar

This is the continuation of one of the previous posts from me [ Link ].  In this method, you don't need to create any reusable library and found easy compared to all the other methods. Please follow the below example test class..

Method 2

package packagename;

import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class className {
WebDriver driver;
Sheet s;

@BeforeTest
public void setUp() {
driver = new FirefoxDriver();
driver.get("http://www.quikr.com/bye");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void Quikrpass() throws Exception {
FileInputStream fi = new FileInputStream("C:\\Data.xls");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(0);
for (int row = 1; row <= s.getRows() - 1; row++) {
String input = s.getCell(0, row).getContents();
String output = s.getCell(1, row).getContents();

driver.findElement(By.className("search_field")).clear();
driver.findElement(By.className("search_field")).sendKeys(input);
driver.findElement(By.className("blue_search_button_f4")).click();

Assert.assertEquals(
driver.findElement(
By.xpath("//div[@id='welcome']/div[3]/span[2]"))
.getText(), output);
driver.navigate().back();
}
}

@Test
public void Quikrfail() throws Exception {
FileInputStream fi = new FileInputStream("C:\\Data.xls");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(0);
for (int row = 1; row <= s.getRows() - 1; row++) {
String input2 = s.getCell(2, row).getContents();
String output2 = s.getCell(3, row).getContents();

driver.findElement(By.className("search_field")).clear();
driver.findElement(By.className("search_field")).sendKeys(input2);
driver.findElement(By.className("blue_search_button_f4")).click();

Assert.assertEquals(
driver.findElement(
By.xpath("//div[@id='welcome']/div[3]/span[2]"))
.getText(), output2);
driver.navigate().back();
}
}

@AfterTest
public void tearDown() {
driver.quit();
}
}


Create an Excel sheet that resembles this:



Test Console looks the same as below after Execution


3 comments: