zoukankan      html  css  js  c++  java
  • 【自动化测试】搭建一个简单从Excel读取用例内容并输出结果的脚本

    # -*- coding:utf-8 -*-
    from selenium import webdriver
    import xlrd
    import xlwt
    from xlutils.copy import copy
    import time
    
    class Batchauto:
        def __init__(self,x,y):
            self.x = x
            self.y = y
            
        def auto_brower(self):
            #打开浏览器
            open_brower = webdriver.Chrome()
            url = "http://xxxxx"
            open_brower.get(url)
            
            #读取文件
            read_file = r"C:UsersadminDesktoppyautoauto_denglu.xls"
            book = xlrd.open_workbook(read_file)
           #sheet的表格中,已经命名sheet为“denglu”
            read_file_sheet = book.sheet_by_name("denglu")
            
            #获取文件内容
            user_value = read_file_sheet.cell_value(self.x,self.y)
            pwd_value = read_file_sheet.cell_value(self.x,self.y+1)
            
            #填写内容
            time.sleep(2)
            open_brower.find_element_by_id("userName").send_keys(user_value)
            open_brower.find_element_by_id("password").send_keys(pwd_value)
            open_brower.find_element_by_id("submit").click()
            
            time.sleep(5)
            
            #判断登录成功后是否有该元素存在
            try:
                open_brower.find_element_by_xpath("//a[@href='user/confirm.jsp?userName=XXX']")
                tag = u"登录成功"
            except:
                tag = u"登录失败"    
            
            print tag
            
            #填写文件内容,xlrd只有只读没有只写,所以,需要用过xlutils的copy获得写的能力,xlutils相当于xlrd和xlwt的通道
            book_rw = copy(book)
            book_rw_sheet = book_rw.get_sheet("denglu")
            book_rw_sheet.write(self.x,self.y+2,tag)
    
            book_rw.save(read_file)
    
            time.sleep(2)
            open_brower.close()        
    
    if __name__ == "__main__":
        #只需要定位X、y的初始位置即可   
        for x in range(0,3):
            y = 0
            ba = Batchauto(x,y)
            ba.auto_brower()

    最后Excel结果显示为:

  • 相关阅读:
    Maven篇----04 mvn 常用命令
    Maven篇----03 私服配置&使用
    Maven篇----02 Nexus2私服管理
    Maven篇----01 简介&Maven私服
    SonarQube 系列之 — 04 插件扩展
    SonarQube 系列之 — 02 配置与管理
    SonarQube 系列之 — 01 安装和扫描
    JMeter 系列之—-05 支持CI扩展
    一些关于常见的进制教程
    【pic+js+gh】免费高速图床方案
  • 原文地址:https://www.cnblogs.com/mumushizhige/p/9138641.html
Copyright © 2011-2022 走看看