webUI自动化(五)Excel关键字驱动实现
base_05_Excel关键字驱动实现.py
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from base.chrome_options import chrome_options
class Base:
def __init__(self):
self.driver = webdriver.Chrome(chrome_options=chrome_options())
self.driver.implicitly_wait(5)
def open(self,data_dict):
"""打开网址"""
self.driver.get(data_dict['txt'])
def locate(self,data_dict):
"""定位,应用显式等待"""
"""
WebDriverWait(driver,timeout,poll_frequency,ignored_exceptions)
driver:浏览器驱动
timeout:最长超时时间,默认以秒为单位。
poll_frequency:检测的间隔步长,默认为0.5s。即每0.5s询问一次,元素是否找到。
ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常。
"""
try:
ele = WebDriverWait(self.driver, 6, 0.5).until(lambda s: s.find_element(data_dict['locate_by'],data_dict['locate_value']))
return ele
except:
print("定位失败")
raise
def input(self,data_dict):
"""输入"""
self.locate(data_dict).send_keys(data_dict['txt'])
def click(self,data_dict):
"""单击"""
self.locate(data_dict).click()
def max_window(self,data_dict):
"""窗口最大化"""
self.driver.maximize_window()
def switch_to_iframe(self,data_dict):
"""切换到iframe"""
self.driver.switch_to.frame(data_dict['txt'])
def switch_to_windows(self,data_dict):
"""切换新窗口"""
windows = self.driver.window_handles
self.driver.switch_to.window(windows[int(data_dict['txt'])])
def assert_locate(self,data_dict):
"""断言"""
assert self.locate(data_dict) is not None,'断言失败'
print('断言成功!')
def wait(self,data_dict):
"""固定等待"""
sleep(int(data_dict['txt']))
def quit(self,data_dict):
"""退出浏览器"""
self.driver.quit()
main05_Excel关键字驱动实现.py
import openpyxl
from base.base_05_Excel关键字驱动实现 import Base
class Script:
def __init__(self,excel_path):
self.b = Base()
self.excel_obj = openpyxl.load_workbook(excel_path)
def run(self,sheet_name):
self.sheet_obj = self.excel_obj[sheet_name]
values = self.sheet_obj.values
for value in values:
data_dict = dict()
if value[0] != '编号' and int(value[1]) == 1:
data_dict['no'] = value[0]
data_dict['step_name'] = value[2]
data_dict['kw'] = value[3]
data_dict['locate_by'] = value[4]
data_dict['locate_value'] = value[5]
data_dict['txt'] = value[6]
data_dict['note'] = value[7]
data_dict['except'] = value[8]
getattr(self.b,data_dict['kw'])(data_dict)
def quit(self):
self.b.wait({'txt':'5'})
self.excel_obj.close()
self.quit({})
if __name__ == '__main__':
excel_path = "./data/data.xlsx"
s = Script(excel_path)
try:
s.run("bilibili")
finally:
s.quit()