I'm running Playwright tests using the pytest framework in Python, with parallel execution enabled via pytest-xdist.
I've been exploring Playwright's tracing feature, but I'm running into a challenge:
Function-level tracing is not ideal due to parallel execution.
Class-level tracing works, but it consumes a large amount of disk space.
What I want is an efficient approach to capture traces only for failed test cases and skip tracing for passed ones to save space and processing time.
Has anyone implemented or come across a way to enable tracing only for failed tests when using Playwright with pytest and parallel runs?
Any suggestions or examples would be greatly appreciated!
The main idea is that you wanna start tracing after your setup and just before your scenario starts running, and then stop & save the tracing after your scenario finishes running. So, to implement so that you save the tracing only for failed scenarios, you can check if the result of the test is failure and save only for that condition. Here's snippet of how I implemented in my test suite: (Feel free to rewrite it in python as I had implemented in typescript with cucumber+playwright framework).
I have a hooks file where I do the setup. There, I have BeforeAll(), Before(), After() and AfterAll() from cucumber. In Before(), I start the tracing:
await context.tracing.start({
name: SCENARIO_NAME,
title: SCENARIO_NAME,
sources: true,
screenshots: true,
snapshots: true});
Here, context is the browsercontext that you wanna initialize beforehand.
Then, in After(), you can stop only for failed scenario based on the result you get from cucumber run in this way:
After(async function({result}){
...
if(result?.status === Status.FAILED){
await context.tracing.stop({path: LOCATION_TO_BE_SAVED});
}
});
Even though this is probably not the best way to implement the solution, it works for me for now. I may check again to improve it later on.