selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
来源:互联网 发布:icmo是tcp的哪个端口 编辑:程序博客网 时间:2024/05/16 08:41
话不多说直接上代码
# encoding:utf-8from selenium import webdriverimport unittestimport xlwtimport xlrdimport timefrom xlutils.copy import copyfrom selenium.webdriver.common.by import Byclass Denglu(unittest.TestCase): def setUp(self): self.driver=webdriver.Firefox() self.baseurl="http://localhost:8000/phpwind85" def testGetExcel(self,excelfile): """ 该函数主要是获取需要读取数据的excel,如果找到就返回该excel,如果找不到给出提示信息 """ try: data=xlrd.open_workbook(excelfile)#获取通过excel路径工作表 table=data.sheets()[0]#制定需要操作的excel的sheet return table#返回需要操作的excel的sheet except: return False def testGetRows(self): """ 该方法主要是得到要读取数据的excel的行 """ table=self.testGetExcel("E:\\wwg\\dengludata.xls") if table: rows=table.nrows print "该文档的有%i行" %rows return rows else: print("该excel文档不存在") def testGetCols(self): """ 该方法主要是得到要读取数据的excel的列 """ table=self.testGetExcel("E:\\wwg\\dengludata.xls") if table: cols=table.ncols return cols else: print("该excel文档不存在") def testGetLoginDict(self): """ 该方法主要是得到从excel读取的数据并整合成列表 """ table=self.testGetExcel("E:\\wwg\\dengludata.xls")#向testGetExcel方法传递一个excel的路径的参数 logindict={}#定义一个字典 rows=self.testGetRows()#获取该excel一共有多少行 cols=self.testGetCols()#获取该excel一共有多少列 for i in range(1,rows): for m in range(cols-1): logindict[table.cell(i,m).value]=table.cell(i,i).value#使用字典的赋值方法,把用户名和密码作为key和value对应赋值 return list(logindict.items())#把得到的有用户名和密码的字典转换为列表并返回 def testLoginin(self): """ 该方法主要是进行登录操作 """ resultlist=[]#定义一个测试结果的列表 ppp=u"登录成功" faile=u"登录失败" br=self.driver br.get(self.baseurl) loginlist=self.testGetLoginDict()#通过调用方法,得到用户名和密码的列表 br.find_element_by_link_text(u"登录").click() for k,v in loginlist:#遍历上面步骤得到的用户名和密码的列表 br.find_element_by_id("J_u_login_username").send_keys(k)#传递用户名 br.find_element_by_id("J_u_login_password").send_keys(v)#传递密码 br.find_element_by_xpath("//*[@id='J_u_login_form']/div/dl[4]/dd/button").click() time.sleep(10) if br.current_url=="http://localhost:8000/phpwind85/": resultlist.append(ppp)#向测试结果列表中增加值 resultlist.append(br.current_url) self.testWriteData(resultlist)#调用testWriteData方法往excel中输入测试结果 else: print(faile) resultlist.append(faile) self.testWriteData(resultlist) def testWriteData(self,result): rb=xlrd.open_workbook("E:\\wwg\\qqq.xls") wb=copy(rb) wbk=wb.get_sheet(0) for i in range(len(result)): wbk.write(0,i,result[i]) wb.save("E:\\wwg\\qqq.xls") def tearDown(self): passif __name__=="__main__": suite=unittest.TestSuite()# suite.addTest(Denglu("testGetExcel"))# suite.addTest(Denglu("testGetRows"))# suite.addTest(Denglu("testGetCols"))# suite.addTest(Denglu("testGetLoginList")) suite.addTest(Denglu("testLoginin")) runner=unittest.TextTestRunner() runner.run(suite)
1 0
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- C#读取EXCEL 文件同时向文件中写入数据和Excel的Range对象
- Python读取txt内容写入xls格式的excel中
- Python读取txt内容写入xlsx格式的excel中
- 使用jxl工具包生成Excel以及读取Excel的方法
- 使用xlwings读取excel中所有的值,写入mysql
- 使用Python和xlwt向Excel文件中写入中文
- 使用Python和xlwt向Excel文件中写入中文
- 使用python向excel中写入数据(中英文混合)
- Python中向excel中写入数据
- Python向excel中写入数据
- Excel 数据的读取写入
- POI excel的读取,写入
- Excel的读取和写入
- python使用xlwt读取文件写入Excel
- 关于python读取excel,excel表格中存放字典的读取方式
- poi 中读取和写入excel 方法
- bsd修改IP、子网掩码、路由等信息
- 怎样判断一个一个控件是否已经销毁
- 循序渐进学Python】11.常用标准库
- centos上网设置
- Socket 网络通信并解析文件.
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- 升序排序
- 关于mini2440开发板USB转串口乱码问题
- spring hessian 配置
- 舌尖上的中国
- POI创建Excel
- 美团网CEO王兴:创业十年屡败屡战 终成硕果
- 游标
- 高斯滤波理解