sleep(4) result = driver.find_element('link text', '111121').text self.assertEqual(result, '011111')
时间: 2023-06-11 13:09:47 浏览: 42
这段代码是使用 Python 的 unittest 模块编写的一个测试用例。其中 sleep(4) 是让程序暂停 4 秒,等待页面加载完成。接着使用 Selenium 的 driver.find_element() 方法找到页面中链接文本为 '111121' 的元素,并获取它的文本内容。最后使用 unittest 的 self.assertEqual() 方法来断言获取到的文本内容是否与期望的值 '011111' 相等。这个测试用例的目的是测试页面中链接文本为 '111121' 的元素是否正确显示为 '011111'。
相关问题
import unittest from time import sleep from selenium import webdriver class Test_regist_1(unittest.TestCase): def setUp(self) -> None: pass def tearDown(self) -> None: pass def test_regist_seccuse(self): driver = webdriver.Chrome() driver.maximize_window() driver.get('http://localhost:8090/dsmall/public/index.php/home/login/register.html') driver.find_element('id', 'member_name').send_keys('qqqq') driver.find_element('id', 'member_password').send_keys('111121') driver.find_element('id', 'member_password_confirm').send_keys('011111') driver.find_element('class name', 'login-btn').click() sleep(4) result = driver.find_element('link text', '111121').text self.assertEqual(result, '011111') if __name__ == '__main__': unittest.main()
这是一个基于unittest框架的Selenium测试用例,用于测试注册功能是否正常。测试用例中打开Chrome浏览器,最大化窗口,访问注册页面,输入用户名、密码和确认密码,点击注册按钮,等待4秒(用于页面加载完成),然后获取页面上显示的密码,并将其与确认密码进行比较,如果相同则测试通过,否则测试失败。
需要注意的是,这个测试用例只是一个示例,具体的测试用例需要根据实际情况进行编写。同时,也需要保证测试环境的稳定性和可靠性,以避免测试结果的误判。
import csv import unittest from time import sleep from ddt import ddt,data,unpack from selenium.webdriver.chrome import webdriver from selenium.webdriver.common.by import By def add(a,b): return a+b def getData(file_name): rows=[] file_data=open(file_name,"r",encoding='utf8') reader=csv.reader(file_data) next(reader) for row in reader: rows.append(row) return rows def login(driver,username,pwd): driver.get("https://www.baidu.com") # 3. 在谷歌浏览器中打开百度链接sleep(0.5) driver.find_element(By.ID, "kw").send_keys("学习通登录") sleep(0.5) driver.find_element(By.ID, "su").click() # #5.点击搜索按钮 sleep(5) driver.find_element(By.XPATH, '//*[@id="2"]/div/div[1]/h3/a').click() handles = driver.window_handles driver.switch_to.window(handles[-1]) sleep(5) driver.find_element(By.ID, "phone").send_keys(username) driver.find_element(By.ID, "pwd").send_keys(pwd) driver.find_element(By.ID, "loginBtn").click() title=driver.title return title @ddt class SimpleTest(unittest.TestCase): def setUp(self): print("set up") def tearDown(self): print("teardown") @data(*getData('week17.csv')) @unpack def test_login(self, username, pwd, title): print("执行test_add") accounttitle = login(self.driver,username,pwd,title) acttitle = title self.assertEqual(accounttitle,acttitle) if __name__=="__main__": unittest.main()
在代码中存在以下问题:
1. 在第一行代码中,需要在import csv和import unittest之间加上一个空格;
2. 在第二行代码中,需要在from time import sleep和from ddt import ddt,data,unpack之间加上一个空格;
3. 在第三行代码中,需要将selenium.webdriver.chrome改为selenium.webdriver.chrome.webdriver;
4. 在第四行代码中,需要将selenium.webdriver.common.by改为selenium.webdriver.common.by.By;
5. 在login函数的第三行和第六行之间需要加上一个空行;
6. 在test_login函数中,需要将login函数的第四个参数改为acttitle,以保证断言的正确性。
修改后的代码如下所示:
``` python
import csv
import unittest
from time import sleep
from ddt import ddt, data, unpack
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
def add(a, b):
return a + b
def getData(file_name):
rows = []
file_data = open(file_name, "r", encoding='utf8')
reader = csv.reader(file_data)
next(reader)
for row in reader:
rows.append(row)
return rows
def login(driver, username, pwd):
driver.get("https://www.baidu.com")
# 3. 在谷歌浏览器中打开百度链接
sleep(0.5)
driver.find_element(By.ID, "kw").send_keys("学习通登录")
sleep(0.5)
driver.find_element(By.ID, "su").click() # #5.点击搜索按钮
sleep(5)
driver.find_element(By.XPATH, '//*[@id="2"]/div/div[1]/h3/a') \
.click()
handles = driver.window_handles
driver.switch_to.window(handles[-1])
sleep(5)
driver.find_element(By.ID, "phone").send_keys(username)
driver.find_element(By.ID, "pwd").send_keys(pwd)
driver.find_element(By.ID, "loginBtn").click()
title = driver.title
return title
@ddt
class SimpleTest(unittest.TestCase):
def setUp(self):
self.driver = WebDriver()
print("set up")
def tearDown(self):
self.driver.quit()
print("teardown")
@data(*getData('week17.csv'))
@unpack
def test_login(self, username, pwd, acttitle):
print("执行test_login")
accounttitle = login(self.driver, username, pwd)
self.assertEqual(accounttitle, acttitle)
if __name__ == "__main__":
unittest.main()
```
希望能够帮到您!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)