import time
import traceback
from selenium import webdriver
from selenium.webdriver.support.select import Select
from web_auto_frame_2.test_tools.log import Log
class BasePage:
def __init__(self, driver):# 构造函数
self.driver: webdriver.Chrome = driver
self.log = Log()
def __find_element(self, locator: tuple, img_name=''):
"""
元素定位
:param locator: 定位的表达式(元组类型)
:param img_name: 异常时 截图图片名称(页面名称_模块名称_操作名称)
:return:
"""
try:
ele = self.driver.find_element(*locator)
self.log.info("元素%s定位成功"%str(locator))
return ele
except Exception:
self.log.error(traceback.format_exc())
self.log.error("元素%s定位失败"%str(locator))
self.get_screen_shot(img_name)
def input(self, locator: tuple, text='', img_name=''):
"""
输入操作
:param locator: 元素定位
:param text: 输入的数据
:return:
"""
ele = self.__find_element(locator,img_name)
if ele:
ele.send_keys(text)
self.log.info(f"输入{text}成功")
def click(self, locator: tuple, img_name=''):
ele = self.__find_element(locator, img_name)
if ele:
ele.click()
self.log.info(f"点击操作成功")
def switchwin(self, idx=-1):
"""
浏览器窗口切换
:param idx: 窗口列表的下标
:return:
"""
handles = self.driver.window_handles
self.driver.switch_to.window(handles[idx])
self.log.info(f"浏览器窗口切换成功: 已成功切换至{handles[idx]}")
def intoframe(self, locator: tuple, img_name=''):
"""
切入frame框架
:param locator:
:return:
"""
ele = self.__find_element(locator, img_name)
if ele:
self.driver.switch_to.frame(ele)
self.log.info("frame切换成功")
def outoframe(self):
"""切出frame"""
self.driver.switch_to.default_content()
self.log.info("切出frame成功")
def select(self, locator: tuple, visible_text='', img_name=''):
"""
下拉框选择操作
:param locator:
:param visible_text:
:return:
"""
ele = self.__find_element(locator, img_name)
if ele:
select = Select(ele)
select.select_by_visible_text(visible_text)
self.log.info(f"下拉框选择成功: 已选择{visible_text}")
def gettitle(self):
title = self.driver.title
return title
def geturl(self):
url = self.driver.current_url
return url
def gettext(self, locator: tuple, img_name=''):
"""
获取标签的文本值
:param locator:
:return:
"""
ele = self.__find_element(locator,img_name)
if ele:
text = ele.text
self.log.info(f"元素{locator}的文本值为: {text}")
return text
def get_attr(self, locator: tuple, attr_name='', img_name=''):
"""
获取指定元素标签的 指定属性值
:param locator: 定位表达式
:param attr_name: 属性名称
:return:
"""
ele = self.__find_element(locator, img_name)
if ele:
value =ele.get_attribute(attr_name)
self.log.info(f'元素{locator}的{attr_name}属性的值为{value}')
return value
def assert_text(self, expect='', result=''):
"""
断言: 预期数据与实际数据完全一致
:param expect: 预期
:param result: 实际
:return:
"""
if expect == result:
self.log.info(f"预期结果为:{expect}, 实际结果为: {result}, 预期与实际一致, 测试通过")
return True
self.log.error(f"预期结果为:{expect}, 实际结果为: {result}, 预期与实际不一致, 测试失败")
return False
def assert_contains(self, expect='', result=''):
"""
断言预期结果包含于实际结果中
:param expect:
:param result:
:return:
"""
if result.__contains__(expect):
self.log.info(f"预期结果:{expect}包含于{result}, 测试通过")
return True
self.log.error(f"预期结果:{expect}不包含于{result}, 测试失败")
return False
def sleep(self, t=1):
time.sleep(t)
def get_screen_shot(self, img_name=''):
"""
截图
:param img_name: 图片名称(页面名称_模块名_功能名称)
:return:
"""
path = './test_results/imgs/'+img_name+'.png'
self.driver.get_screenshot_as_file(path)