属性错误:对象没有属性“driver”

问题描述 投票:0回答:1

我正在尝试为一个简单的测试设置测试框架,以计算 google.com 上某个单词的搜索结果。这是我的框架的样子:

repo
 |-pages
 |   |-google_launch_page.py
 |   |-google_search_results.py
 |
 |-tests
 |   |-conftest.py
 |   |-test_google.py
 |-conftest.py

这是

test_google.py

from selenium.webdriver.common.by import By
from pages.google_launch_page import LaunchPage
from pages.google_search_results_page import SearchResultPage
import time, re, pytest

@pytest.mark.usefixtures("setup")
class testgooglesite():
    def test_search_results(self):
        lp = LaunchPage(self.driver)
        lp.clickSearch("Chocolate")
        # test 1: there should be non-zero results
        result_element = self.driver.find_element(By.XPATH, "//div[@id='result-stats']")
        result_text = result_element.text
        numbers = re.findall(r'\d{1,3}(?:\s?\d{3})*', result_text)[0]
        result_count = int(''.join(numbers[0]))
        self.driver.close()
        assert result_count>0,"No results"

myobj = testgooglesite()
myobj.test_search_results()

这是

tests/conftest.py
repo/conftest.py
为空):

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pytest

@pytest.fixture(scope="class")
def setup(request):
    service = Service()
    options = webdriver.ChromeOptions()

    driver = webdriver.Chrome(service=service, options=options)
    driver.get('https://google.com/')
    webdriver.wait(driver,5)
    driver.maximize_window()
    request.cls.driver = driver

最后,这是错误消息:

tests\test_google.py:16: in test_search_results
    lp = LaunchPage(self.driver)
E   AttributeError: 'testgooglesite' object has no attribute 'driver'

我假设 pytest 可能会忽略

conftest.py
但我无法找出原因。有人可以帮忙吗?

根据评论中的一项建议,我尝试了以下方法

conftest.py

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pytest

@pytest.fixture(scope="class", autouse=True)
def driver(request):
    service = Service()
    options = webdriver.ChromeOptions()

    driver = webdriver.Chrome(service=service, options=options)
    driver.get('https://google.com/')
    webdriver.wait(driver,5)
    driver.maximize_window() 
    request.cls.driver = driver
    yield driver

测试文件:

from selenium.webdriver.common.by import By
from pages.google_launch_page import LaunchPage
from pages.google_search_results_page import SearchResultPage
import time, re, pytest

# @pytest.mark.usefixtures("driver")
class testgooglesite():
    def test_search_results(self):
        lp = LaunchPage(driver)
        lp.clickSearch("Chocolate")
        # test 1: there should be non-zero results
        # find the corresponding element
        result_element = driver.find_element(By.XPATH, "//div[@id='result-stats']")
        result_text = result_element.text
        print(result_text)
        numbers = re.findall(r'\d{1,3}(?:\s?\d{3})*', result_text)[0]
        result_count = int(''.join(numbers[0]))
        driver.close()
        assert result_count>0,"No results"

myobj = testgooglesite()
myobj.test_search_results()

在这里,我尝试通过产生驱动程序并修改夹具来替换夹具以执行

autouse=True
。但这给了我一个错误,
E   NameError: name 'driver' is not defined

python selenium-webdriver selenium-chromedriver pytest conftest
1个回答
0
投票

感谢@MrBean Bremen,我在代码中发现了错误。它只是使用小写的类名。应该是

class TestGoogleSite
而不是通常的
class testgooglesite
。每当类缺少
Test
方法时,就有必要从
__init__()
开始(请阅读 here)。

以下代码有效:

test_google.py

from selenium.webdriver.common.by import By
from pages.google_launch_page import LaunchPage
from pages.google_search_results_page import SearchResultPage
import time, re, pytest

@pytest.mark.usefixtures("setup")
class TestGoogleSite():
    def test_search_results(self):
        lp = LaunchPage(self.driver)
        lp.clickSearch("Chocolate")
        # test 1: there should be non-zero results
        result_element = self.driver.find_element(By.XPATH, "//div[@id='result-stats']")
        result_text = result_element.text
        numbers = re.findall(r'\d{1,3}(?:\s?\d{3})*', result_text)[0]
        result_count = int(''.join(numbers[0]))
        self.driver.close()
        assert result_count>0,"No results"

conftest.py

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pytest

@pytest.fixture(scope="class")
def setup(request):
    service = Service()
    options = webdriver.ChromeOptions()

    driver = webdriver.Chrome(service=service, options=options)
    driver.get('https://google.com/')
    webdriver.wait(driver,5)
    driver.maximize_window()
    request.cls.driver = driver
© www.soinside.com 2019 - 2024. All rights reserved.