r/softwaretesting • u/Ashamed_Salamander86 • Jan 21 '26
Test automation: Using one page objects class in another. Is it anti pattern?
Hi all, is it a bad practice to import one page class to another page class and then use elements from both class in a page object method like pageloaded? Is it against page object model pattern. I might expect one or another page based on LD flag 50-50 variant? One of my team mate is doing this and not understanding that page loaded for a page should only deal with that page.
8
Upvotes
12
u/endurbro420 Jan 21 '26
The correct way to do this is to create a base page that can be inherited by the others.
Put those common methods on the base page then when you create a new POM, inherit the base page and then build from there.
5
u/Environmental_Sir356 Jan 21 '26
Maybe it's not very common, but I kinda like the idea of linked page objects, so that when executing a page object function that leads to another page it would return the relevant page object instead of void. This way the tests are fluid and have fewer boilerplate. On the other hand, it may be harder to implement.
2
u/Itchy_Extension6441 Jan 21 '26
It is bad practice and it should be avoided.
If some pages are similar you can use inheritance, and if it's only some specific functionalities that are used through different pages that are otherwise unique you can extract the shared functionality into a separate Element that you import in all pages that use it.
1
u/No-Reaction-9364 Jan 21 '26
I don't really think that is an issue. I think of a POM just like I think of classes. So there would be instances where one POM would be a child of another POM and inherit its methods. Then there might also be POMs that would be imported in. Say I have a navigation bar that always exists on every page. I would typically just import that into all my pages since it always exists and I want my tests to have access to it without explicitly always importing it into the test.
I also had the following type of workflow. I have a device page. I can then create a new device which loads a create device page with selectors for device type. Then finally I get the create page based on the device type, which varies by type. If I have 10 kinds of devices that is 12 different pages. If I architect correctly, my main device page can import the others and I only need a fixture for the main device page for my test and I will have access to all of them. The workflow through the UI would also not allow you to access the nested pages without first starting out at the main device page, so I thought this design felt logical, but maybe opinions differ on this kind of thing.
3
u/Sachy_ Jan 21 '26
I recall reading a post on page object architecture. And in its beginning it mentioned how it's unfortuante that it's called Page object pattern because that gives tendencies to have a 1:1 ratio of UI page to Page Object class.
Because it makes sense to have a class for a complex form and its interactions, and then have such class in its parent object. That way you still have correct encapsulation, and readability - whilst technically you have one "page object" inside another.
1
u/Yogurt8 Jan 21 '26
Page objects as commonly described are already full of anti patterns. This just highlights yet another problem people run into due to tight coupling and not separating out concerns.
1
u/PatienceJust1927 Jan 21 '26
Yes, that’s what step definitions are for.
0
u/[deleted] Jan 22 '26
[deleted] — view deleted comment