3

Please read the whole Q before disliking or commenting something. I have searched on internet before posting it here. I'm having the below project structure.

pages(package)
    > Homepage.java
test(package)
    > Flipkart.java 

Inside Homepage.java i have declared all the WebElements using POM Page Factory methods and created respective method to click on Electronics link.

@FindBy(xpath = '//input[@title='Electronics']')
private WebElement lnkElectronics;

Inside Fipkart.java I'm calling the Electronics click method.

My doubt over here is the declared WebElement is specifically for Electronics.

Is there a way i can create a WebElement with type like mentioned below and pass value for %s dynamically from main method?

@FindBy(xpath = '//input[@title='%s']')
private WebElement lnkElectronics;
Sandeep Raulo
  • 154
  • 1
  • 5
  • 21
  • Not if you wish to use the FindBy annotation, which requires a constant value. One possible work-around is to instead create a List of WebElements if you can narrow them down to the ones you want, and write a method in your page class to pass a parameter and search for that particular item in the array. – Bill Hileman Mar 14 '18 at 18:43

3 Answers3

2

Answer referenced from Page Object Model in Selenium

You cannot create a FindBy with Variable, FindBy accepts only constants.

In case if you want to achieve that variability then you should write or find the element using normal findElement method

1

As per the Test Design Consideration following Page Object Design Pattern :

  • A Page Object is an object-oriented class that serves as an interface to a page of the Application Under Test. Your @Tests uses the methods of this Page Object class whenever they need to interact with the User Interface of that page. The benefit is that if the UI changes for the page your @Tests themselves don’t needs to be changed. Only the code within the Page Object needs to be changed.

    Advantages :

    • Clean separation between test code and page specific code such as locators, methods and layout.
    • A single repository for the operations offered by the page rather than having these services scattered throughout the tests.

Based on these Page Factory features you won't be able to create any generic WebElement for which you can pass value dynamically from main() or @Test annotated method e.g.

@FindBy(xpath = '//input[@title='%s']')
private WebElement lnkElectronics;

You can find almost a similar discussion in Where should I define modal specific code in Selenium Page Object Model pattern

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

WorkAround : on page class you can define a method, and can pass the text on the fly from the calling class to click on specific tab

if you want to click any common text on the page. You can create a method as given below and can pass the text on the fly to click on that specific tab on that page

  public void clickTab(String tabText){
       String tabxpath = "//div[contains(text(), '" + tabText + "')]";
       driver.findElement(By.xpath(tabxpath)).click();

   }
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50