How to launch one browser for multiple tests in Python/pytest files.



  • I'm just starting to develop into automated testing. Need assistance from more experienced colleagues) I'm writing automobiles for the project, and there are several files of a pyru, each of which is one auto test (completing a certain function). Is it possible, in some way, to initiate and launch a DODIN browser once for all available files? So I could, for example, run tests from one folder and they were all done at one session of the browser? I mean, don't close the browser after every vehicle code file, but start one browser and make sure he just takes the different references in the browser.get(s) code? Thank you very much for the answers. There's a file that created this kind of record:

    import pytest
    from selenium import webdriver
    from m1.config.local_config import *
    import time
    from Credentials.credentials_magento import *
    

    @pytest.fixture(scope='module')
    def browser():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.get(magento_base_url)
    name = driver.find_element_by_id("username")
    name.send_keys(user_name)
    password = driver.find_element_by_id("login")
    password.send_keys(user_password)
    button = driver.find_element_by_class_name("action-login")
    button.click()
    yield driver
    driver.quit()

    UPD: Roman Konoval recommended the code, but the problem is still not solved. I still have a new browser chrome for every file, but now the old one doesn't close. And can a browser be initiated once and for all until every file opens and the tests it needs, it's never initiated again. And now it's like the code described above is still in front of every file (and I have one case code in every file for better readability) opens a new browser and fulfils the code from the record. How do we get this code from the locker to be ODIN once, when any test has been launched, and it's still not done until the browser closes?
    The code now appears as follows:

    import pytest
    from selenium import webdriver
    from m1.config.local_config import *
    import time
    from Credentials.credentials_magento import *

    @pytest.fixture(scope='session')
    def browser():
    with webdriver.Chrome() as driver:
    driver.implicitly_wait(10)
    driver.get(magento_base_url)
    name = driver.find_element_by_id("username")
    name.send_keys(user_name)
    password = driver.find_element_by_id("login")
    password.send_keys(user_password)
    button = driver.find_element_by_class_name("action-login")
    button.click()
    yield driver



  • In order to get what you need, tell me for the record. scope='session' And use it in all the tests.

    In this case, it will be created once before the end of the test run. Which means the browser will be created once.

    Just consider that you have a potential problem in the code now with the closure of the session. If the test ends with a mistake, the session will not be removed (and the browser will not close). What's guaranteed is that everyone closes must be done:

    @pytest.fixture(scope='session')
    def browser():
        with webdriver.Chrome() as driver:
            driver.implicitly_wait(10)
            driver.get(magento_base_url)
            name = driver.find_element_by_id("username")
            name.send_keys(user_name)
            password = driver.find_element_by_id("login")
            password.send_keys(user_password)
            button = driver.find_element_by_class_name("action-login")
            button.click()
            yield driver
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2