Hi, I have page object defined in class, like
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, like
expect(TemplateListPage().list_first_row.use_btn).to_have_count(0)
But got an error:
AttributeError: 'Locator' object has no attribute 'use_btn'
Is there any way that I can chain the page object locators?
This thread is trying to answer question "Is it possible to chain page object locators in assertion?"
I didn't know this, but apparently you can do this:
const saveButton = page.getByRole('button', { name: 'Save' });
// ...
const dialog = page.getByTestId('settings-dialog');
await dialog.locator(saveButton).click();
https://playwright.dev/docs/locators#matching-inside-a-locator
Hi, you can combine Locator objects with Locator.locator https://playwright.dev/python/docs/api/class-locator#locator-locator
Something like ... expect(TemplateListPage().list_first_row.locator(TemplateListPage().use_btn)).to_have_count(0)
... should work although I'm not a Python expert.
What I typically do with lists though is to create a page (component) object for the list and a page (component) object for individual list items.
So you could write something like
expect(TemplateListPage().rows(1).use_btn).to_have_count(0)
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].