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
原创粉丝点击