How to use chained page object locators?

112 views Asked by At

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?

1

There are 1 answers

3
Rob C On BEST ANSWER

Playwright recently added the and_ locator, which should allow you to achieve what your aiming for:

expect(TemplateListPage().use_btn.and_(TemplateListPage().list_first_row)).to_have_count(0)