Closed JasKirk70 closed 10 months ago
We recommend taking the resources/artifacts in the afterAll hooks and attaching them but doing the actual reporting as a custom reporter.
Have you tried that approach?
Is there any way to transfer data from the afterAll hooks to the custom reporter methods apart from writing content to a text file and attaching it. I need this as I get a unique id generated in afterAll hook through an api call, that I need to reuse in the onTestEnd method
You can annotate tests and read the annotations on a test level.
in the test.afterEach method I added below code
test.info().annotations.push({
type: 'bug',
description: id,
});
and trying to read in custom reporter class in onTestEnd method as
test.annotations.forEach(annotation => {
const { type, description } = annotation;
console.log(Type: ${type}
);
if (description) {
console.log(Description: ${description}
);
} else {
console.log('No description available');
}
});
but nothing is getting logged.
I tried the following and it works for me:
// reporter.ts
import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
class MyReporter implements Reporter {
onTestEnd(test: TestCase, result: TestResult) {
console.log(`Finished test ${test.title}: ${result.status}`);
for (const annotation of test.annotations) {
const { type, description } = annotation;
console.log(`Type: ${type}`);
if (description) {
console.log(`Description: ${description}`);
} else {
console.log('No description available');
}
}
}
}
export default MyReporter;
// mytest.spec.ts
import { test, expect } from '@playwright/test';
test.afterEach(async ({ }) => {
test.info().annotations.push({
description: "hi",
type: 'comment',
})
})
test('has title', async ({ }) => {
});
What am I doing different?
Closing per the response above, feel free to open a new issue if it doesn't work.
Use case
I have to create a bug using some bug apis on test case failure which I am doing in test.afterAll method. For this I need the screenshots and video as well. Now this is easily available in reporters, but I want to access these screenshot and video to send in my bug api in test.afterAll method. is it possible?
I am doing below code in test.afterEach method but not sure if I can access these attachments through testinfo.attachments in test.afterAll method const screenshot = await page.screenshot(); await testInfo.attach('screenshot', { body: screenshot, contentType: 'image/png'});