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
444 stars 69 forks source link

CLI arguments #259

Closed Nevimzase closed 1 week ago

Nevimzase commented 2 weeks ago

Hi, I am begginer in Python-Pytest and learning CLI arguments. I have Python 3.12, Pytest 8.3.3 and Playwright 1.48 and none of these arguments work for me

--headed: Run tests in headed mode (default: headless). --browser: Run tests in a different browser chromium, firefox, or webkit. It can be specified multiple times (default: chromium). --browser-channel Browser channel to be used. --slowmo Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on (default: 0). --device Device to be emulated. --output Directory for artifacts produced by tests (default: test-results). --tracing Whether to record a trace for each test. on, off, or retain-on-failure (default: off). --video Whether to record video for each test. on, off, or retain-on-failure (default: off). --screenshot Whether to automatically capture a screenshot after each test. on, off, or only-on-failure (default: off). --full-page-screenshot Whether to take a full page screenshot on failure. By default, only the viewport is captured. Requires --screenshot to be enabled (default: off).

https://playwright.dev/python/docs/test-runners

I don´t know why. It does not work for any tests - working, failing, simply do not react. For exemple --device does not emulate devices, --video and --screenshot do not create file with video and screenshot records, --output does not create wished directory, etc.

What is the reason and solution?

mxschmitt commented 2 weeks ago

Have you installed pytest-playwright? What is the output of pip list? I recommend following this guide: https://playwright.dev/python/docs/intro

Nevimzase commented 2 weeks ago

More details: If I hit in terminal for exemple this: pytest -k test_exemple.py --screenshot=on --video=retain-on-failure it runs all tests and does not make any screenshots nor video and it even does not create new folder for these. I tried also test name without .py. And have no idea where the trouble is.

Thank you.

Nevimzase commented 2 weeks ago

Have you installed pytest-playwright? What is the output of pip list? I recommend following this guide: https://playwright.dev/python/docs/intro

Yes, pytest-playwright is installed pytest-playwright 0.5.2m. result of pip list is: Package Version

pip 24.2

mxschmitt commented 2 weeks ago

Maybe you have it installed in different virtual environments? We have not heard of issues around this. e.g. try using virtualenv and do pip uninstall pytest pytest-playwright before.

Nevimzase commented 2 weeks ago

Maybe you have it installed in different virtual environments? We have not heard of issues around this. e.g. try using virtualenv and do pip uninstall pytest pytest-playwright before.

I don´t think I installed it in different virtual environment. I have been taking an online course on Udemy and did same steps as the lector. Did not installed anything extra. Only the same packages. I don´t see any package for environments in my Interpreter.

Still thank you for your help.

I will try to re-install pytest and pytest-playwright and see if it helps.

Nevimzase commented 2 weeks ago

well, re-installing did not help

mxschmitt commented 2 weeks ago

Do you mind sharing more information on how you reproduce it?

Whats the output of pytest?

Whats the output of pip list?

Whats your OS?

How did you install Python?

Happy to help but its hard without these kind of information. Also what happens if you follow the virtualenv instructions from above and install pytest-playwright via that?

Nevimzase commented 1 week ago

Do you mind sharing more information on how you reproduce it?

Whats the output of pytest?

Whats the output of pip list?

Whats your OS?

How did you install Python?

Happy to help but its hard without these kind of information. Also what happens if you follow the virtualenv instructions from above and install pytest-playwright via that?

Hi, before I was about to go your way I wanted to check once more everything so that I can say honestly I did everything I could to find out the reason by myself and just before I found something that I saw before but it did not occured to me this might be it:

"Note that CLI arguments are only applied to the default browser, context and page fixtures. If you create a browser, a context or a page with the API call like browser.new_context(), the CLI arguments are not applied." https://playwright.dev/python/docs/test-runners

And in my test there is:

def test_about_us_section_verbiage(playwright: Playwright):
    browser = playwright.chromium.launch()
    #browser = playwright.chromium.launch(headless=False)
    **page = browser.new_page()**

OR

def test_TOP_09(playwright: Playwright) -> None:
    browser = playwright.chromium.launch()
    #browser = playwright.chromium.launch(headless=False)
    **context = browser.new_context()**

So I guess I found it, right? :D

And why it did not work with test_exemple.py? I don´t know. Maybe some typo? I tried it now and it works. It creates folder with video and screenshot and other CLI arguments work.

However I got confused because in the video of the course, the lector works with these lines context = browser.new_context()

    page = context.new_page()   and it works to him. **Why?**

If you agree this is it, I thank you for your patience and time. If you say that it something else, I will send you the asked info.

Actually one more question about Playwright Inspector - when I try to create new test. It opens always with:

_import re
from playwright.sync_api import Playwright, sync_playwright, expect

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()

    # ---------------------
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)_

And this prevents me to use CLI commands in future. How can be     
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()

replaced in order to make CLI commands working?

mxschmitt commented 1 week ago

So I guess I found it, right?

yes

Actually one more question about Playwright Inspector - when I try to create new test. It opens always with:

We can probably change the default to Pytest in Python: playwright codegen --target=python-pytest

Nevimzase commented 1 week ago

We can probably change the default to Pytest in Python: `playwright codegen --target=python-pytest

Oh yes! It helped! Thank you so much!

(I met another problem with pytest command but was able to solve it. I don´t know whyt I had opened two terminals and something caused god know why mess)