Saturday, April 18, 2015

How to read data from application and write to excel selenium webdriver

  Subscribe to get Updates Enter your email address:


Delivered by Mindq Software Testing Tutorials

Read data from application and write to excel --- Using selenium Webdriver

By Using JXL.jar we can write the data to excel

Here i am showing sample application to write the data to excel
Download  jxl.jar and selenium-server-standalone-2.39.0.jar and configure in the build path of java project

package smlisuzu;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Smile {
public static void main(String args[]) throws IOException,
RowsExceededException, WriteException, InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://smlisuzu.com/DealerNetwork.aspx");
driver.findElement(By.xpath(".//*[@id='lstDealerCategory_3']")).click();
//Select dropdown = new Select(driver.findElement(By.id("ddlState")));

//List<WebElement> l = dropdown.getOptions();
//int max = l.size();
  int m = 0;
int n = 0;
WritableWorkbook workbook = Workbook.createWorkbook(new File(
"/home/naveen/Videos/smlisuzu.xls"));
WritableSheet sheet = workbook.createSheet("First Sheet", 0);

driver.findElement(By.id("btnSearch")).click();


//System.out.println(states);
Thread.sleep(22334);
Select dropdown = new Select(driver.findElement(By.id("ddlPageing")));
dropdown.selectByIndex(6);
Thread.sleep(22334);
int z=0;

for(int h=3;h<364;h=h+2)
{
if (h==364){
break;
}
String cityname = driver.findElement(By.xpath(".//*[@id='dlDealer']/tbody/tr/td/table/tbody/tr["+h+"]/td[1]")).getText();

Label labelnew = new Label(0,z, cityname);
sheet.addCell(labelnew);
Thread.sleep(23);
System.out.print(cityname+z);

String office = driver.findElement(By.xpath(".//*[@id='dlDealer']/tbody/tr/td/table/tbody/tr["+h+"]/td[2]")).getText();
Label officelabel = new Label(1,z, office);
sheet.addCell(officelabel);
System.out.print(office);
String officeadd = driver.findElement(By.xpath(".//*[@id='dlDealer']/tbody/tr/td/table/tbody/tr["+h+"]/td[3]")).getText();
Label officeaddlabel = new Label(2,z, officeadd);
sheet.addCell(officeaddlabel);
System.out.println(officeadd);
for(int y=1; y<4;y++){
String mobno = driver.findElement(By.xpath(".//*[@id='dlDealer']/tbody/tr/td/table/tbody/tr["+h+"]/td[4]/table/tbody/tr["+y+"]/td[2]")).getText();
Label mobnolable = new Label(y+2,z, mobno);
sheet.addCell(mobnolable);
System.out.println(mobno);
}

z++;

}
workbook.write();
driver.close();
workbook.close();
}
}

No comments:

Post a Comment