0

I know for a fact that in Page Object Model the DOM objects are declared in a class file pertaining to a particular page in the below format:

@FindBy(xpath = "//*[@id='page_content_inner']/div/a[1]/div")
private WebElement setupBtn;

I want to know that whether we can store these xpaths in database and access it from their.

I cannot at this stage share any further information at this stage as it is still unclear for us.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Prakash P
  • 419
  • 1
  • 8
  • 18
  • 1
    Of course it's possible, but why would you do it that way? :/ What's wrong with storing it as normal strings in your codebase? – acikojevic Dec 12 '17 at 08:37
  • 2
    Annotations require compile time constants as values. https://stackoverflow.com/questions/1458535/which-types-can-be-used-for-java-annotation-members – Grasshopper Dec 12 '17 at 09:15
  • @acikojevic Can you let know how we can do this.... Is there any reference material or code that you can share. I complete agree with you that we can store in the codebase, but I have been told to check the feasibility of the same. – Prakash P Dec 12 '17 at 15:15
  • I'm afraid acikojevic is incorrect and grasshopper is correct. You cannot use variables in FindBy. This is the -only- drawback of using it that I'm aware of. If you are determined to use an external source for the locator definitions, then you cannot use the FindBy method and will need to use the standard methods. Personally I think it is much better to include the locators directly in the page object definition files themselves. I have yet to find a scenario where I could not make that work for me, even with that limitation. – Bill Hileman Dec 12 '17 at 15:51

1 Answers1

0

Here are two ways of doing this.

Approach #1

This is going to be nothing but a hack and you will be stuck with dependencies on JDK flavors and versions as well.

Here are the high- level set of actions that you would need to do. I have created a detailed blog post that talks about how to do this in JDK7 and JDK8 [ I haven't tried this in JDK9, so your mileage may vary ]

  • Create classes that implement each of the annotations (Annotations are internally represented as interfaces, so you can build classes that implement them)
  • Use reflection to query the field that holds annotations and once you have it you instantiate the class that you created for the annotation, with values that you now need to represent (in your case, with values from the database) and then push it back into the field.

That should do it for you.

Approach #2

Here we basically piggy back on the customization support that Selenium's page factory model provides you. On a high-level here are the list of things that you need to do, to get this done. For a detailed overview, you can refer to my blog post here.

  1. A customized org.openqa.selenium.support.pagefactory.ElementLocator
  2. A customized org.openqa.selenium.support.pagefactory.ElementLocatorFactory
  3. A customized approach to deciphering the annotations and reading out the values from the annotations.
  4. A custom annotation that captures some meta data which can be used to read the locator from the DB for a given web element.
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66