'Pytest in multiple browsers

I am trying to run a py test on multiple browsers. However, i keep getting an error saying that my init_driver is not found.

The code in my browser.py

from selenium import webdriver

from selenium.webdriver.chrome.service import Service
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import (
    TimeoutException,
    WebDriverException,
    NoSuchElementException)
import pytest

@pytest.fixture(params=["firefox","chrome","edge"],scope='function')
def init_driver(request):
    if request.param == "firefox":
        firefox_options = webdriver.FirefoxOptions()
        firefox_options.headless = True
        web_driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()),options=firefox_options)
    if request.param == "chrome":
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument('--disable-gpu') 
        web_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=chrome_options)
    if request.param == "edge":
        edge_options = webdriver.EdgeOptions()
        edge_options.use_chromium = True
        edge_options.add_argument("--headless")
        web_driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()),options=edge_options)
   
    request.cls.driver = web_driver
    yield
    #web_driver.close()
    #web_driver.quit()

def get_url(url):
    try:
        init_driver.get(url)
        response = True
    except WebDriverException as exc:
        response = False
    return response

def find_input_field(path,timeout=5):
    try:
        WebDriverWait(init_driver, timeout).until(
            EC.presence_of_element_located((
                By.XPATH, path)))
        response = True

    except TimeoutException:
        response = False
    return response

def find_then_type(path, text):
    try:
        element = init_driver.find_element(By.XPATH,path)

        element.send_keys(text)

        response = True

    except NoSuchElementException as exc:
        
        response = False

    return response

The code for test_run.py

from baseline.browser import browser
import pytest
from ..config import (url,user_field)

@pytest.mark.usefixtures("init_driver")
def test_login(init_driver):
    assert browser.get_url(url),\
        "failed to navigate to {}".format(url)
    print ("successfuly access {}".format(url))

def test_css():
    assert browser.find_input_field(user_field),\
        "failed to navigate to {}".format(user_field)
    print ("found input field")

I tried to run in google chrome browser at first and it successfully shown that the pytest run successfully. However , i want to try to run in multiple browsers such edge, firefox and chrome at the same time. When i created a function for it , it keep saying that the init_driver is not found.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source