[英]AttributeError: 'TestTomsLogin' object has no attribute 'driver'
conftest.py file conftest.py 文件
import pytest
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture(scope="class")
def setup(request):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://itera-qa.azurewebsites.net/Login")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
request.cls.driver = driver
request.cls.driver = wait
yield
driver.close()
loginpage.py file登录页面.py 文件
from selenium.webdriver.common.by import By
class TomsLogin:
def __init__(self, driver, wait):
self.driver = driver
self.wait = wait
def toms_login(self, usern, pwd):
self.driver.find_element(By.ID, "Username").send_keys(usern)
self.driver.find_element(By.ID, "Password").send_keys(pwd)
test_toms_login.py file -- Getting error while calling this file test_toms_login.py 文件——调用此文件时出错
import pytest
from base.loginpage import TomsLogin
@pytest.mark.usefixtures("setup")
class TestTomsLogin():
def test_login(self):
self.tl = TomsLogin(self.driver, self.wait)
self.tl.toms_login("User1", "pwd1")
ttl = TestTomsLogin()
ttl.test_login()
Error Details:错误详情:
collecting ...
test_toms_login.py:None (test_toms_login.py)
test_toms_login.py:11: in <module>
ttl.test_login()
test_toms_login.py:7: in test_login
self.tl = TomsLogin(self.driver, self.wait)
E AttributeError: 'TestTomsLogin' object has no attribute 'driver'
I would change the structure of you code to something like this:我会将您的代码结构更改为以下内容:
And make sure driver is actually installing (you can use hard coded path once to see the problem)并确保驱动程序正在实际安装(您可以使用硬编码路径一次来查看问题)
@pytest.fixture(scope="class")
def driver(request):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://itera-qa.azurewebsites.net/Login")
driver.maximize_window()
yield driver
driver.quit()
@pytest.fixture(scope="class")
def setup(request, driver):
# put your wait here too
request.cls.driver = driver
ttl = TestTomsLogin()
ttl.test_login()
The Issue got resolved by removing the above two lines.通过删除上述两行,问题得到解决。 It should not be used when using pytest methods.
使用 pytest 方法时不应使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.