pytest-dev / pytest-selenium

Plugin for running Selenium with pytest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scope 'session' and 'module' doesn't work on fixture with browser initialization

DenizGen opened this issue · comments

Hello. I have some troubles with starting test with fixture in conftest.py

If i try scope 'session' or 'module', fixture get error:

ScopeMismatch: You tried to access the function scoped fixture selenium with a session scoped request object, involved factories:
conftest.py:31:  def web_browser(request, selenium)
venv/lib/python3.12/site-packages/pytest_selenium/pytest_selenium.py:199:  def selenium(driver)

my code:

@pytest.fixture(scope='session')
def web_browser(request, selenium):
    browser = selenium

    # Return browser instance to test case:
    yield browser

    # Do teardown (this code will be executed after each test):

    if request.node.rep_call.failed:
        # Make the screen-shot if test failed:
        try:
            browser.execute_script("document.body.bgColor = 'white';")

            # Make screen-shot for local debug:
            browser.save_screenshot('screenshots/' + str(uuid.uuid4()) + '.png')

            # Attach screenshot to Allure report:
            allure.attach(browser.get_screenshot_as_png(),
                          name=request.function.__name__,
                          attachment_type=allure.attachment_type.PNG)

            # For happy debugging:
            print('URL: ', browser.current_url)
            print('Browser logs:')
            for log in browser.get_log('browser'):
                print(log)

        except:
            pass  # just ignore any errors here

How can i use only one session to one module or class like in classic pytest?

Oh, i am sorry, i see duplicate in #176