一、项目结构
-base_func ·········封装一些常用方法,requests、sql、xls等 -config 记录接口地址、数据库地址、页面URL等 -data 存放测试数据、测试文件等 -driver 封装webdriver -element_info 存放页面元素 -env 运行环境,包括selenium、python等 -page_object 封装页面元素的各种方法 -testcase 测试用例存放目录 main.py 自动化用例启动入口
1. webdriver封装
chromedriver的封装如下,根据需要可加入firefox、Edge等浏览器的driver封装。
class Set_Driver(object):
def set_chrome_driver(self, __url):
prefs = {"": ""}
prefs["credentials_enable_service"] = False
prefs["profile.password_manager_enabled"] = False
chrome_options=Options()
chrome_options.add_experimental_option("prefs", prefs)
# 谷歌文档提到需要加上这个规避bug
chrome_options.add_argument('--disable-gpu')
# 设置默认编码为utf-8
chrome_options.add_argument('lang=zh_CN.UTF-8')
chrome_options.add_experimental_option('useAutomationExtension', False)
# 取消chrome受自动化软件控制提示
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(10)
driver.maximize_window()
driver.get(__url)
return driver
2. selenium封装
此处对selenium的基本方法做了一个简单的封装,包括元素点击、查找、选择等。
class SeleniumDriver(object):
ip = mod_config.get_config("address", "ip_web")
login_page = ip+mod_config.get_config("page", "login_page")
def __init__(self, __driver):
self.driver = __driver
def close_driver(self):
self.driver.close()
def get_element(self, type, value):
try:
element = None
if type == 'id':
element = self.driver.find_element_by_id(value)
elif type == 'name':
element = self.driver.find_element_by_name(value)
elif type == 'css':
element = self.driver.find_element_by_css_selector(value)
elif type == 'class':
element = self.driver.find_element_by_class_name(value)
else:
element = self.driver.find_element_by_xpath(value)
except:
print("定位方式:", by, "定位值", value, "定位出现错误,没有定位成功")
return element
3. 页面元素提取
采用yaml文件存放页面元素
start_date:
dsc: 请假开始日期
type: xpath
value: //*[@id="app"]/div/div/div/section/section/main/div/div/form/div[1]/div/div/div[4]/div[1]/div/div[1]/div/div[2]/div/span/span
end_date:
dsc: 请假结束日期
type: xpath
value: //*[@id="app"]/div/div/div/section/section/main/div/div/form/div[1]/div/div/div[4]/div[2]/div/div[1]/div/div[2]/div/span/span
页面元素读取方法:
def parseyaml(__filename):
path = os.path.dirname(os.path.realpath(__file__))
yaml_path = path.split("base_func")[0]+"element_info\\"+__filename
page_elements = {}
if ".yaml" in str(yaml_path):
with open(yaml_path, 'r', encoding='utf-8') as f:
page = yaml.load(f, Loader=yaml.FullLoader)
page_elements.update(page)
return page_elements
4. 页面操作封装:
class AskLeavePage(SeleniumDriver):
element_info = parse_yml.parseyaml("ask_leave.yaml")
def __init__(self, __driver):
super().__init__(__driver)
def click_leave_management(self):
"""
点击请假管理
:return:
"""
self.click_element(self.element_info["leave_management"]["type"],
self.element_info["leave_management"]["value"])
5. 测试用例:
class TestClassRegister(object):
def setup_method(self):
self.register_page = register_method.RegisterClass() #初始化
def teardown_method(self):
self.register_page.driver.quit() # 恢复环境,关闭driver
def test_case_001(self):
"""
用例描述
"""
|