Skip to content

Instantly share code, notes, and snippets.

@jimode
Created May 22, 2014 13:01
Show Gist options
  • Save jimode/c55f239287ab4fdf1137 to your computer and use it in GitHub Desktop.
Save jimode/c55f239287ab4fdf1137 to your computer and use it in GitHub Desktop.
JUnit: hyrbrid_framework
// Global Variables
public class hybrid_driver1 {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
//Test Case Variables
int xRows_TC, xCols_TC;
String[][] xData_TC;
//Test Steps Variables
int xRows_TS, xCols_TS;
String[][] xData_TS;
// Test Data Variables
int xRows_TD, xCols_TD;
String[][] xData_TD;
String vKW, vIP1, vIP2, vIP3, vTCID;
String vURL, vBrowser, vFirstName, vLastName, vEmail, vPassword, vFirstLastName;
String vE_TC, vE_TS, vE_TSDetails;
String vE_verify;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://uk.linkedin.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
xlRead_TC("C:/Users/Jimi/Selenium/Workspace/SelJ11_workfolders/HFPlan1.1.xls", "TestCases");
xlRead_TS("C:/Users/Jimi/Selenium/Workspace/SelJ11_workfolders/HFPlan1.1.xls", "TestSteps");
xlRead_TD("C:/Users/Jimi/Selenium/Workspace/SelJ11_workfolders/HFPlan1.1.xls", "TestData");
}
@Test
public void testKDFFindAColleague() throws Exception {
// Test Data variables
for (int k= 1; k<xRows_TD; k++) { // Go to each row in the Test Data Sheet.
// Which Test Data to execute
if (xData_TD[k][1].equalsIgnoreCase("Y")) {
// Test Data Variables
vURL = xData_TD[k][2];
vBrowser = xData_TD[k][3];
vFirstName = xData_TD[k][4];
vLastName = xData_TD[k][5];
vEmail = xData_TD[k][6];
vPassword = xData_TD[k][7];
vFirstLastName = vFirstName + " " + vLastName;
// Read Test Cases and Test Steps Sheet from Excel into script
// Go to each row in the TC Sheet and see if the execute flag is Y
for (int i=1; i<xRows_TC; i++) {
if ("Y".equalsIgnoreCase(xData_TC[i][4])) {
vE_TC = "Pass"; // by default the test is a pass
System.out.println("Test Case ID to execute is " + xData_TC[i][1]);
// If Y, then go to each row in the TS sheet and see if the TCID's match
vTCID = xData_TC[i][1];
for (int j=1; j<xRows_TS; j++) {
if (xData_TC[i][1].equals(xData_TS[j][0])) {
vKW = xData_TS[j][4];
vIP1 = xData_TS[j][5];
vIP2 = xData_TS[j][6];
vIP3 = xData_TS[j][7];
// Get Input Data from the Test Steps.
get_inputs();
// Execute the corresponding test steps (or Keywords)
vE_TS = "Pass";
vE_TSDetails = "No Error";
vE_verify = "Pass";
try {
execute_keywords();
} catch (Exception e) {
System.out.println("Error in TCID: " + vTCID);
System.out.println("Error in Keyword" + vKW);
System.out.println("Error is: " + e);
//vE_TC = "Fail";
vE_TS = "Fail";
vE_TSDetails = "Error is: " + e;
vE_TC = "Fail";
}
if (vE_verify.equals("Fail")) {
vE_TS = "Fail";
vE_TSDetails = "Error is: Verify Failed";
vE_TC = "Fail";
}
// Each keyword function to return a Pass or Fail along with a Test Result
xData_TS[j][8] = vE_TS;
xData_TS[j][9] = vE_TSDetails;
}
}
xData_TC[i][5] = vE_TC;
}
}
xlwrite_ts("C:/Users/Jimi/Selenium/Workspace/SelJ11_workfolders/HFResults_TS_" + xData_TD[k][0] + ".xls", "TestStepRESULTS", xData_TS);
xlwrite_tc("C:/Users/Jimi/Selenium/Workspace/SelJ11_workfolders/HFResults_TC_" + xData_TD[k][0] + ".xls", "TestCaseRESULTS", xData_TC);
}
}
// Execute the CORRESPONDING test steps (or KEYWORDS)
}
@After
public void tearDown() throws Exception {
// driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
public void get_inputs() {
if (vIP1.equals("vURL")) {
vIP1 = vURL;
}
if (vIP1.equals("vFirstName")) {
vIP1 = vFirstName;
}
if (vIP1.equals("vLastName")) {
vIP1 = vLastName;
}
if (vIP1.equals("vFirstLastName")) {
vIP1 = vFirstLastName;
}
if (vIP1.equals("vEmail")) {
vIP1 = vEmail;
}
if (vIP1.equals("vPassword")) {
vIP1 = vPassword;
}
if (vIP2.equals("vURL")) {
vIP2 = vURL;
}
if (vIP2.equals("vFirstName")) {
vIP2 = vFirstName;
}
if (vIP2.equals("vLastName")) {
vIP2 = vLastName;
}
if (vIP2.equals("vFirstLastName")) {
vIP2 = vFirstLastName;
}
if (vIP2.equals("vEmail")) {
vIP2 = vEmail;
}
if (vIP2.equals("vPassword")) {
vIP2 = vPassword;
}
}
public void execute_keywords() throws Exception{
// 1. Verify what the Keyword is
// 2. Match the keyword with the corresponding function to call
// 3. Pass the required inputs into the keyword function
if (vKW.equalsIgnoreCase("go_url")) {
go_url(vIP1);
}
if (vKW.equalsIgnoreCase("edit_input")) {
edit_input(vIP1, vIP2);
}
if (vKW.equalsIgnoreCase("button_click")) {
click_button(vIP1);
}
if (vKW.equalsIgnoreCase("click_link")) {
click_link(vIP1);
}
if (vKW.equalsIgnoreCase("verify_text")) {
vE_verify = verify_text(vIP1, vIP2);
}
if (vKW.equalsIgnoreCase("verify_value")) {
vE_verify = verify_value(vIP1, vIP2);
}
if (vKW.equalsIgnoreCase("verify_link")) {
vE_verify = verify_link(vIP1);
}
if (vKW.equalsIgnoreCase("wait_time")) {
// Thread.sleep(Long.valueOf(vIP1)*1000); - to be resolved
// Thread.sleep(5000);
String s = vIP1;
double d = Double.valueOf(s);
long l = (new Double(d)).longValue();
Thread.sleep(Long.valueOf(l)*1000);
}
}
public void edit_input(String eID, String eText) {
// Purpose: Clears text in an edit box and enters a new text
// I/P: Element identification, text to place into edit field
driver.findElement(By.xpath(eID)).clear();
driver.findElement(By.xpath(eID)).sendKeys(eText);
}
public void click_button(String eID) {
// Purpose: Clears text in an edit box and enters a new text
// I/P: Element identification
driver.findElement(By.xpath(eID)).click();
}
public void go_url(String vURL) {
// driver.get(vURL + "/");
driver.navigate().to(vURL);
}
public String verify_text(String eID, String eText) {
// Purpose: To verify that the text is present in a specific location in the application
// Input: eID, eText
// Output:
if (eText.equals(driver.findElement(By.xpath(eID)).getText())) {
return "Pass";
} else {
return "Fail";
}
}
public String verify_value(String eID, String eText) {
// Purpose: To verify that the text is present in a specific edit field in the application
// Input: EID, Text
// Output:
if (eText.equals(driver.findElement(By.xpath(eID)).getAttribute("value"))) {
return "Pass";
} else {
return "Fail";
}
}
public String verify_link(String eLinkText) {
// Purpose: To verify that the text is present in a specific edit field in the application
// Input: Link Text
// Output: Pass or Fail
if (driver.findElement(By.linkText(eLinkText)).isDisplayed()) {
return "Pass";
} else {
return "Fail";
}
}
public void click_link(String eLinkText) {
// Purpose: To verify if a link is present.
// Input: Link Text
// Output:
driver.findElement(By.linkText(eLinkText)).click();
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
//Custom methods are here
// ****Method to read XL****
// Read Test Case.
public String[][] xlRead_TC(String sPath, String iSheet) throws Exception {
// Karthik declared isheet as an Int instead of a String
File myxl = new File(sPath);
FileInputStream myStream = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(myStream);
HSSFSheet mySheet = myWB.getSheet(iSheet);
xRows_TC = mySheet.getLastRowNum()+1;
xCols_TC = mySheet.getRow(0).getLastCellNum();
System.out.println("Rows in Test Case sheet are " + xRows_TC);
System.out.println("Cols in Test Case sheet are " + xCols_TC);
xData_TC = new String[xRows_TC][xCols_TC];
for (int i = 0; i < xRows_TC; i++) {
HSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols_TC; j++) {
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
xData_TC[i][j] = value;
}
}
return xData_TC;
}
// Read Test Steps.
public String[][] xlRead_TS(String sPath, String iSheet) throws Exception {
File myxl = new File(sPath);
FileInputStream myStream = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(myStream);
HSSFSheet mySheet = myWB.getSheet(iSheet);
xRows_TS = mySheet.getLastRowNum()+1;
xCols_TS = mySheet.getRow(0).getLastCellNum();
System.out.println("Rows in Test Steps are " + xRows_TS);
System.out.println("Cols in Test Steps are " + xCols_TS);
xData_TS = new String[xRows_TS][xCols_TS];
for (int i = 0; i < xRows_TS; i++) {
HSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols_TS; j++) {
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
xData_TS[i][j] = value;
}
}
return xData_TS;
}
// Read Test Data.
public String[][] xlRead_TD(String sPath, String iSheet) throws Exception {
File myxl = new File(sPath);
FileInputStream myStream = new FileInputStream(myxl);
HSSFWorkbook myWB = new HSSFWorkbook(myStream);
HSSFSheet mySheet = myWB.getSheet(iSheet);
xRows_TD = mySheet.getLastRowNum()+1;
xCols_TD = mySheet.getRow(0).getLastCellNum();
System.out.println("Rows in Test Data are " + xRows_TD);
System.out.println("Cols in Test Data are " + xCols_TD);
xData_TD = new String[xRows_TD][xCols_TD];
for (int i = 0; i < xRows_TD; i++) {
HSSFRow row = mySheet.getRow(i);
for (int j = 0; j < xCols_TD; j++) {
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
xData_TD[i][j] = value;
}
}
return xData_TD;
}
// ****Method to write to XL****
// Write Test Steps.
public void xlwrite_ts(String sPath, String iSheet, String[][] xData) throws Exception {
File outFile = new File(sPath);
HSSFWorkbook wb = new HSSFWorkbook(); // The workbook
HSSFSheet osheet = wb.createSheet(iSheet); // The worksheet
int xR_TS = xData.length; // The length or number of rows of the 2 dimensional array
int xC_TS = xData[0].length; // The length or number of columns of the first row
for (int myrow = 0; myrow < xRows_TS; myrow++) { // Go through each row
HSSFRow row = osheet.createRow(myrow); // Create a new row
for (int mycol = 0; mycol < xCols_TS; mycol++) { // Go through each column
HSSFCell cell = row.createCell(mycol); // Create a new column
cell.setCellType(HSSFCell.CELL_TYPE_STRING); // Make the cell a string type
cell.setCellValue(xData[myrow][mycol]);
}
FileOutputStream fOut = new FileOutputStream(outFile);
wb.write(fOut);
fOut.flush();
fOut.close();
}
}
// Write Test Cases.
public void xlwrite_tc(String sPath, String iSheet, String[][] xData) throws Exception {
File outFile = new File(sPath);
HSSFWorkbook wb = new HSSFWorkbook(); // The workbook
HSSFSheet osheet = wb.createSheet(iSheet); // The worksheet
int xR_TS = xData.length; // The length or number of rows of the 2 dimensional array
int xC_TS = xData[0].length; // The length or number of columns of the first row
for (int myrow = 0; myrow < xRows_TC; myrow++) { // Go through each row
HSSFRow row = osheet.createRow(myrow); // Create a new row
for (int mycol = 0; mycol < xCols_TC; mycol++) { // Go through each column
HSSFCell cell = row.createCell(mycol); // Create a new column
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xData[myrow][mycol]);
}
FileOutputStream fOut = new FileOutputStream(outFile);
wb.write(fOut);
fOut.flush();
fOut.close();
}
}
// ****Change cell type****
public static String cellToString(HSSFCell cell) {
// This function will convert an object of type excel cell to a string value.
int type = cell.getCellType();
Object result;
switch (type) {
case HSSFCell.CELL_TYPE_NUMERIC: //0
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING: //1
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA: //2
throw new RuntimeException("We can't evaluate formulas in Java");
case HSSFCell.CELL_TYPE_BLANK: //3
result = "_";
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //4
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR: //5
throw new RuntimeException("This cell has an error");
default:
throw new RuntimeException("We don't support this cell type: " + type);
}
return result.toString();
}
}
@villisaravanans
Copy link

Dear Sir,

Can you provide your HFPlan1.1.xls speadsheet [C:/Users/Jimi/Selenium/Workspace/SelJ11_workfolders/HFPlan1.1.xls]?

i want to run script.

please send me your HFPlan1.1.xls spreadsheet to my email id [email protected].

thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment