Example Testcase: Create a script to check login functionality of Gmail.
Steps followed:
1) Open Google.2) Click on Sign in
3) Enter Username and Password.
4) Submit Creds.
5) Check if the user is Signed In
Script:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestScript
{
@Test
public void Login() throws IOException
{
String Path="D://TestData.xls";
int row\_count;
String Username, Password;
InputStream in=new FileInputStream(Path);
_//Create an inputstream object of our Testdata File._
h4.Workbook wbook=new HSSFWorkbook(in);
_//Create XLS workbook Object_
Sheet sheet=wbook.getSheetAt(0);
_//Get the First sheet of our Workbook._
row\_count=sheet.getLastRowNum();
_//Get the used Row count of the Sheet._
for(int row=1;row<=row_count;row++)// iterate through the rows
{
WebDriver driver=new FirefoxDriver();
_//Create a Driver object and initialize it with a Firefox browser._
Username=sheet.getRow(row).getCell(0).getStringCellValue();
_//Get the Username cell value of the current row._
Password=sheet.getRow(row).getCell(1).getStringCellValue();
_//Get the Password cell value of the current row._
driver.get("https://www.google.co.in/");
_//Open Google using get method._
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.id("Email")).sendKeys(Username);
driver.findElement(By.id("Passwd")).sendKeys(Password);
driver.findElement(By.id("signIn")).click();
_//creation of custom assertion to check if mail search box is present:_
if(driver.findElement(By.id("gbqfq")).isDisplayed())
_//check if the element is displayed_
{
sheet.getRow(row).createCell(2).setCellType(Cell.CELL_TYPE_STRING);
_//Create a new cell to add the result. And set the type as String._
sheet.getRow(row).createCell(2).setCellValue("Passed");
_//add ‘PASSED’ value to cell_
}
else
sheet.getRow(row).createCell(2).setCellValue("Falied");
_//add ‘Failed’ value to cell_
driver.quit();
_//close the browser._
}
FileOutputStream out=new FileOutputStream(Path);
_//Create a FileOutput object to save result._
wbook.write(out);
_//Save EXCEL file._
out.close();
_//Close the connection and free the memory._
}
}
1) TestData File before running the test :
2) TestData File after the test :
More Documentation on interacting with EXCEL-
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html(http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html)
More Documentation on Selenium WebDriver :
http://docs.seleniumhq.org/docs/03_webdriver.jsp(http://docs.seleniumhq.org/docs/03_webdriver.jsp)