microsoft / playwright-pytest

Pytest plugin to write end-to-end browser tests with Playwright.
https://playwright.dev/python/docs/test-runners
Apache License 2.0
423 stars 69 forks source link

[Feature]: Expose output_dir as a fixture #227

Open red8888 opened 3 months ago

red8888 commented 3 months ago

🚀 Feature Request

I could not find how to get a handle to the names playwright generates for its output folders under "test-results". I would think the browser context or something would have a property I could access to get the dynamically generated test-results/<TEST_NAME_SANITIZED>/trace.zip

I want to colocate browser trace json files in the same folder with the playwright trace files. The problem is I can't find docs on how playwright sanitizes and generates the test specific folders- I also don't want to have to duplicate that code myself just so I can do this:

browser_context.start_tracing(
    page=page,
    screenshots=True,
    path=<Path under test-results where the trace.zip will be located>,
)

I don't want to try to generate <Path under test-results where the trace.zip will be located> myself.

How do I control the naming of these folders OR get a handle to their paths from playwright? Seems like this feature does not exist right?

Example

No response

Motivation

I want to colocate browser trace json files in the same folder with the playwright trace files so my test results aren't scattered around everywhere.

mxschmitt commented 3 months ago

which trace.json file do you want to add?

The code is currently not exposed, but we could maybe expose it, so its something like we do upstream in Node.js - like output_dir:

https://github.com/microsoft/playwright-pytest/blob/6d624fc42d58a0f4b12bef4591e1e99ec902ed8b/pytest_playwright/pytest_playwright.py#L183-L191

You could probably import the function from your test as a workaround for now.

red8888 commented 3 months ago

@mxschmitt I just want to add my browser traces I enable with cdp. It would be great to make full trace output folder path an attribute of the context object.

Can I assume that truncate_file_name is public facing because of the lack of _ lol

from slugify import slugify
from pytest_playwright.pytest_playwright import truncate_file_name

PYTEST_PLAYWRIGHT_DEFAULT_OUTPUT_DIR = "test-results"

folder = f"{PYTEST_PLAYWRIGHT_DEFAULT_OUTPUT_DIR}/{truncate_file_name(slugify(request.node.nodeid))}"
mxschmitt commented 3 months ago

Can I assume that truncate_filename is public facing because of the lack of lol

Its more public by accident. If we were to expose output_dir, then as a fixture. I'd recommend to import _build_artifact_test_folder for now until we expose that feature. If you are interested in creating a PR to expose the fixture, happy to merge.