Question: What is the best Page Object framework for Java? And are there any tools to build Page Objects e.g. scan the page and create a class with all the @FindBy annotations on WebElements etc.?
I don't know. I have not researched any Page Object frameworks. I don't use any, and I don't recommend any.
I know there are plenty of libraries for other languages
And there are bound to be more. I don't know.
I prefer to:
- Write the classes I need by hand.
- As I need them.
- Adding methods and
WebElements as I use them.
- Refactoring the classes as they, and my understanding, and our use of them, evolve.
I don’t even recommend using the Page Factory built in to Selenium WebDriver e.g. @FindBy’s etc. because every time I’ve used it, I have eventually moved away from those annotations and the
PageFactory.initElements to allow more flexible synchronisation and more flexibility in general.
But note the "every time I've used it." Because…
There was a time when I did want a framework to make it easy.
And there was a time where I not only wanted a tool to scan the page and build a class, but I actually thought about building a tool that would scan the page and build the class, and I think I created some prototype code, but I can't find it now.
But that was when I viewed writing the Page Objects as drudge work. Work that added no value to the automating.
In fact, every class we add has a reason. The classes represent a model of my understanding of the application and how we would go about automating it.
I think being aware of that allows me to build cleaner and simpler code.
Automatically building Page Objects would have led to 'large' Page Objects, which would be unmaintainable ["Ah, but I wouldn't have to maintain them because they would be automatically created."]
They would also be hard to use. Imagine code completion on an automatically created Page Object. There would be no grouping of elements into headers and footers, or identified widgets, there would just be a big list of stuff — and would the automatically generated names read well enough to explain what they were for?
I can understand why such a tool or framework would look attractive, but in the longer term, I think it would hamper, rather than improve, productivity.
It might even prevent you from experimenting.
- Element abstractions.
- Physical abstractions (which are analogous to automatically generated abstractions but with full human control).
- Logical abstractions.
- Slow loadable abstractions.
- Navigation abstractions.
And there are more. You would miss out on Actor/Task type approaches like the screenplay pattern.
I have a general aversion to frameworks.
I don't mind libraries, but anything that controls the way I write the code, I try to minimize my exposure to.
Because I've learned to value flexibility.
I know the code is going to evolve, because I know my understanding will evolve. And my understanding is encoded into the abstractions I write.
I know the application is going to evolve. And I want my code to adapt gracefully as the application changes.
I know I'm going to be refactoring my code, to improve the structure and implementation.
I don't want anything that might get in the way of my doing that.
It could be that there is a perfect framework out there for me, and because I'm not looking for it, I'll never find it.
But I haven't needed it.
P.S. Having said all that, if you find one that works well then let me know.