Rayrun
← Back to Discord Forum

Extending assertion methods.

I have made additional layers on top of playwright to adapt to some needs at my company.

One of these is to define some DOM components as classes in order to quickly pick up/understand the responsibility.

Example

class TextField {
  
  constructor(selector: string, page: Page){
    this.selector = selector;
    this.page = page;
    this.locator = page.locator(selector);
  }
  
  async fill(value: string){
    // some things here
  }
}

class DropDown {
   // same ctor as above
  async pick(value: string){
    // Code to pick a value from a list
  }
}

All these classes extend the Component class, which has some QoL methods that are insignificant here.

What I want to do in my tests, is to be able to extend the expect assertions to accept Component as type, and define the behavior accordingly.

How would I go on about doing that ?

This thread is trying to answer question "How can I extend the expect assertions to accept Component as type and define the behavior accordingly in my tests?"

1 reply
refactoreric

Hi, from a quick glance over the docs it looks like this might work: https://playwright.dev/docs/test-configuration#add-custom-matchers-using-expectextend

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