python webdriver keywords framework

Keyword framework is recommended to use eval, do not use exec

eval () function returns a value, exec () function returns no value

 

teststep.txt

open||chrome
visit||https://www.126.com
click||switchAccountLogin
sleep||3

 

The main program script

keyword.py

 

#encoding=utf-8
from selenium import webdriver
import time

with open("teststep.txt") as fp:
teststeps = fp.readlines()

driver = ""

def open(browser_name):
global driver
if "ie" in browser_name:
driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
elif "chrome" in browser_name:
driver = webdriver.Chrome(executable_path = "e:\\chromedriver")
else:
driver = webdriver.Firefox(executable_path = "e:\\geckodriver")

def visit(url):
global driver
driver.get(url)

def click(id):
try:
driver.find_element_by_id(id).click()
except:
print("click fail!")
raise

def sleep(times):
time.sleep(int(times))

for teststep in teststeps:
action = teststep.split("||")[0] #action = "open"
value= teststep.split("||")[1].strip() #value= "chrome"
try:
command = "%s(\"%s\")" %(action,value) # 拼接函数func(参数1,参数2)"open(\"chrome\")"
eval(command) #利用eval()执行函数 open("chrome")
except:
print("执行",command,"有异常")

print ("DONE!")

 

Guess you like

Origin www.cnblogs.com/ff-gaofeng/p/12670279.html