Monday, December 15, 2014

Data Driven Using TestNG



Am going to Read Data from excel sheet and fill the username and password of gmail.

Please find the source code below:

import java.io.FileInputStream;

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.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class EmailTest {
private WebDriver driver;

@BeforeClass
public void Startup() {
driver = new FirefoxDriver();
}

@Test(description = "Read Gmail account")
public void Login() throws Exception {
FileInputStream fi = new FileInputStream("E:\\testemail.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
driver.get("http://www.gmail.com");
try {
for (int i = 1; i < s.getRows(); i++) {
// Read data from excel sheet
String s1 = s.getCell(0, i).getContents();
String s2 = s.getCell(1, i).getContents();
driver.findElement(By.name("Email")).sendKeys(s1);
driver.findElement(By.name("Passwd")).sendKeys(s2);
driver.findElement(By.name("signIn")).click();
Thread.sleep(9000);

}
} catch (Exception e) {
System.out.println(e);
}
}

@AfterClass
public void teardown() {
driver.quit();
}
}



Hope you like this post





5 comments:

  1. import jxl.Sheet;
    import jxl.Workbook;

    Giving errors.

    ReplyDelete
    Replies
    1. Hi Gourav, you need to add jxl library(jar) as reference to your project

      Download jxl from below location
      http://www.java2s.com/Code/Jar/j/Downloadjxl2612jar.htm

      Delete
  2. Hi Mukesh,w.getSheet will return string value ,but in your code it is given w.getSheet(0),how it is possible.if i copy and paste also unable to resolve.

    Even in getRow() below in for loop i am getting error .

    for (int i = 1; i < s.getRows(); i++)

    please help me in this issue.

    ReplyDelete