2

Is there any way so that we can parameterize the string which we pass to create a page object using a page factory?

ex:

String v = "password";
@FindBy(name=v)
private WebElementFacade password_Field;

I am trying to push the string v into the @FindBy but I am getting an error.

I am getting The value for annotation attribute FindBy.name must be a constant expression.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Santhosh Siddappa
  • 708
  • 3
  • 14
  • 29

2 Answers2

1

Yes. It is possible. Please find the solution below:

In your Page, instead of using @FindBy, get the WebElementFacade using find method of the Page and then use it for operations.

For example: If you need to click on an element, please see the code below:

public void click(String elementId) {
    WebElementFacade element = find(ById.id(elementId));
    element.click();
}

This works perfectly for me. And the element id is coming all the way from .story examples.

Arun S
  • 55
  • 1
  • 7
0

No, Unfortunately, it's not possible to send arguments to annotation (reference). Since Java annotations does not allow dynamic parameterization. The compiler evaluates annotation metadata at compile time. So, it must be present. Though there are work around for this. Check this- Java Annotations values provided in dynamic manner

Raghwendra Sonu
  • 471
  • 3
  • 7