> self.driver.assertTrue(res) E AttributeError: 'TestLogin' object has no attribute 'driver'
时间: 2023-11-08 11:18:44 浏览: 46
这个错误提示表明在你的测试类中缺少 `driver` 属性。`driver` 是一个 WebDriver 实例,用于控制浏览器进行自动化测试。你需要在你的测试类中实例化一个 WebDriver 并将它赋值给 `driver` 属性。例如:
```
from selenium import webdriver
import unittest
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_login(self):
self.driver.get('http://example.com')
# 执行登录操作
res = login('username', 'password')
self.assertTrue(res)
def tearDown(self):
self.driver.quit()
```
在这个例子中,我们使用 Chrome 浏览器作为 WebDriver,实例化一个 ChromeDriver 并将它赋值给 `driver` 属性。在 `test_login` 方法中,我们首先使用 `driver.get` 方法打开了一个例子网站,然后执行登录操作并将结果赋值给 `res` 变量。最后使用 `assertTrue` 方法对结果进行断言。在 `tearDown` 方法中,我们关闭了浏览器。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)