Share code for Selenium POI and Application Integration code | Selenium Forum
M
Posted on 23/06/2016
Hi,

I am working small data migration projects through selenium webdriver. I attached my sample class for Redbus Application[ Redbus.txt] to automate it. POI API Excel file [Excelpoi.txt] as back end. Kindly let me how to integrate these two class to test +ve and - ve scenarious. i am not using any framework. It would helpful for small project data verification process.


i) I attached Class file.

ii) Excel reader file.

iii) Test data for redbus Application.

May know how to pass test data from MS- Excel to My class file to verify +ve and -ve results.




[u:3ofhp1r6][b:3ofhp1r6]Excel reader : using POI API[/b:3ofhp1r6][/u:3ofhp1r6]

package Javapackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.testng.annotations.Test;

public class Excelspoi {

@Test
public void test()
{
try {
FileInputStream file = new FileInputStream(new File("D://Sample.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_BLANK:
System.out.print(cell.getStringCellValue() + "\t\t");
break;

case Cell.CELL_TYPE_FORMULA:

System.out.print(cell.getStringCellValue() + "\t\t");
break;

case Cell.CELL_TYPE_ERROR:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("\t\t");
}
file.close();
FileOutputStream out =
new FileOutputStream(new File("D://Sample.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); }

}
}



[b:3ofhp1r6][u:3ofhp1r6]ii) Application Class file:[/u:3ofhp1r6][/b:3ofhp1r6]



package Javapackage;

import java.util.List;

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

public class hello {

public WebDriver driver;

@BeforeTest
public void beforetest() throws InterruptedException
{
driver = new FirefoxDriver();
driver.get("https://www.redbus.in//");
driver.manage().window().maximize();
Thread.sleep(500);
}

@Test
public void start() throws InterruptedException
{



driver.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("coimbatore");
Thread.sleep(100);
driver.findElement(By.xpath(".//*[@id='txtDestination']")).sendKeys("Bangalore");
driver.findElement(By.xpath(".//*[@id='txtOnwardCalendar']")).click();
Thread.sleep(100);
driver.findElement(By.xpath(".//*[@id='rbcal_txtOnwardCalendar']/table[1]/tbody/tr[7]/td[4]")).click();
driver.findElement(By.xpath(".//*[@id='txtReturnCalendar']")).click();
driver.findElement(By.xpath(".//*[@id='rbcal_txtReturnCalendar']/table[2]/tbody/tr[7]/td[7]")).click();
driver.findElement(By.xpath(".//*[@id='searchBtn']")).click();
Thread.sleep(1000);
// driver.findElement(By.xpath(".//*[@id='onwardTrip']/div[2]/ul/li[1]/div/div[8]/button")).click();
List<WebElement> myElements = driver.findElements(By.xpath("//li[1]/div/div[8]/button"));
System.out.println("Size of List: "+myElements.size());
myElements.get(0).click();





}

@AfterTest
public void teardown() throws InterruptedException
{
Thread.sleep(1000);
driver.quit();
}

}


[b:3ofhp1r6][u:3ofhp1r6]iii) Test Data[/u:3ofhp1r6][/b:3ofhp1r6]

From To
Bangalore Chennai
tttttttttt Chennai
tttttttttt tttttttttt
tttttttttt Bangalore
Chennai
Bangalore

M
Replied on 24/06/2016

i don't completely understand what you're asking.


M
Replied on 27/06/2016

Hi,

How to Pass data from Excel poi Java class to Redbus java class for From and To destination Input parameters using MS- Excel.


M
Replied on 27/06/2016

why are you using xls? and not xlsx file and XLS_Reader?


M
Replied on 27/06/2016

I dont have doubt in Xls and XLSX reading problem,.

How to pass as Parameter from Excel File test data to Redbus class file to test +ve and -ve scenarios.


M
Replied on 27/06/2016

i am asking you that because you can Xls_reader.java class object to write in your excel(xlsx) files.

[quote:2hw7orpy]How to pass as Parameter from Excel File test data to Redbus class file to test +ve and -ve scenarios[/quote:2hw7orpy]
use dataprovider watch module 20.