Why should I be making my page objects instantiated rather than static? Why should I be making my page objects instantiated rather than static? selenium selenium

Why should I be making my page objects instantiated rather than static?


I started to work with Selenium and Page Objects 2 months ago. And I'm also curious about the topic. I decided to go with classes and static methods about 1 month ago. But as a codebase grew up I'm started to think about switching to object instances. Main reason - objects can have state. In object I can check if I'm really working with the correct page. With classes I can only assume that current html matches the class (or bloat my code with asserts in every static method of page object). The other reason - autocomplete. Static methods encourage testers to use classes, not the class variables. So it's getting harder and harder to find correct class to call method from. With objects you'll have to declare a variable if you want to call any method. So you're limited with what you can call. With classes and static methods, you can call any method any time (and fail the test cause the expected html is not available eg). It starts to grow into a problem when you try to teach your team members to use your code. As long as you are the only test writer, it might be ok.


If you are not creating the instance methods in your page object class, you can not pass the driver life to your page object class from your test classes (by passing driver to constructor while creating object)


As far as I can tell, using static all over the place gets rid of most of the benefits of object-oriented programming. For instance, inheritance and the ability to have multiple objects of the same class now won't work very well. But if it works the way you're doing it, I don't think it should be a problem.