Skip to content

[πŸ› Bug]: Driver is not recognized in test file . Passed through request.cls.driver from conftest.pyΒ #15349

@Rajikrish2211

Description

@Rajikrish2211

What happened?

Hello All,

I'm working on simple pytest framework and tried to pass we driver from Conftest to Test file but Driver is not recognized . I'm getting "TestSearch{class name} ' object has no attribute 'driver'.

Pasting the code here. Kindly help me

How can we reproduce the issue?

**Conftest.py**
import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

@pytest.fixture(scope="class")
def setup_and_teardown(request):
    driver = webdriver.Chrome()
    driver.get("https://www.redbus.in/")
    driver.maximize_window()
    driver.implicitly_wait(5)
    request.cls.driver = driver
    yield
    driver.close()

**searchbus_test.py**
import time
import pytest
from selenium.webdriver.common.by import By
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

@pytest.mark.usefixtures("setup_and_teardown")
class TestSearch:
    def test_search_bus(self):
        # entering source location
        ele = self.driver.find_element(By.XPATH, "//input[@id='src']")
        ele.send_keys("coim")
        res = self.driver.find_elements(By.XPATH, "//ul[@class='sc-dnqmqq dZhbJF']/li")
        print(len(res))
        for x in res:
            if "Gandhipuram" in x.text:
                x.click()
                # time.sleep(5)
                break

        #entering destination location
        ele2 = self.driver.find_element(By.XPATH, "//input[@id='dest']")
        ele2.send_keys("pal")
        # time.sleep(5)
        res2 = self.driver.find_elements(By.XPATH, "//ul[@class='sc-dnqmqq dZhbJF']/li")
        print(len(res2))
        for x in res2:
            if "Palakkad Bypass" in x.text:
                x.click()
                # time.sleep(5)
                break
                # time.sleep(5)

        #choosing the date
        self.driver.find_element(By.XPATH, "//div[@id='onwardCal']").click()
        self.driver.find_element(By.XPATH, "//div[@class='labelCalendarContainer']").click()
        self.driver.find_element(By.XPATH, "//span[normalize-space()='28']").click()

        #clicking the search button
        self.driver.find_element(By.XPATH, "//button[@id='search_button']").click()
        time.sleep(5)
        #filter before 6am
        self.driver.find_element(By.XPATH, "//ul[@class='dept-time dt-time-filter']//li[1]//label[1]").click()
        time.sleep(5)

    # time.sleep(5)


obj = TestSearch()
obj.test_search_bus()

Relevant log output

AttributeError: 'TestSearch' object has no attribute 'driver'

Operating System

Windows 10

Selenium version

Selenium 4.22.0

What are the browser(s) and version(s) where you see this issue?

Chrome

What are the browser driver(s) and version(s) where you see this issue?

Chrome driver 133.0.6943.127

Are you using Selenium Grid?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions