这是对应的关键字框架,用来处理解析好的外部脚本,降低自动化测试门槛:
import time
from selenium.webdriver.common.by import By
from DrivenFramework import Driver
from selenium import webdriver
from FileOp.TXTOP import TXTop
from FileOp.SQLOP import MySQLOP
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
def find_value(data, key):
for value in data:
if value[0] == key:
temp = key
return value[1]
else:
continue
return None
def run_sample(path):
pg = Driver.Page(webdriver.Chrome())
f = TXTop.read_txt_file(type[TXTop], path+'.txt')
c = TXTop.read_txt_file(type[TXTop], 'control.txt')
data = MySQLOP.select_sql(type[MySQLOP], 'root', '', 'automation', 'select * from properties' )
for line in f:
line = line.replace('\n', '')
oparry = line.split('@')
for case in switch(oparry[0]):
if case('goto'):
pg.goto(find_value(data, oparry[1]))
break
if case('input'):
pg.input([By.ID, find_value(data, oparry[1])], oparry[2])
break
if case('click'):
pg.click([By.ID, find_value(data, oparry[1])])
break
if case('sleep'):
time.sleep(float(oparry[1]))
break
if case('get_text'): # default, could also just omit condition or 'if True'
content = pg.get_element_text([By.CSS_SELECTOR, find_value(data, oparry[1])])
print(content)
break
if case('script'):
run_sample(oparry[1])
break
if True:
break
def run_list(path):
pg = Driver.Page(webdriver.Chrome())
f = TXTop.read_txt_file(type[TXTop], path)
for line in f:
run_sample(line)