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

Sample image: 

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,

Supporter Asked on August 27, 2014 in Selenium WebDriver.
Add Comment
  • 7 Answer(s)

    Hi luckperez,

    Visit http://www.toolsqa.com/selenium-webdriver/page-object-model/. It may help you.

    Regards,
    Vaidy

    Doctor Answered on August 27, 2014.
    Add Comment

    If I am able to understand your question correctly, you should be appending the [1], [2] etc in the same method call instead of creating different function.  Taking example from your method list

    
    public static WebElement txtbx_SecondPartnerGivenName(){
     
    try{
    String partnerIndex = 1 ; //or 2 or 3 or may be pick from the data sheet.
    element = driver.findElement(By.id("bn-given-name[" + partnerIndex + "]")); // 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;
     
    }
    
    Professor Answered on August 27, 2014.
    Add Comment

    The image got broken not sure why, cannot seem to post a comment so i will have to put it here. sorry for this, https://imageshack.com/i/n1eln6p

    Supporter Answered on August 27, 2014.
    Add Comment

    Hi Virender,

    Can you explain it more thoroughly? What do you mean by “may be pick from the data sheet.“? And yes you are right i can  append all my elements index so that i will not get all the elements for each partner (imagine if i do this method, i will have to get all of the elements up to 20 partners which is not the smart way). another problem is, how will my action class identify if its for the second, third, fourth, etc. partner?  Example i want to insert a given name for the second given name field

    //Section : Input the secondPartner 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);

    Note. Thanks Vaidy i already followed that POM model. the only issue i have now is for multiple execution of script.

    Supporter Answered on August 29, 2014.
    Add Comment

    Hi luckperez

    Everytime when you click on add partner button the page get appended with name,email and contact no.  And you can add upto 20 partners.  Exception arises from the second partner onwards.  It’s true how to find weather is P artner1or 2,3…
    Please post the solution if you find any

    Doctor Answered on September 1, 2014.
    Add Comment

    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

    I  am not sure but yes looping concept is required, the solution is difficult to implement with this framework

    just an approach u can change your page object class method so that it can run in loop and return list of elements
    these list of elements store the id’s of the  element txtbx_GivenName for all partner visible on page

    Looping like this
    for (int i=0;i=10;1++)  {

    element = driver.findElements(By.name("partner[" + i "][person][given_name]"));
     }

    @Edit (method=PageObject)

    public static List <WebElement> txtbx_GivenName(){
    try {
    for(int partner=0; partner <= 15; partner++){

    System.out.println(start);
    List <WebElement> element = new ArrayList<WebElement>() ;

    try {
    element.add = driver.findElements(By.name("partner[" + i "][person][given_name]"));
    Log.info("Given name textbox" + partner + "found");
    }catch(Throwable t) { System.out.println(t);}

    catch (Exception e){

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

    throw(e);

    }

    return element; // this will be same as it is list of textbox elements stored
    }

    Supporter Answered on March 25, 2015.
    Add Comment
  • Your Answer

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