Sunday 24 May 2015

Selenium Framework: 1 (Using Test Case scripts, Generic Library's, Test Data)

Scenario: Login to actiTIME
                Create a customer
                And verify the customer name
                Expected result: Customer should be created
Scenario: Login to actiTIME
                Create a project for the customer
                And verify the project name
                Expected result: Project should be created for the customer.
===================================================================
ClassName: ProjectAndCustomerTest.java

package com.actitime.custandprojtest;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.actitime.genericlib.Excellib;

public class ProjectAndCustomerTest {
Excellib cell=new Excellib();
@Test
public void createCustomerTest() throws EncryptedDocumentException, InvalidFormatException, IOException
{
String expVal="HDFC";
String username=cell.getExcelData("Sheet1", 1, 2);
String password=cell.getExcelData("Sheet1", 1, 3);
String customername=cell.getExcelData("Sheet1", 1, 4);
WebDriver driver=new FirefoxDriver();
   driver.get("http://hunter/login.do");
   driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
   driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys(password);
   driver.findElement(By.id("loginButton")).click();
 
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//div[text()='Tasks']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//a[contains(text(),'Projects & Customers')]")).click();
   driver.findElement(By.xpath("//input[@value='Create New Customer']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//input[@name='name']")).sendKeys(customername);
   driver.findElement(By.xpath("//input[@value='Create Customer']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   String actVal = driver.findElement(By.xpath("//a[text()='HDFC']")).getText();
 
   Assert.assertEquals(actVal, expVal,"Not Found");
   cell.setExcelData("Sheet1", 1, 5, "PASS");
   driver.quit();
 
}

@Test
public void createProjectTest() throws EncryptedDocumentException, InvalidFormatException, IOException
{
String username=cell.getExcelData("Sheet1", 2, 2);
String password=cell.getExcelData("Sheet1", 2, 3);
String projname=cell.getExcelData("Sheet1", 2, 6);
String expVal="Banking";
WebDriver driver=new FirefoxDriver();
   driver.get("http://hunter/login.do");
   driver.findElement(By.xpath("//input[@name='username']")).sendKeys(username);
   driver.findElement(By.xpath("//input[@name='pwd']")).sendKeys(password);
   driver.findElement(By.id("loginButton")).click();
 
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//div[text()='Tasks']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//a[contains(text(),'Projects & Customers')]")).click();
   driver.findElement(By.xpath("//td[table[tbody[tr[td[a[text()='HDFC']]]]]]/following-sibling::td/a[text()='add project']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   driver.findElement(By.xpath("//input[@name='name']")).sendKeys(projname);
   driver.findElement(By.xpath("//input[@value='Create Project']")).click();
   driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 
   String actVal = driver.findElement(By.xpath("//a[text()='Banking']")).getText();
   Assert.assertEquals(actVal, expVal, "Not found");
   cell.setExcelData("Sheet1", 2, 5, "PASS");
   driver.quit();
}
}
========================================================================
Excel Library

package com.actitime.genericlib;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Excellib {
String filename="C:\\Users\\NHCE\\Desktop\\MyInput.xlsx";
public String getExcelData(String sheetName,int rowNum,int cellNum) throws EncryptedDocumentException, InvalidFormatException, IOException
{
FileInputStream fis = new FileInputStream(filename);
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheet(sheetName);
Row rw = sh.getRow(rowNum);
String data = rw.getCell(cellNum).getStringCellValue();
return data;
}
@SuppressWarnings("static-access")
public void setExcelData(String sheetName, int rowNum, int cellNum, String cellValue) throws EncryptedDocumentException, InvalidFormatException, IOException
{
FileInputStream fis = new FileInputStream(filename);
Workbook wb = WorkbookFactory.create(fis);
Sheet sh = wb.getSheet(sheetName);
Row rw = sh.getRow(rowNum);
Cell cel = rw.createCell(cellNum);
cel.setCellType(cel.CELL_TYPE_STRING);
FileOutputStream fos = new FileOutputStream(filename);
cel.setCellValue(cellValue);
wb.write(fos);
}

}

No comments:

Post a Comment