Replacement of @Findby in pagefactory for dynamic value with locator



  • I like to use POM pattern with page factory and in fact I have been using it since long in my various automation frameworks.

    Some time I am facing issue where I have to write text based xpaths in some projects and then when text changes, my xpath fails.

    For example : currently my xpath is //div[text()='Welcome'], Now when in application developer change welcome to something else , my xpath will fail. I can not use id , class etc sometime as most of things are dynamic so I found text based xpath is only reliable option.

    As we can not pass dynamic text with @Findby, Is there any better way to handle such scenarios?

    What I thought is as a solution :

    1. Create one separate class which will return me web elements only and I will pass text on call or from property file in xpath. For example :

    private WebElement loginTextBox(String placeholder) { By.xpath(String.format("//a/h4[text()='%s']", placeholder)); }

    But does this make sense? It will need more time to write methods for each web element. Can this be replacement of @Findby for dynamic values?



  • Do you really want to test that the text Welcome is there? You say the identifiers are dynamic. In these situations, I always suggest to talk to the developers and make it better testable. Preferably developers and testers write and maintain the end-to-end tests together. Let them feel your pain.

    Now when in application developer change welcome to something else , my xpath will fail.

    I would suggest referencing the text from the original source file. I would expect most developers to anticipate the internationalization of an application. Meaning the string texts are probably already separated from the code/view in property files. If not, maybe now is a good time to start doing this.

    texts = ResourceBundle.getBundle("MessagesBundle", currentLocale);
    String msg1 = texts.getString("welcome");
    

    Now if the resource bundle text changes the tests still work. I think you can even remove the magic string "welcome" and make sure you get a compile error when someone removes the text from the code. This would be my ideal situation, the test code won't compile when a developer changes the application, meaning they have to fix it.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2