Wednesday, 26 June 2013

Data Driven (Automation) Sample 1

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;

//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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
//import org.openqa.selenium.support.ui.Select;



public class driven 
{
public static void main(String[] args) throws Exception
{
String[][] data;
data = excelRead();
double ref;
for (int i=1; i<data.length; i++)
{
ref = checkTestNG(data[i][0],data[i][1]);
System.out.println(" Emails from :  "+ data[i][0]+ "  and  " + data[i][1] + " is "+ref);
}
}
/* public static double setContact(String contact)
{
WebDriver wd = new FirefoxDriver();
}
*/
public static double checkTestNG(String emails, String helps) throws Exception
{
WebDriver d = null;
String browserType = "Chrome";
if(browserType.equals("IE"))
{
d = new InternetExplorerDriver();
}
else if(browserType.equals("Chrome"))
{
System.setProperty("webdriver.chrome.driver","c:\\selenium\\Jar Files\\chromedriver.exe");
d = new ChromeDriver();
d.get("your url");
}
else if(browserType.equals("Firefox"))
{
d = new FirefoxDriver();
}
//Thread.sleep(5000);
d.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement contact = d.findElement(By.cssSelector("button[id='new-issue']"));
contact.click();
//Thread.sleep(2000);
WebElement name = d.findElement(By.cssSelector("#username"));
name.sendKeys("a");
//Thread.sleep(2000);
WebElement email = d.findElement(By.cssSelector("#user-email"));
email.sendKeys(emails);//("a@gmail.com");
//Thread.sleep(2000);
WebElement desc = d.findElement(By.cssSelector("#new-issue-body"));
desc.sendKeys(helps);//("body");
//Thread.sleep(2000);
WebElement go = d.findElement(By.cssSelector(".modal-submit-button"));
go.click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}

  public static String[][] excelRead() throws Exception{
File f = new File("C:\\selenium\\excel1.xlsx");
FileInputStream fis = new FileInputStream(f);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum()+ 1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for (int i=1; i<rowNum; i++)
{
XSSFRow row = ws.getRow(i);
for (int j=0; j<colNum; j++)
{
XSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println("the value is " + value);
}
}
return data;
}
/*catch(Exception e)
{
e.printStackTrace();
}*/

public static String cellToString(XSSFCell cell)
{
int type;
Object result;
type = cell.getCellType();

switch (type)
{
case 0:
result = cell.getNumericCellValue();
break;
case 1:
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("There are no suport for this type of cell");

}
return result.toString();

}

}









the excel data :

email               help
a@aol.com a
b@aol.com b
c@aol.com         c
d@aol.com d
e@aol.com e
f@aol.com         f
g@aol.com g