python selenium unittest框架实战代码
时间: 2023-09-10 10:15:50 浏览: 23
以下是一个简单的 Python Selenium unittest 框架实战代码示例:
```python
import unittest
from selenium import webdriver
class TestGoogleSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(10)
def test_search_in_google(self):
self.driver.get("https://www.google.com")
self.assertIn("Google", self.driver.title)
search_box = self.driver.find_element_by_name("q")
search_box.send_keys("Selenium")
search_box.submit()
results = self.driver.find_elements_by_css_selector("div.g")
self.assertTrue(len(results) > 0)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
```
这个测试用例使用了 unittest 框架,首先在 setUp 方法中初始化了 Chrome WebDriver,然后在 test_search_in_google 方法中执行了一些简单的测试步骤来搜索关键字“Selenium”,最后在 tearDown 方法中关闭了 WebDriver。如果所有测试用例都通过了,最后的结果应该是 OK。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)