I have a function, contains the page objects
class TemplateListPage:
def __init__(self, page):
self.list_first_row = self.page.locator(".grid-row").first
self.use_btn = self.page.locator(".useTemplate")
And I would like to chain the button with the first row in assertion, in the spec test. Like
expect(TemplateListPage().list_first_row.use_btn).to_have_count(0)
or
TemplateListPage().list_first_row.use_btn.click()
But got an error:
AttributeError: 'Locator' object has no attribute 'use_btn'
Is there any way that I can chain the page object locators in the test?
self.page.locator(".useTemplate")is already locating directly on the wholepage, not onpage.locator(".grid-row").firstspecifically. You can't re-chain that assignment after the fact. Sure, you could use some introspection to make this possible but, eh, I'd suggest just writing normal, readable Python code instead of trying to be too clever.locator(page.locator())or you can.filter()your locators. nevertheless, you can chain locators, becausefirstreturns a locator. However you could chain likefirst_row = ....locator("...").first->btn = locator(....)-->first_row.locator(btn)TemplateListPage().list_first_row.use_btnisn't standard. There's no easy way to do that with locators.TemplateListPage().list_first_row_use_btnis much easier and more standard, but then the relationship is hardcoded. With only 2 locators, there's no way to determine whether this makes sense in your larger use case (I assume you want to reuse.use_btnon many different parent locators).