import unittest from time import sleep from ddt import ddt, data, unpack from selenium import webdriver import openpyxl from selenium.webdriver.common.by import By def read_excel(): xlsx = openpyxl.load_workbook("D:\python\project\case\xl.xlsx") #打开路径内的xl
时间: 2023-11-09 22:07:47 浏览: 43
.xlsx文件 sheet = xlsx.active #获取活动的工作表 data = [] for row in sheet.rows: data.append([cell.value for cell in row]) #遍历每一行,将每一行的值存入data列表 return data
这段代码的作用是读取指定路径下的 .xlsx 文件,并将其中的数据存储在一个列表中。其中使用了 openpyxl 库来操作 Excel 文件,使用了列表推导式来遍历每一行并存储数据。
相关问题
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()
```
希望能够帮到您!
写一段通过ddt+selenium完成的简单的数据驱动的测试
好的,下面是一个通过 ddt+selenium 完成的简单的数据驱动的测试例子:
```python
import unittest
from ddt import ddt, data, unpack
from selenium import webdriver
@ddt
class TestLogin(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.get('http://www.example.com')
cls.driver.maximize_window()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
@data(('username1', 'password1'), ('username2', 'password2'), ('username3', 'password3'))
@unpack
def test_login(self, username, password):
# 输入用户名和密码,并登录
self.driver.find_element_by_id('username').send_keys(username)
self.driver.find_element_by_id('password').send_keys(password)
self.driver.find_element_by_id('login-btn').click()
# 验证登录是否成功
self.assertTrue('Welcome' in self.driver.page_source)
```
在这个例子中,我们首先通过 `setUpClass()` 方法打开 Chrome 浏览器并访问指定的网站,并在 `tearDownClass()` 方法中关闭浏览器。然后,我们通过 `@data` 和 `@unpack` 装饰器将一组用户名和密码数据传入 `test_login()` 方法中,使用这些数据进行登录,并验证登录是否成功。
注意,为了运行该测试用例,我们还需要安装 ddt 和 selenium 模块。可以使用以下命令进行安装:
```python
pip install ddt
pip install selenium
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)