selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
2014-07-07 11:35
1486 查看
话不多说直接上代码
# encoding:utf-8 from selenium import webdriver import unittest import xlwt import xlrd import time from xlutils.copy import copy from selenium.webdriver.common.by import By class 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): pass if __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)
相关文章推荐
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
- Python测试用例生成脚本(合并相应单元格以及写入单行数据)代码实例
- Delphi中使用python脚本读取Excel数据
- python读写Excel文件--使用xlrd模块读取,xlwt模块写入
- 关于Windows 8 开发之测试代码的使用方法
- scrapy一些非常实用资料整理,与其他mongdb、django、redis、solr、tor结合使用以及用Python脚本调用等等方法
- Delphi中使用python脚本读取Excel数据
- 使用python脚本代码本地测试环境部署和启动 V0.1
- ZK框架简介,以及在ZK下使用JXL读取Excel模板,导出数据模型到Excel的方法
- Python3--文件读取、写入、追加及seek()方法的使用
- 使用jxl工具包生成Excel以及读取Excel的方法
- Python使用xlrd读取Excel格式文件的方法
- 【python】解析Excel中使用xlrd库、xlwt库操作,写入excel进阶完整脚本实例(五)
- 使用Python导出Excel图表以及到处为图片的方法
- 使用Python导出Excel图表以及导出为图片的方法
- 关于serialPort控件的使用跟INI文件的创建,读取,写入方法
- python 在excel文件中写入date日期数据,以及读取excel日期数据,如何在python中正确显示date日期。
- 归纳STM32 FLASH 擦除(以及如何防止误擦除程序代码)、写入、读取方法
- 用SQL脚本读取Excel中的sheet数量及名称的方法代码