microsoft/playwright-pytest

[Feature]: Expose output_dir as a fixture

red8888 opened this issue ยท 3 comments

๐Ÿš€ 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.

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:

def _build_artifact_test_folder(
pytestconfig: Any, request: pytest.FixtureRequest, folder_or_file_name: str
) -> str:
output_dir = pytestconfig.getoption("--output")
return os.path.join(
output_dir,
truncate_file_name(slugify(request.node.nodeid)),
truncate_file_name(folder_or_file_name),
)

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

@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))}"

Can I assume that truncate_file_name 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.