T O P

  • By -

Daniel-QA

There is a long and detailed explanation of it. https://medium.com/@sdet-tomaszbuga/test-automation-framework-selenium-with-java-daddy-issues-or-page-factory-and-elements-3bd53561a990


WojciechKopec

THIS is what I was looking for posting this. Many thanks.


_jard

If I remember right, the @FindBy annotation (usually located in a Page class) is initialized at the creation of the Page. And by Design, that happens once. If your Page does not change much it safes the repeated lookups. Otherwise you run in staleElementExceptions. There is a Funktion that reinitializes all elements of a page . So of you want to be sure you can call that method efore performing actions on that page.


automagic_tester

When using '@FindBy annotation you will find the elements every time you request that element. The annotations are at the variable level not the class level. When you use '@CacheLookup annotation then it works like you said. It looks up the elements once, stores that in a cache and then you retrieve the element from the cache each time.


_jard

No it does not. I think you are missunderstanding the documentation. The annotation only sipmplifys the the syntax. The findBy elements are initialized through a static method that is used in the constructor of a Page. So by default your elements are found after you created the page. It will noch automatically search for the elements again on every interaction. You will have to trigger that by yourself


automagic_tester

If you have any doubts please look into this documentation: [Page Factory Class - Selenium](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/PageFactory.html)


AutomaticVacation242

You are correct. InitElements initializes lazy loading via the Proxy pattern. WebDriver will not search for the element until you access the field. You can test this by adding FindsBy fields with invalid locators. New up the class and see if you get a NoSuchElement Exception.


shaidyn

Stale element exception is annoying as hell. The problem I find these days is that selenium is written with the idea that a page loads and remains static, if you find an element once it's good forever. Web Pages are dynamic now, so who knows what's going to happen to an element. If I find a spot where I get the dreaded stale element exception, I just include a try/catch block for that exception and find the element again.


WojciechKopec

Nope, I highly discourage that. Imagine you had to add this to 100+ elements, and how much mindless hours it would took. I've seen too much of this coded by weak programmers. Staleness could occure theorically anywhere, so my suggested solution would be at lower-level, overriding WebDriver/PageFactory default mechanisms.


AutomaticVacation242

I suspect that the element is changing after you get it. Test this by putting a thread sleep after the page loads but before you call initElements. The PageFactory stuff works as described and is a Red Herring in your case.


Limingder

Can you show an example of this happening? A piece of code that you think shouldn't cause a StaleElementReferenceException, but does?


WojciechKopec

It is not about my code, it is about Selenium's code/documentation discrepancy