Rayrun
← Back to Discord Forum

[Question] How to Pass Test Data to onTestEnd Reporter Method in Playwright?

I'm working on a project using Playwright, and I'm looking for some guidance on how to pass test data to the onTestEnd method. Here's a simplified example of my test suite:

for (const record of records) { test(Web Healthcheck - ${record.Application} ${record.Environment}, async ({ page, loginPage, commonUtil }, testInfo) => { await page.goto(record.WebUrl); await loginPage.loginGssApp(record.Username, record.Password); await expect.soft(loginPage.accomodation_img).toBeVisible(); }) }

In this example, I want to pass the record object from each test to the onTestEnd method for reporting purposes. Here's what my onTestEnd method looks like:

onTestEnd(test, result) { // I need to access the 'record' object here for reporting }

Is there a way to achieve this? Any guidance or examples would be greatly appreciated.

This thread is trying to answer question "How can I pass test data to the onTestEnd method in Playwright?"

4 replies

Put it in annotations https://playwright.dev/docs/test-annotations#custom-annotations, I think it does not accept objects so you will need to stringify it. Then access it in onTestEnd from test.annotations.

If needed, remove it from annotations so it does not end up in the html report or elsewhere.

@skorp32 Thanks bro. I have implemented like this. It works well. but i dont want to print the values in html report. how do i prevent it.

Test: for (const uiRecord of uiRecords) { test(Web Health Check - ${uiRecord.Application} - ${uiRecord.Environment}, async ({ page, loginPage}) => { const uiRecordString = JSON.stringify(uiRecord); test.info().annotations.push({ type: 'UIRecord', description: uiRecordString }); }) }

Reporter:

onTestEnd(test: TestCase, testinfo: TestResult): void { const annotations = test.annotations; let uiRecord: any; if (annotations && annotations.length > 0) { const uiRecordAnnotation = annotations.find((annotation) => annotation.description); if (uiRecordAnnotation) { uiRecord = JSON.parse(uiRecordAnnotation.description); } } this.commonUtil.storeHealthCheckResult(uiRecord, resultArr, testinfo.status); }

remove it from test.annotations at the end of onTestEnd function

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