RE: How to repeat test execution using POM when elements will have different id’s

Sample image: 

RE: How to repeat test execution using POM when elements will have different id's

Still having problem with this kind of scenario. so here’s the problem, I’ve used the POM with this project.

The scenario of the image above is the user can fill in all the details per partner, the user can add a maximum of 20 partners by clicking the add partner button.

On my script the framework that i used is data driven testing so here’s an example of my code.

PageObjects


public static WebElement txtbx_GivenName(){

try{

element = driver.findElement(By.id("bn-given-name"));

Log.info("Given name textbox found");

}catch (Exception e){

Log.error("Given name textbox is not found on the Register Page");

throw(e);

}

return element;

}
public static WebElement txtbx_LastName(){

try{

element = driver.findElement(By.id("bn-last-name"));

Log.info("Last name textbox found");

}catch (Exception e){

Log.error("Last name textbox is not found on the Register Page");

throw(e);

}

return element;

}
public static WebElement txtbx_Email(){

try{

element = driver.findElement(By.id("bn-email"));

Log.info("Email textbox found");

}catch (Exception e){

Log.error("Email textbox is not found on the Register Page");

throw(e);

}

return element;

}
public static WebElement txtbx_ContactNumber(){

try{

element = driver.findElement(By.id("bn-contact-number"));

Log.info("Contact number textbox found");

}catch (Exception e){

Log.error("Contact number textbox is not found on the Register Page");

throw (e);

}

return element;

}
// and so on for the other details

ActionClass

//Section : Input the Partner details from Excel Sheet
String sGivenName = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_GIVEN_NAME);

Log.info("Given name picked from Excel is " + sGivenName);

com.businessname.pageobjects.RegistrationPage.txtbx_GivenName().sendKeys(sGivenName);

Log.info("Given name entered in the Given name text box");
String sLastName = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_LAST_NAME);

Log.info("Last name picked from Excel is " + sLastName);

com.businessname.pageobjects.RegistrationPage.txtbx_LastName().sendKeys(sLastName);

Log.info("Last name entered in the Lastname text box");
String sEmail = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_EMAIL);

Log.info("email picked from Excel is " + sEmail);

com.businessname.pageobjects.RegistrationPage.txtbx_Email().sendKeys(sEmail);

Log.info("Email entered in the email text box");
String sContactNumber = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_CONTACT_NUMBER);

Log.info("Contactnumber picked from Excel is " + sContactNumber);

com.businessname.pageobjects.RegistrationPage.txtbx_ContactNumber().sendKeys(sContactNumber);

Log.info("Contact number entered in the contact number text box");

That is an example for the set of first partner details, here comes the tricky part, when i will add a 10 or more partners (which is up to 20 partners) i will have to write all the code like that because i’m using data driven (which i know that is the concept of data driven framework) but every time i will add a new set of partner it will have a different elements.

Example the Given name of second partner will have this kind of element,


public static WebElement txtbx_SecondPartnerGivenName(){

try{

element = driver.findElement(By.id("bn-given-name[1]")); // the element will increment to one because its for the second partner.

Log.info("Given name textbox found");

}catch (Exception e){

Log.error("Given name textbox is not found on the Register Page");

throw(e);

}

return element;

}

and by this i will have to input the elements for every partner until to 20 partners, i know some of you will not agree with this kind of approach because its tedious thing to do.

Please do give me a hint or guide me for a better way to this approach. What i tried so far is use for loop on elements, but its not working.

Note* Sir Lakshay already gave me a link on how to solve this issue a month ago but i cannot seem to find it anymore, i think it got deleted because of the new site.

Regards,

luckperez Supporter Asked on August 27, 2014 in Selenium WebDriver.
Add Comment
7 Answers

Hi anu17,

I still cannot find a solution for this but i’m trying to work on it, my problem is that every time i will add a new set of details (ex. i will add a partner 2, 3,4 and so on, the fields will have a different element. )

Here’s an example element of Given Name text box:


//for Partner 1 here's the element (its by name)

public static WebElement txtbx_GivenName(){

try{

element = driver.findElement(By.name("partner[0][person][given_name]"));

}catch (Exception e){

throw(e);

}

return element;

}

//for Partner 2 given name

public static WebElement txtbx_GivenName(){

try{

element = driver.findElement(By.name("partner[1][person][given_name]")); // As you can see the element for given name of partner 2 increment to 1

}catch (Exception e){

throw(e);

}

return element;

}

// This will increment up to [20] if the user will add up to 20 partners.

so my problem is i have to get all of the elements of all partners and write a code for all of them. which i think is not the smart way to do it.

Supporter Answered on September 1, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.