Rayrun
← Back to Discord Forum

Is it possible to chain page object locators in assertion?

bandito9274posted in #help-playwright
Open in Discord

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?"

5 replies

Or the only way is to create a brand new page object with the chained locators? self.list_first_row_use_btn_chained = self.page.locator(".grid-row").first.locator(".useTemplate") Why I needed to create a new PO just for using it for expect?

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

I'm opened this thread about: python and assertion

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)

Thank, unfortunately, the first solution doesn't work playwright._impl._api_types.Error: Unexpected token "<" while parsing selector Because we already described use_btn as a locator in the class, so you cannot call it like .locator(TemplateListPage().use_btn)

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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].