There will be a need to write data into Excel.So This post tells you How to Write data into Excel.
Download Jxl jar/Apache Poi. Here I am Using Jxl jar.
package java_practise;
import java.awt.Label;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.channels.WritableByteChannel;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class testng_7
{
public WebDriver fd;
@Test
public void excel() throws Exception
{
FileOutputStream f= new FileOutputStream(“D:\\data.xls”);
WritableWorkbook wb= Workbook.createWorkbook(f);
WritableSheet ws=wb.createSheet(“sample”, 0);
ws.addCell(new jxl.write.Label(0, 0, “username”));
ws.addCell(new jxl.write.Label(1, 0, “password”));
ws.addCell(new jxl.write.Label(0, 1, “[email protected]”));
ws.addCell(new jxl.write.Label(1, 1, “passwordinvalid”));
ws.addCell(new jxl.write.Label(1, 2, tesrer));
ws.addCell(new jxl.write.Label(0, 2, “username2”));
wb.write();
wb.close();
FileInputStream ff= new FileInputStream(“D:\\data.xls”);
Workbook book= Workbook.getWorkbook(ff);
Sheet sh=book.getSheet(0);
for (int r=1;r<sh.getRows();r++)
{
fd=new FirefoxDriver();
fd.get("http://www.yahoomail.com");
fd.manage().window().maximize();
Thread.sleep(5000);
fd.findElement(By.name("login")).sendKeys(sh.getCell(0, r).getContents());
fd.findElement(By.name("passwd")).sendKeys(sh.getCell(1, r).getContents());
fd.findElement(By.id(".save")).click();
Thread.sleep(5000);
try
{
if(fd.findElement(By.linkText("Sign Out")).isDisplayed());
System.out.println("login");
}
catch (Exception e)
{
String str=fd.findElement(By.xpath(".//*[@id='fsLogin']/div/strong")).getText();
System.out.println(str);
File f=((TakesScreenshot)fd).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f, new File("D:\\pics"+r+".jpg"));
}
fd.close();
}
}
}