Play around Data and Keyword simultaneously — Hybrid Driven Framework

Mukul Bhatnagar
3 min readJan 22, 2022

Hybrid Framework

A mixture of data with a pinch of keyword

Ingredients

JAVA, Selenium, IDE (any)

Directions

v Sauté Keywords

public class Keywords {

public void click(RemoteWebDriver driver, String ObjectName, String locatorType)

throws IOException, InterruptedException{

Thread.sleep(1000);

try {

driver.findElement(this.getObject(ObjectName, locatorType)).click();

}catch (Exception e) {

//

}

}

public void enter(RemoteWebDriver driver, String ObjectName, String locatorType, String value) throws InterruptedException {

Thread.sleep(1000);

try {

WebDriverWait webDriverWait = new WebDriverWait(driver, 20);

webDriverWait.until(ExpectedConditions.elementToBeClickable(this.getObject(ObjectName, locatorType)));

driver.findElement(this.getObject(ObjectName, locatorType)).click();

new Actions(driver).sendKeys(value).build().perform();

} catch (Exception e) {

//

}

}

public void assertion(RemoteWebDriver driver, String ObjectName, String locatorType, String Expected, String value) throws InterruptedException, IOException {

boolean present = false;

try {

WebDriverWait webDriverWait = new WebDriverWait(driver, 20);

webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(this.getObject(ObjectName, locatorType)));

if (driver.findElement(this.getObject(ObjectName, locatorType)).getText().contains(value))

present = true;

}

catch (Exception e) {

//

}

if(Expected.contains(“true”))

Assert.assertTrue(present);

else Assert.assertFalse(present);

}

By getObject(String ObjectName, String locatorType) throws

IOException{

//Object Repository is opened

File file = new File(Path.CONFIG_OR_FILE_PATH);

FileInputStream fileInput = new FileInputStream(file);

//Properties file is read

Properties prop = new Properties();

prop.load(fileInput);

//find by xpath

if(locatorType.equalsIgnoreCase(“XPATH”)){

return By.xpath(prop.getProperty(ObjectName));

// ObjectName is read and its value is returned

}

//find by class

else if(locatorType.equalsIgnoreCase(“ID”)){

return By.id(prop.getProperty(ObjectName));

// ObjectName is read and its value is returned

}

//find by name

else if(locatorType.equalsIgnoreCase(“NAME”)){

return By.name(prop.getProperty(ObjectName));

// ObjectName is read and its value is returned

}else {

}

return null;

}

}

v Assemble Steps

public class Steps {

Keywords keyword = new Keywords();

public void perform(RemoteWebDriver driver,String operation, String objectName, String typeLocator,String expected, String testdata) throws InterruptedException, IOException {

switch (operation) {

case “Enter_URL”:

driver.get(testdata);

break;

case “Enter”:

keyword.enter(driver, objectName, typeLocator, testdata);

case “Click”:

keyword.click(driver, objectName, typeLocator);

default:

break;

}

if(operation.contains(“AssertElement”)){

keyword.assertion(driver,objectName, typeLocator, expected, testdata);

}

}

}

v Mix and Serve

public void ExecuteStepsFromExcel(RemoteWebDriver driver, int count) throws InterruptedException, IOException {

File file = new File(filePath + File.separator + fileName);

String value = null;

FileInputStream inputStream;

Workbook workbook = null;

try {

inputStream = new FileInputStream(file);

workbook = WorkbookFactory.create(inputStream);

} catch (Exception e) {

}

Sheet worksheet = workbook.getSheet(readSuiteNameUsingKeyFromExcel().get(count));

Row firstRow = worksheet.getRow(0);

int row = worksheet.getLastRowNum();

int column = worksheet.getRow(1).getLastCellNum();

String Testdata = null;

for (int i = 1; i <= row; i++) {

LinkedList<String> Testexecution = new LinkedList<>();

for (int j = 0; j < column; j++) {

Cell Criteria = worksheet.getRow(i).getCell(j);

String CriteriaText;

if (Criteria == null) {

CriteriaText = null;

} else {

CriteriaText = Criteria.getStringCellValue();

}

Testexecution.add(CriteriaText);

Cell cell = worksheet.getRow(i).getCell(j);

switch (cell.getCellType()) {

case Cell.CELL_TYPE_STRING:

value = cell.getRichStringCellValue().getString();

break;

case Cell.CELL_TYPE_NUMERIC:

value = Integer.toString(((int) cell.getNumericCellValue()));

break;

case Cell.CELL_TYPE_BLANK:

value = “”;

break;

}

}

String Keyword = Testexecution.get(0);

String TypeLocator = Testexecution.get(1);

String ObjectName = Testexecution.get(2);

String Expected = Testexecution.get(3);

String Testdata = Testexecution.get(4);

if (i==1) {

if (!Testdata.isBlank())

step.perform(driver, Keyword, ObjectName, TypeLocator, Expected, Testdata);

} else if (i > 1) {

step.perform(driver, Keyword, ObjectName, TypeLocator, Expected, Testdata);

}

System.out.println(“Row” + i + “ is read and action performed”);

}

System.out.println(“****TEST CASE “ + worksheet.getSheetName() + “ is executed*****”);

}

Stir Excel

v Roast Testcase

v Garnish Keywords

Expert Tips

Use Maven for dependencies

Choose TestNG for reports

--

--