测试数据文件
测试脚本:
#encoding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
#定义获取excel所有行的方法
def get_rows(file_path):
from openpyxl import load_workbook
try:
wb = load_workbook(file_path)
sheet = wb.active
rows = []
for row in sheet.iter_rows():
rows.append(row)
return rows
except Exception as e:
print(e)
#定义获取页面元素的方法
def get_element(driver,locateType,locatorexpression):
try:
if locateType.lower() == "id":
locateType = By.ID
elif locateType.lower() == "xpath":
locateType = By.XPATH
element = driver.find_element(locateType,locatorexpression)
#print(element)
return element
except Exception as e:
raise e
#打开浏览器关键字
def open_browser(browser):
global driver
try:
if browser.lower() == "chrome":
driver = webdriver.Chrome(executable_path="e:\\chromedriver")
elif browser.lower() == "firefox":
driver = webdriver.Firfox(executable_path = "e:\\geckodriver")
except Exception as e:
raise e
#访问指定网址关键字
def visit_url(url):
global driver
try:
driver.get(url)
except Exception as e:
raise e
#输入框输入内容关键字
def input_string(locateType,locatorexpression,content):
global driver
try:
element = get_element(driver,locateType,locatorexpression)
element.send_keys(content)
except Exception as e:
raise e
#点击按钮关键字
def click(locateType,locatorexpression):
global driver
try:
element = get_element(driver,locateType,locatorexpression)
element.click()
except Exception as e:
raise e
#暂停等待关键字
def pause(seconds):
import time
try:
time.sleep(seconds)
except Exception as e:
raise e
#切换进入指定frame关键字
def switch_to_frame(locateType,locatorexpression):
global driver
try:
element = get_element(driver,locateType,locatorexpression)
driver.switch_to.frame(element)
except Exception as e:
raise e
#从子frame切换回主frame关键字
def switch_to_parent_frame():
global driver
try:
driver.switch_to.parent_frame()
except Exception as e:
raise e
#断言关键字
def assert_word(assertWord):
global driver
try:
assert assertWord in driver.page_source
except Exception as e:
raise e
#退出浏览器驱动,并关闭所有浏览器窗口关键字
def close_browser():
global driver
try:
driver.quit()
except Exception as e:
raise e
#测试主函数,实现测试主逻辑
def main():
import traceback
try:
test_file_path = "G:\\个人\\学习\\光荣之路\\自动化\\自动化练习\\testdata.xlsx"
excel_rows = get_rows(test_file_path)
#遍历测试数据库文件excel的每行
for row in excel_rows[1:]:
action = row[1].value
locateType = row[2].value
locatorExpression = row[3].value
operateValue = row[4].value
#print(action,locateType,locatorExpression,operateValue)
#根据每行的内容拼接需要执行的关键字
if locateType == None and locatorExpression == None and operateValue == None:
command = "%s()" %action
elif locateType == None and locatorExpression == None and operateValue != None:
if isinstance(operateValue,int):
command = "%s(%d)" %(action,operateValue)
else:
command = "%s('%s')" %(action,operateValue)
elif locateType != None and locatorExpression != None and operateValue == None:
command = "%s('%s','%s')" %(action,locateType,locatorExpression)
elif locateType != None and locatorExpression != None and operateValue != None:
command = "%s('%s','%s','%s')" %(action,locateType,locatorExpression,operateValue)
print(command)
try:
#执行关键字函数
eval(command)
except Exception as e:
print(traceback.print_exc())
except Exception as e:
print(e)
if __name__ == "__main__":
main()
#driver = webdriver.Chrome(executable_path = "e:\\chromedriver")
#driver.get("http://mail.qq.com")
#login_frame = driver.find_element(By.XPATH,'//iframe[@id="login_frame"]')
#print(login_frame)
#driver.switch_to.frame(login_frame)
#username = driver.find_element(By.XPATH,'//input[@id="u"]')
#username.send_keys("xxxx")
转载于:https://blog.51cto.com/13496943/2345283