【自动化测试】搭建一个简单从Excel读取用例内容并输出结果的脚本

时间:2022-10-23 20:31:18
# -*- 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:\Users\admin\Desktop\py\auto\auto_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结果显示为:

【自动化测试】搭建一个简单从Excel读取用例内容并输出结果的脚本