Skip to content

Instantly share code, notes, and snippets.

@prashanth-sams
Created October 16, 2014 12:41
Show Gist options
  • Save prashanth-sams/4fb3d3af67901442523e to your computer and use it in GitHub Desktop.
Save prashanth-sams/4fb3d3af67901442523e to your computer and use it in GitHub Desktop.
Package packagename;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;
public class className {
private static WebDriver driver;
private static String baseUrl;
@BeforeTest
public void setUp() {
driver = new ChromeDriver();
baseUrl = "https://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider = "DP")
public static int findcount(String keyword1, String keyword2)
throws InterruptedException {
int res = 0;
driver.get(baseUrl + "/");
driver.findElement(By.name("q")).sendKeys(keyword1);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys(keyword2);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
driver.findElement(By.name("q")).clear();
return res;
}
@DataProvider(name = "DP")
public static String[][] excelRead() throws Exception {
File excel = new File("/Users/.../data.xls");
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[(rowNum - 1)][colNum];
int k = 0;
for (int i = 1; i < rowNum; i++) {
HSSFRow row = ws.getRow(i);
for (int j = 0; j < colNum; j++) {
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[k][j] = value;
System.out.println("the value is: " + value);
}
k++;
}
return data;
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
public static void main(String[] args) throws Exception {
String[][] data;
data = excelRead();
if (data != null) {
int j = 0;
String aValue = null;
for (int i = 1; i < data.length; i++) {
int pageCount = findcount(data[i][1], data[i][2]); //vary depending upon the Excel columns
if (pageCount > j) {
j = pageCount;
aValue = data[i][1];
}
System.out.println("The count of the data[" + i + "][1] is: " + data[i][1] + " = " + pageCount);
}
System.out.println("Greater value: " + aValue + " = " + j);
} else {
System.out.println("No records...");
}
}
public static String cellToString(HSSFCell cell) {
Object result;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
default:
throw new RuntimeException("Unknown Cell Type");
}
return result.toString();
}
}
@amikaila
Copy link

amikaila commented Apr 5, 2016

image

on 51 line I am getting error...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment