← Back to Discord Forum

Is it possible to return element from POM and make actions in tests instead of actions in POM

Hi,all. I am totally new to Playwright and came here from Cypress. I am used to create selectors for elements in Page Object and then use them in Test. In cypress we do smth like function getElement(){return cy.get(selector) }. And after I can call this function in test and make action. Smth like this: PO.getElement().click(). Is it possible to do in Playwright? I don`t like approach what I see on youtube where guys use actions in PO. Thank you in advance for help!

This thread is trying to answer question "Is it possible to return an element from POM and perform actions in tests instead of in POM in Playwright?"

1 reply

Do you mean something like Page Object Model? https://playwright.dev/docs/next/pom


Sure, you can return an element from the Page Object Model (POM) and perform actions in your tests in Playwright. This gives you more control over your tests and allows for easier customization based on specific scenarios.

Here's how you do it:

First, create a locator using the Locators API to find elements on a page. Once you've located an element, return it from your POM method to your test code.

For instance, you might have a POM method called getSubmitButton that returns the submit button element on a form. You can then call this method in your test code and perform actions on the returned element.

// POM method
public getSubmitButton(): Locator {
  return page.locator('button[type="submit"]');

// Test code
const submitButton = await myPage.getSubmitButton();
await submitButton.click();

This approach gives you flexibility, but remember that any changes to the element's locators or structure will require updates in your test code. So, think carefully about whether to return elements from the POM or perform actions directly within it.

In a nutshell, Playwright lets you return elements from the POM and perform actions in tests, giving you more control over your tests. But, you need to manage your element locators and structure carefully.

Related Discord Threads

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 luc@ray.run.