Rayrun
← Back to Discord Forum

Custom matcher with page.evaluate

I want to get a value using page.evaluate (value is stored as a global variable on window) and do a custom matcher. Simply put it should replace something like:

const ret = await page.evalute((name) => window.myVar.name)
expect(name).toEqual(1);
// so I would like a custom expect like:
expect(name).toHaveValue(num)

Any useful tip? specifically - how can I get access to page on a custom matcher?

This thread is trying to answer question "How can I get access to page on a custom matcher in JavaScript?"

3 replies

Don’t have an experience with custom matchers but perhaps it would be easier to make something like expect(page).toHaveValue(name, num)? Otherwise I don’t think you can access page without passing it to custom expect.

Hmmm, this might be tricky, my understanding javascript wont allow you to do reflection as you might in C#. You can't directly reference a variable like this. While i'm thinking you can have a method on your webpage that will work as a proxy, that method would reference a dictionary you've created for this purpose, pass in the key/variable name and you mapping/proxy function will return the value back... Then using PW's evaluate you should be able to get the value...

I have no trouble getting the value from the browser. It is only in the expect that I cannot reference it.

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