25
Jul

Integrating POM, POJO for Test driven automated test scenarios

In earlier post, POM framework was identified which segregates verification and page objects identification under different classes (Page and workflow classes).

With this approach, data is still manually entered to respective test case. Suppose we need to run same test across multitude of test data, we can define POJO for all elements on a web page and integrate it with POM for Test driven automated scenario.

Steps involving Integration POJO with POM can be summarized as follow:

#1. Create test data file for login page eg. (CSV format)
TestCaseNumber,UserName,Password,Run
TC001,User1,Password1,Yes
TC002,User1,,Yes
TC003,,Password,Yes
TC004,,,Yes

#2. Create a library function which reads above CSV File as Map eg.

/*
* Read all contents of atmTransactionFile into Map>
*/
static Map> csvData = new HashMap<>();

public Map> readCSVFile(String fileName) {
int rowCount = 0;
String csvRowData = null;

try (BufferedReader csvFileData = new BufferedReader(
new FileReader(fileName))) {
while ((csvRowData = csvFileData.readLine()) != null) {
List rowData1 = new ArrayList<>();
rowData1.add(csvRowData);
csvData.put(rowCount, rowData1);
rowCount = rowCount + 1;
}
} catch (Exception e) {
logger.error(“failed to read CSV file {}{}”, e, e.getMessage());
}
return csvData;
}

/*
* This function is used to get the row number from file where TestId is defined in data file
* It accepts “TestCaseNumber” as string parameter eg. for TC001 this () will return 1
*/

public int getRowId(String strTestId)
{
int rows = 0;
String currentRow;

try (BufferedReader csvDataFromFile = new BufferedReader(new FileReader(filename))) {
while ((currentRow = csvDataFromFile.readLine()) != null) {
String[] rowData = currentRow.split(“,”);
if(rowData[3].equalsIgnoreCase(strTestId)) { //rowData[3] is position of Run? column in test data file
break;
}
rows = rows + 1;
}
} catch (Exception e) {
logger.error(“Error occurred while fetching RowId==>”+e.getMessage());
}
return rows;
}

/*
* Reads given CSVfile, if the row passed as argument to this function has column “Run? = Yes”
* return Map of this row
*
* eg. Input: readloginPageDataFile(1)
* Output: {UserName=User1, Password=xxxx}
*
* If Run? column is not Yes, “{}” is returned
*/
public Map readRowPosInCSVFile(int Row, String headerName) {
Map results = new HashMap<>();
int posRunInCSVFile = getHeadersPosition(filename, headerName);
Map> csvFileData = null;
List headersCSVFile = new ArrayList();

headersCSVFile = getCSVHeaders(filename);
String[] headers = headersCSVFile.toArray(new String[headersCSVFile.size()]);
csvFileData = readCSVFile(filename);

String textFromList = csvFileData.get(Row).toString();
textFromList = textFromList.substring(1, textFromList.length()); // remove { from list
textFromList = textFromList.substring(0, textFromList.length() – 1); // remove } from list
String[] rowData = textFromList.split(“,”);

if (rowData[posRunInCSVFile].equalsIgnoreCase(“Yes”)) {
for (int i = 0; i < rowData.length; i++) { results.put(headers[i], rowData[i]); } } return results; } /* * Get header position in CSV File Input: CSV File Name, Header name Returns * HeaderPosition (index of 0 to n) or 999 if no match found * */ public int getHeadersPosition(String fileName, String headerName) { int position = 999; try (BufferedReader csvDataFromCSVFile = new BufferedReader( new FileReader(fileName))) { String firstRow = csvDataFromCSVFile.readLine(); String[] firstRowPos = firstRow.split(","); for (int f = 0; f < firstRowPos.length; f++) { if (firstRowPos[f].equalsIgnoreCase(headerName)) { position = f; } } } catch (Exception e) { logger.error("failed to read file headers position", e, e.getMessage()); } return position; } /* * Get headers in a file */ public List getCSVHeaders(String fileName) {
List headers = new ArrayList<>();

try (BufferedReader csvDataFromFile = new BufferedReader(
new FileReader(fileName))) {
String firstRow = csvDataFromFile.readLine();
String[] firstRowPos = firstRow.split(“,”);
for (int f = 0; f < firstRowPos.length; f++) { headers.add(firstRowPos[f]); } } catch (Exception e) { logger.error("failed to read file headers", e, e.getMessage()); } return headers; } #3. Create Page objects of login page as per earlier post. public class loginPage() { WebDriver driver; public HomePage(WebDriver driver) { this.driver = driver; } // identify all the elements on this page eg. @FindBy(id=”userName”) WebElement txtUserName; @FindBy(id=”password”) WebElement txtPassword; and so on….. // define actions on above page objects eg. public void setPassword(String pwd) { txtUserName.sendKeys(pwd); } and so on…. } #6. Create POJO for loginPage public class loginPagePojo { private String userName; private String passWord; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public static loginPagePojo getloginPageData(Map map)
{
loginPagePojo loginPojo = new loginPagePojo();

loginPojo.setUserName(map.get(“UserName”));
loginPojo.setPassWord(map.get(“Password”));

return loginPojo;
}

}

#5. Create Steps class which initiliazes POJO Data and maps it to respective Page objects, as follow:

public Map map = null;
int testCaseNumber = utilities.getRowId(“TC001”);
map = utilities.readRowPosInCSVFile(testCaseNumber, “Run?”);
loginPagePojo objLoginPojo = loginPagePojo.getloginPageData(map);
// Call to workflowclass with reference of objLoginPojo
loginPageWorkflow(objLoginPojo);

#6. Create workflow which accepts pojo of login page as follow and executes functionality as follow:

public class loginWorkflow(loginPagePojo loginPojo)
{
// Initialize the webdriver eg.
WebDriver driver;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“URL to application under test”);

// Instantiate Page objects
loginPage pageObjectLoginPage = PageFactory.initElements(driver, loginPage.class);

// Structure page objects actions to execute script workflow eg.
pageObjectLoginPage.setUserName(loginPojo.getUserName());
pageObjectLoginPage.setPassword(loginPojo.getPassWord());
pageObjectLoginPage.clickLogin();

and so on…..

}