目的:实现了操作步骤与程序的分离以及数据与程序的分离,提高代码复用性,减少代码冗余
封装公共类:General.py、operate_file.py
General.py:主要封装功能为操作步骤方法+定位元素方法
operate_file.py:主要封装功能为读取测试步骤+读取测试数据+eval执行方法操作
业务类(Functions.py):根据功能划分方法,提高代码可读性
代码如下:
General.py代码:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
class Common(object):
driver=""
def open_browser(self,browser_name):#启动浏览器驱动
global driver
if browser_name.lower().strip()=="ie":
driver=webdriver.Ie()
elif browser_name.lower().strip()=="firefox":
driver=webdriver.Firefox()
elif browser_name.lower().strip()=="chrome":
driver=webdriver.Chrome()
else:
return ("未知浏览器")
driver.maximize_window()
def visit_url(self,url):#获取网页源码
global driver
driver.set_page_load_timeout(3)
driver.get(url)
def implicit_waiting(self,times):#隐式等待时间
global driver
driver.implicitly_wait(int(times))
def sleep_time(self,times):
time.sleep(int(times))
def quit(self):#关闭网页
global driver
driver.quit()
def location_element(self,type,element):#元素定位
global driver
if type.lower().strip()=="id":
el= driver.find_element_by_id(element)
elif type.lower().strip()=="name":
el= driver.find_element_by_name(element)
elif type.lower().strip()=="tag":
el= driver.find_element_by_tag_name(element)
elif type.lower().strip()=="class":
el= driver.find_element_by_class_name(element)
elif type.lower().strip()=="link_text":
el= driver.find_element_by_link_text(element)
elif type.lower().strip()=="partial_link":
el= driver.find_element_by_partial_link(element)
elif type.lower().strip()=="xpath":
el= driver.find_element_by_xpath(element)
elif type == "css_selector":
el= driver.find_element_by_css_selector(element)
return el
def input_content(self,type,element,data):#输入数据
el=self.location_element(type,element)
el.clear()
return el.send_keys(data)
def swith_to_iframe(self,type,element):#切至iframe
global driver
el=self.location_element(type,element)
return driver.switch_to.frame(el)
def swith_out_iframe(self):#切出iframe
global driver
return driver.switch_to.default_content()
def click(self,type,element):#点击事件
el=self.location_element(type,element)
el.click()
def submit(self,type,element):#表单提交事件
el=self.location_element(type,element)
el.submit()
def keys_enter(self,type,element):#键盘回车事件
el=self.location_element(type,element)
el.send_keys(Keys.ENTER)
def action_chains(self,type,element):#鼠标双击事件
global driver
el=self.location_element(type,element)
action_chains = ActionChains(driver)
action_chains.double_click(el).perform()
operate_file.py代码:
import os
import re
from General import Common
class operate_file(object):
def read_test_data(self,file_path):#读测试数据
test_data=[]
if os.path.exists(file_path):
with open(file_path,"r",encoding="utf-8") as fp:
temp=fp.readlines()
for line in temp:
test_data.append(eval(line.strip()))
return test_data
else:
return False
def read_test_step_data(self,file_path):#读测试步骤数据
test_step_data=[]
if os.path.exists(file_path):
with open(file_path,"r",encoding="utf-8") as fp:
temp=fp.readlines()
for line in temp:
datalist=line.strip().split("||")
templist=[]
for data in range(len(datalist)):
templist.append(datalist[data])
test_step_data.append(templist)
return test_step_data
else:
return False
def eval_commands(self,operate_test_data,operate_test_step_data):
General=Common()
for test_data in operate_test_data:
for test_step_data in operate_test_step_data:
if len(test_step_data)==1:
command=test_step_data[0]+"()"
elif len(test_step_data)==2:
command=test_step_data[0]+"('"+test_step_data[1]+"')"
elif len(test_step_data)==3:
command=test_step_data[0]+"('"+test_step_data[1]+"','"+test_step_data[2]+"')"
elif len(test_step_data)==4:
keyword="".join(re.findall(r"^\$\{(.*?)\}",test_step_data[3]))
command = test_step_data[0] + "('" + test_step_data[1] + "','" + test_step_data[2] + "','"+test_data[keyword]+"')"
print("General."+command)
eval("General."+command)
Functions.py文件代码:
from operate_file import operate_file
class functions(object):
def __init__(self):
self.of = operate_file()
def login_fun(self):#登录
login_test_data=self.of.read_test_data("login_test_data.txt")
login_test_step_data=self.of.read_test_step_data("login_test_step_data.txt")
self.of.eval_commands(login_test_data,login_test_step_data)
def create_contact(self):#创建联系人
create_contact_test_data = self.of.read_test_data("create_contact_test_data.txt")
create_contact_test_step_data = self.of.read_test_step_data("create_contact_test_step_data.txt")
self.of.eval_commands(create_contact_test_data, create_contact_test_step_data)
def send_email(self):#发送邮件
send_email_test_data=self.of.read_test_data("send_email_test_data.txt")
send_email_test_step_data=self.of.read_test_step_data("send_email_test_step_data.txt")
self.of.eval_commands(send_email_test_data,send_email_test_step_data)
if __name__=="__main__":
fun=functions()
fun.login_fun()
fun.create_contact()
fun.send_email()
运行结果: