playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] How to avoid creating too many trace files?

Open AlexDaniel opened this issue 2 years ago • 0 comments

When my test fails, I'm greeted with this wall of text:

    attachment #1: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace.zip

    ------------------------------------------------------------------------------------------------

    attachment #2: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-1.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-1.zip

    ------------------------------------------------------------------------------------------------

    attachment #3: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-2.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-2.zip

    ------------------------------------------------------------------------------------------------

    attachment #4: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-3.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-3.zip

    ------------------------------------------------------------------------------------------------

    attachment #5: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-4.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-4.zip

    ------------------------------------------------------------------------------------------------

    attachment #6: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-5.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-5.zip

    ------------------------------------------------------------------------------------------------

    attachment #7: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-6.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-6.zip

    ------------------------------------------------------------------------------------------------

    attachment #8: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-7.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-7.zip

    ------------------------------------------------------------------------------------------------

    attachment #9: trace (application/zip) ---------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-8.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-8.zip

    ------------------------------------------------------------------------------------------------

    attachment #10: trace (application/zip) --------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-9.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-9.zip

    ------------------------------------------------------------------------------------------------

    attachment #11: trace (application/zip) --------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-10.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-10.zip

    ------------------------------------------------------------------------------------------------

    attachment #12: trace (application/zip) --------------------------------------------------------
    test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-11.zip
    Usage:

        npx playwright show-trace test-results/backoffice-misc-Miscellaneous-Backoffice-stuff-da01b-nt-test-basics-chromium/trace-11.zip

The reason for that is that I need to make several API requests to prepare the data, and in some cases some additional API requests to test the results after user interaction in the browser, so I end up using request.newContext(…). However, even though it is a single test, following/debugging the result becomes annoyingly hard because requests are spread over 12 trace files.

Yes, in some cases I'm using request.newContext unnecessarily and I can reuse an existing context, but looking at the available APIs that I use during tests, it seems that I will always have about 4 trace files (1 browser + 3 APIs).

Ideally, I just want 1 trace file for any test, with all of the requests. I was looking for a way to clone an existing APIRequestContext (while changing its baseURL in the clone), but I don't see any built-in way. What are my options?

AlexDaniel avatar Aug 04 '22 22:08 AlexDaniel

Hello! I faced the same issue with receiving around 8-10 different trace files.

I was able to chunk it down to two only with this solution.

The first trace file will contain all the api requests that were made. The second trace file will contain the UI interaction.

As you mentioned the “request.newContext” is the issue in this case. What you need to do is to pass the same request through all of your functions. That will store all api calls in the same trace file.

This is how I did it.

Step 1. Remove all request.newContext calls in your functions. Write directly await request.put/post..etc. To make your IDE stop complaining, import and set the type of request according to the picture below. 92FB11F5-EEA5-4EE8-B54B-A464D85D9095

Then just pass the request fixture from your test.

test(“example”, async({request,page}) =>{ Const token= await getLoginToken(request) await anotherAPICall(request) Await page.goto(./) await thirdAPICall(request) })

The same requestContext will be shared by all API calls so it will be stores in the same file :) Issue solved.

oscargforce avatar Aug 21 '22 17:08 oscargforce

The first trace file will contain all the api requests that were made. The second trace file will contain the UI interaction.

Yeah, I'm glad this works for you, but as I mentioned in the original post, I have multiple APIs that have different headers. Doing what you suggested will leave me with 4 trace files, at which point it doesn't really matter if it's 4 or 8, it's too many anyway.

AlexDaniel avatar Aug 21 '22 17:08 AlexDaniel