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

瘦欲@ 提交于 2019-12-01 05:44:26

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!