playwright-pytest icon indicating copy to clipboard operation
playwright-pytest copied to clipboard

Option --output not working with a manually launched context

Open rafutek opened this issue 2 years ago • 21 comments

I am trying to output test results in another directory with the --output option. But it seems to not be working : the default directory (test-results) contains test results at each run, even if I delete the folder.

Using pytest-playwright 0.3.0 .

rafutek avatar May 09 '22 12:05 rafutek

I was trying it out locally and it works for me. How do you specify the command to pytest? Do you have a small repro repository which you can share?

mxschmitt avatar May 10 '22 22:05 mxschmitt

For example : pytest --output test_dir tests/admin/test_group_management.py And sorry, I don't have a repo to share.. Pytest is up to date also (7.1.2)

rafutek avatar May 12 '22 09:05 rafutek

Same here with something like:

$ pytest path/to/some/test_playwright.py --tracing=on --output pytest-tracing

even if adding or changing the tracing with screenshots or video or completely omitting the --output param the results is the same: no folder and no output

mnovait avatar May 19 '22 14:05 mnovait

how does your test_playwright.py look like?

mxschmitt avatar May 19 '22 14:05 mxschmitt

I created a couple of fixtures like that:

@pytest.fixture(scope="session")
def mypage(browser, session_cookies):
    context = browser.new_context(storage_state=session_cookies)
    return context.new_page()

@pytest.fixture(scope="session")
def myfeature(mypage, backend):
    myfeature = MyFeaturePage(
        page= mypage,
    )
    yield myfeature
    myfeature.close()

where MyFeaturePage is only an helper that wraps a page and make easier tests development

the test.py is irrelevant, it just take myfeature and does some action on myfeature.page.something()

looking at the code here I'd say that the params related to the artifacts (video, screenshot, tracing) are set only if the context feature is called, which is not in my case.

does it make sense to you?

mnovait avatar May 19 '22 15:05 mnovait

It's intended that the --output only works with the given context and page fixture. If you manually launch a browser or create a browser context, --output will have no effect.

mxschmitt avatar May 23 '22 17:05 mxschmitt

I also modified browser context with

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    return {
        **browser_context_args,
        "ignore_https_errors": True
    }

Removing this fixtures enables --output option, indeed. Thank you !

rafutek avatar May 24 '22 12:05 rafutek

It's intended that the --output only works with the given context and page fixture. If you manually launch a browser or create a browser context, --output will have no effect.

ok. we could add few documentation lines about it.

in theory it would be possibile to get artifacts even if the called fixture is browser, but it could be tricky because in a browser you can initialize more than one context. @mxschmitt do you see this as a possibile improvement of the plugin?

mnovait avatar May 24 '22 13:05 mnovait

Yeah, I think upstream we also support it.

In what artifacts are you exactly interested in? (video, screenshot, trace)?

mxschmitt avatar May 24 '22 13:05 mxschmitt

I'd say everything is feasible 😄 An idea could be iterate over all the contexts defined in browser and print everything. I could work a bit on this, if it can help

mnovait avatar May 24 '22 13:05 mnovait

I do not understand why --output option is not compatible with a modified browser_context_args fixture. Is it possible to make them compatible ?

rafutek avatar Jun 23 '22 08:06 rafutek

Finally I just used pytest --junit-xml instead of --output, it works like a charm

rafutek avatar Jun 23 '22 12:06 rafutek

Yeh this is quite annoying tbh. It would be nice if it was fixed.

dmeecs avatar Jun 29 '22 13:06 dmeecs

Suggested this fix for it https://github.com/microsoft/playwright-pytest/pull/118

dmeecs avatar Jun 30 '22 11:06 dmeecs

Finally I just used pytest --junit-xml instead of --output, it works like a charm

I'm not sure about what you mean but --output provides a folder where the artifacts should be stored, --junit-xml is for the xunit xml file path.

mnovait avatar Jul 07 '22 20:07 mnovait

Yes, sorry, --output and --junit-xml are completely different things.

Is there any update on this issue ? Modifying context and outputing artifacts should be possible no ?

rafutek avatar Aug 30 '22 08:08 rafutek

Our use case:

I need to define context. context = browser.new_context(ignore_https_errors=True). I have it in conftest.

We are trying to move from Selenium to playwright. Not being able to take screenshot when tests fail is the only step that blocking us from moving(#139). In our Selenium framework, the screenshot is taken when test fails. The screenshot then being added to Allure report. It is very convenient to look at the screenshot on the Allure report page especially for team members who are not that familiar with certain tests. I am currently using pytest-bdd and Allure report. Everything works but this.

soprano8086 avatar Nov 16 '22 01:11 soprano8086

Hi, Is there update or plan or work around for this issue? I have paused the Playwright project for a couple of months because screenshot does not work with custom context.

soprano8086 avatar Jan 11 '23 07:01 soprano8086

Hello! Is there any progress on this issue? When is it planned to release adding screenshots with new_context?

TonyKorolev avatar Jul 17 '23 13:07 TonyKorolev

I stumbled upon this issue but was able to solve it for me by modifying the browser_context_args: you can configure the creation of context by modifying the browser_context_args fixture, either per test (as shown in the docs) or by masking the fixture like here https://github.com/microsoft/playwright-pytest/pull/118#issuecomment-1171080654.

screenshots (--screenshot) and videos (--video) are only recorded when using the provided context fixture, see https://github.com/microsoft/playwright-pytest/blob/main/pytest_playwright/pytest_playwright.py#L227.

Documenting this here as there seem to be some open questions about it.

strfx avatar Aug 31 '23 11:08 strfx

It is worth noting that nothing in the documentation at https://playwright.dev/python/docs/test-runners#cli-arguments even hints that any of the CLI flags are dependent on using the default context fixture, or that the suggestion on the same page for modifying context to session-scope would be incompatible with one or more of those flags.

(ETA: at some point last week I came across the browser context args documentation at https://playwright.dev/python/docs/api/class-browser#browser-new-context, but without any mention or cross-reference of the pytest CLI flags, I was left scratching my head as to how these args would interact with CLI flags, and why I would need to set record-video-dir if I had already set --output at the CLI...)

Worse, the --output flag DELETES the provided directory when (because of custom context) no artifacts are stored there. That behavior is also undocumented.

APCBoston avatar Sep 27 '23 00:09 APCBoston