1

I have a page factory class with the following WebElements:

@FindBy(how = How.XPATH,using = "//button[contains(@aria-label, '" + today + "')]")
WebElement startDate;

@FindBy(how = How.XPATH,using = "//button[contains(@aria-label, '" + tomorrow + "')]")
WebElement endDate;

How do I create and call a method to return today's date and tomorrow's date dynamically into the 'today' and 'tomorrow' values inside the contains? From within a main method, I can get the date in the format I want using:

    Date dt = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(dt);

    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM d");
    String today = dateFormatter.format(dt);

But how do I create this and the method for tomorrow as separate re-usable methods inside of the same page factory class?

David Gray
  • 13
  • 3
  • I think, this is not possible in java, but i have seen same thing we can achieve with c#. possible duplicate of [https://sqa.stackexchange.com/questions/1498/how-to-use-seleniums-pagefactory-annotations-with-dynamic-loaded-elements](https://sqa.stackexchange.com/questions/1498/how-to-use-seleniums-pagefactory-annotations-with-dynamic-loaded-elements] – Murthi Feb 14 '18 at 05:29
  • This link redirects to a page with a 404 error. Is there an updated link? – David Gray Feb 15 '18 at 05:11

1 Answers1

0

No, you can't.

If you look into the Java Docs of FindBy these are types of Annotation but not method().

These Annotations are used to mark a field on a Page Object for locating the element or a list of elements. Essentially these Annotations are used in conjunction with PageFactory which allows the user to quickly and easily create PageObjects.

These annotations can be specified both through "how" and "using" or by specifying one of the Locator Strategies with an appropriate value to use. Both options will delegate down to the matching By methods in By class.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    I simply added the logic above into its own method inside of the page factory class, and just used driver.findElement to find the dynamic xpath value instead of trying to set it up using an @FindBy annotation. – David Gray Feb 16 '18 at 20:25