思路
1.将常见操作:点击按钮,获取元素值,输入值,等待时间,打开浏览器,输入地址,元素定位等封装成方法,方便后续调用
2.准备好excel,将需要读取的数据事先填写好
3.写一个方法,循环读取excel中的数据
4.根据关键词去判断此数据类型,然后进行下一步的操作
5.根据关键词调用不同的方法,执行自动化操作
6.并断言操作后的值是否与预期一致,一致则输入pass,否则输入fail
代码示例
定义方法类
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from selenium.webdriver.common.by import By #定位
class Libraay:
# 打开浏览器
def open_brower(self,brower):
# 首字母大写
brower=brower.capitalize()
self.driver=getattr(webdriver,brower)()
self.driver.maximize_window() # 最大化打开chrome
# 元素定位
def locator(self,locator_type,element):
element_object = None
if locator_type == 'id':
element_object = self.driver.find_element_by_id(element)
elif locator_type == 'name':
element_object = self.driver.find_element_by_name(element)
elif locator_type == 'class_name':
element_object = self.driver.find_element_by_class_name(element)
elif locator_type == 'css':
element_object = self.driver.find_element_by_css_selector(element)
elif locator_type == 'xpath':
element_object = self.driver.find_element_by_xpath(element)
print(element_object)
return element_object
#输入内容定位
def input_text(self, targeting, element, content):
self.locator(targeting, element).send_keys(content)
#点击按钮
def click_btn(self, targeting, element):
self.locator(targeting, element).click()
# 等待时间
def wait_time(self,s):
time.sleep(s)
# 获取元素值内容
def get_text(self,targeting,element):
t_text=self.locator(targeting, element).text
return t_text
#关闭浏览器
def quit_browser(self):
self.driver.quit()
读写excel文件
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
from openpyxl import load_workbook
from datetime import datetime
from Keywords.library import Libraay
import traceback
import os
from Testdatas.conmmon_datas import *
def TestCase(filename):
# 实例化
lib = Libraay()
wb = load_workbook(filename)
print("输出所有sheet表:",wb.sheetnames)
#获取sheet_names
for sheetname in wb.sheetnames:
ws = wb[sheetname]
print("输出当前遍历的:",ws)
#获取最大行数
ws_rows_max = ws.max_row
print(ws_rows_max)
#sheet.rows为生成器, 里面是每一行的数据,每一行又由一个tuple包裹
idx = 2
while idx <= ws_rows_max:
# 序号
num=ws.cell(row=idx,column=1).value
print("序号:",num)
# 操作步骤
steps=ws.cell(row=idx,column=2).value
print("操作步骤:",steps)
# 定位方式
Targeting = ws.cell(row=idx, column=4).value
print("定位方式:", Targeting)
# 元素定位表达式
Element_positioning = ws.cell(row=idx, column=5).value
print("元素定位表达式:",Element_positioning)
# 操作值
Manipulated_value = ws.cell(row=idx, column=6).value
print("操作值:", Manipulated_value)
# 指定的关键字
essential = ws.cell(row=idx, column=3).value
print("指定的关键字:", essential)
try:
#根据传入的方法名来确定每一步执行哪一个方法
if essential == 'open_brower':
lib.open_brower(Manipulated_value)
elif essential == 'open_url':
lib.load_url(Manipulated_value)
elif essential == 'input_text':
lib.input_text( Targeting,Element_positioning, Manipulated_value)
elif essential == 'click_btn':
lib.click_btn(Targeting,Element_positioning)
elif essential == 'quit_browser':
lib.quit_browser()
elif essential == 'wait_time':
lib.wait_time(Manipulated_value)
elif essential == 'assert_get_text':
get_text = lib.get_text(Targeting,Element_positioning)
#断言
if get_text == Manipulated_value:
print(lib.get_text())
#如果执行无误则将执行结果写入Pass
ws.cell(row=idx, column=7).value = 'Pass'
else:
# 如果执行无误则将执行结果写入Pass
ws.cell(row=idx, column=7).value = 'Fail'
wb.save(filename)
return
# 如果执行无误则将执行结果写入Pass
ws.cell(row=idx, column=7).value = 'Pass'
# 获取当前系统时间,作为测试用例执行时间
curr_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
ws.cell(row=idx, column=8).value = curr_time
except Exception:
ws.cell(row=idx, column=7).value = '异常'
# 写入异常信息
ws.cell(row=idx, column=9).value = traceback.format_exc()
idx += 1
wb.save(filename)
if __name__ == '__main__':
TestCase(file_3)
执行结果
参考文章:https://www.cnblogs.com/auguse/articles/14570069.html