Rayrun
← Back to Discord Forum

Can a test change its own name during the execution (the name that will be listed in the report)

I have a test suite running against my web app that takes as input about a dozen entity ids, runs a test in parallel for each of those ids, and during the execution of those tests it fetches a bunch of info about those entities.

What I'd like do to is use the info that I fetch to enrich the name of the test that will be displayed in the test report, so that insted of "Entity {id}" in the report it says "Entity {id] [FOO] [BAR] [BAZ]"

Is there any way to do that now? If it matters I'm coding in TypeScript.

This thread is trying to answer question "Can a test change its own name during the execution (the name that will be listed in the report)?"

14 replies

Don't store it in title, use https://playwright.dev/docs/test-annotations#custom-annotations

There is the title on testInfo, but it could be read only, idk: https://playwright.dev/docs/api/class-testinfo#test-info-title

I would not recommend doing this. The title is somewhat an identifier for a test. IDE extensions will probably break and not be able to link your test results to tests.

Try to have everything you need to generate the title before calling test().

If this is 'not possible' you could copy and customize only the HTML reporter to manipulate the title.

This helped a lot, thanks. Annotations are useful but unfortunately are not visible on the main report page where tests are listed, and I really need to see 2-3 crucial info points about each entity that is tested (since the content of the entities is often changed, it cannot be retrieved and cached in advance)

Having everything before running tests would mean dozens of API calls made before the tests even start running, so I'm not ecstatic about that. Actually I wouldn't even know how to do it since Request is only available in the context of an execution of a test, right?

testInfo.title = 'new title' will work but as stated in previous comment, it's not recommended

I tried to create a custom report, but I cannot figure out how to customize the title property and use it, any change I make to the title property is not reflected in the report (which makes sense I guess).

Edit: I see the previous comment mentions that this should work, but I'd avoid that anyway since its not a good practice

RE: Customizing the HTML report

I see the source code is here - https://github.com/microsoft/playwright/tree/main/packages/html-reporter - but 1. I cannot build it locally, and 2. even if I could, I don't see how I could configure PW to use it, since the PW https://playwright.dev/docs/test-reporters#custom-reporters API only exposes a few onXYZ callbacks

And PW config for the reporter field seems to accept only built-ins, or custom reporters that only have a few onXYZ callbacks to use

reporter?: LiteralUnion<'list'|'dot'|'line'|'github'|'json'|'junit'|'null'|'html', string> | ReporterDescription[];

you can customize title via custom reporter and it will be reflected in built-in report but in the list of reporters you need to add your custom reporter above html reporter

import type {
  FullConfig, FullResult, Reporter, Suite, TestCase, TestResult
} from '@playwright/test/reporter';

class MyReporter implements Reporter {
  onTestEnd(test: TestCase, result: TestResult) {
    console.log(`Finished test ${test.title}: ${test.annotations}, ${result.status}`);
  }
}

export default MyReporter;```

Something like this? I think this reporter would print a test's `title`, followed by the test's `annotations`, followed by the test's `status` after each test finishes

To clarify, this is roughly what I'd like the end result to look like (configurable labels next to the test title)

If it turns out that I can figure out how to customize the HTML report further, I also plan on adding additional filters at the top for each lable type that allow you to filter by the options that are present in the report for each label (e.g. LABEL1 could be a boolean label type that has a "Show only tests that have this label / Show only tests that do not have this label / Disregard this label" filter, LABEL2 can be a plantext label with a multiselect dropdown "foo, bar, baz" filter)

So regarding customizing the HTML report, has anyone tried modifying the default HTML reporter (https://github.com/microsoft/playwright/tree/main/packages/html-reporter) locally, building it, and somehow configuring PW to use the customized reporter?

you can't really do it just for html reporter, you would need to fork whole playwright repo and build runner, html reporter and everything else yourself

Thanks, thats what I started to suspect. I'll take a look at one of the listed examples of full-fledged 3rd party reporters and see how much time I'd need to build a new one.

In the meantime for a pitch to my org I'll use https://www.npmjs.com/package/patch-package to patch the default HTML reporter in node_modules and add a few lines of plain JS code for what I need to sell 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].