importosimporttimefrom PIL importImagefrom selenium importwebdriverfrom appium importwebdriver as appfrom selenium.common.exceptions import *
from selenium.webdriver.common.keys importKeysfrom selenium.webdriver.common.action_chains importActionChainsfrom selenium.webdriver.support importexpected_conditions as ECfrom selenium.webdriver.support.select importSelectfrom selenium.webdriver.support.wait importWebDriverWaitfrom common.logger importLogfrom common importread_configdef open_browser(browser='chrome'):"""打开浏览器函数。"firefox"、"chrome"、"ie",'phantomjs'"""
#驱动路径
driver_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))try:if browser == 'firefox':
executable_path= os.path.join(driver_path, 'driver\\geckodriver.exe')#executable_path = os.path.join(driver_path, 'driver/geckodriver')
driver = webdriver.Firefox(executable_path=executable_path)returndriverelif browser == 'chrome':#加启动配置,忽略 Chrome正在受到自动软件的控制 提示
option =webdriver.ChromeOptions()
option.add_argument('disable-infobars')#chrome启动静默模式;默认显示浏览器界面
if read_config.chrome_interface != 'True':
option.add_argument('headless')
executable_path= os.path.join(driver_path, 'driver\\chromedriver.exe')#executable_path = os.path.join(driver_path, 'driver/chromedriver')
driver = webdriver.Chrome(chrome_options=option, executable_path=executable_path)returndriverelif browser == 'ie':
driver=webdriver.Ie()returndriverelif browser == 'js':
driver=webdriver.PhantomJS()returndriverelse:
Log().warning('额,暂不支持此浏览器诶。先试试firefox、chrome、ie、phantomJS浏览器吧。')return
exceptException as msg:
Log().error('浏览器出错了呀!%s' %msg)return
defopen_app():try:
desired_caps={'platformName': read_config.platform_name,'deviceName': read_config.device_name,'platformVersion': read_config.platform_version,'appPackage': read_config.app_package,'appActivity': read_config.app_activity,'noReset': True,#隐藏手机默认键盘
'unicodeKeyboard': True,'resetKeyboard': True
}#关联appium
driver = app.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)returndriverexceptException as e:raise Exception('连接 Appium 出错:{}'.format(e))classCrazy:"""基于原生的selenium框架做二次封装"""
def __init__(self, driver):"""启动浏览器参数化,默认启动chrome"""self.driver=driver
self.action=ActionChains(self.driver)
self.timeout= 5 #显示等待超时时间
self.t = 1self.log=Log()def open(self, url, t=''):"""get url,最大化浏览器,判断title"""self.driver.get(url)
self.driver.implicitly_wait(10)#是否最大化浏览器
if read_config.maximize != 'True':
self.driver.maximize_window()try:
WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_contains(t))
self.log.info('打开网页成功!')exceptTimeoutException:
self.log.error('打开%s title错误,超时' %url)exceptException as msg:
self.log.error('打开网页产生的其他错误:%s' %msg)deffind_element(self, locator):"""重写元素定位方法"""
if notisinstance(locator, tuple):
self.log.error('locator参数必须是元组类型,而不是:{}'.format(type(locator)))return ""
else:try:
element=WebDriverWait(self.driver, self.timeout, self.t).until(
EC.presence_of_element_located(locator))ifelement.is_displayed():returnelementexcept:
self.log.info('%s页面中未能找到元素%s' %(self, locator))return ""
deffind_elements(self, locator):"""定位一组元素"""
if notisinstance(locator, tuple):
self.log.error('locator参数必须是元组类型,而不是:{}'.format(type(locator)))return ""
else:try:
elements=WebDriverWait(self.driver, self.timeout, self.t).until(
EC.presence_of_all_elements_located(locator))returnelementsexcept:
self.log.info('%s页面中未能找到元素%s' %(self, locator))return ""
def click_coordinate(self, coordinate, timeout=10):"""点击坐标"""self.driver.tap(coordinate, timeout)defclicks(self, locator, n):"""点击一组元组中的一个"""element=self.find_elements(locator)[n]
element.click()defclick(self, locator):"""点击操作"""element=self.find_element(locator)
element.click()defdouble_click(self, locator):"""双击操作"""element=self.find_element(locator)
self.action.double_click(element).perform()defsend_keys(self, locator, text):"""发送文本,清空后输入"""element=self.find_element(locator)
element.clear()
element.send_keys(text)defsends_keys(self, locator, n, text):"""选中一组元素中的一个,发送文本,清空后输入"""element=self.find_elements(locator)[n]
element.clear()
element.send_keys(text)defsend_keys_enter(self):"""敲enter"""self.action.send_keys(Keys.ENTER).perform()defsend_keys_down(self):"""敲向下键"""self.action.send_keys(Keys.DOWN).perform()defsend_keys_arrow_down(self):
self.action.send_keys(Keys.ARROW_DOWN).perform()defsend_keys_arrow_right(self):
self.action.send_keys(Keys.ARROW_RIGHT).perform()defis_text_in_element(self, locator, text):"""判断文本在元素里,没定位到元素返回False,定位到返回判断结果布尔值"""
try:
result=WebDriverWait(self.driver, self.timeout, self.t).until(
EC.text_to_be_present_in_element(locator, text))
self.log.info('is_text_in_element 成功')returnresultexceptTimeoutException:
self.log.error('%s元素没有定位到' %str(locator))returnFalsedefis_text_in_value(self, locator, value):"""判断元素的value值,没有定位到返回False,定位到返回判断结果布尔值"""
try:
result=WebDriverWait(self.driver, self.timeout, self.t).until(
EC.text_to_be_present_in_element_value(locator, value))exceptTimeoutException:
self.log.error('元素没有定位到:%s' %str(locator))returnFalseelse:returnresultdefis_title(self, title):"""判断title完全等于"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_is(title))returnresultdefis_title_contains(self, title):"""判断title包含"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_contains(title))returnresultdefis_selected(self, locator):"""判断元素被选中,返回布尔值, 一般用在下拉框"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.element_located_to_be_selected(locator))returnresultdef is_selected_be(self, locator, selected=True):"""判断元素的状态,selected是期望的参数True/False,返回布尔值"""result=WebDriverWait(self.driver, self.timeout, self.t).until(
EC.element_located_selection_state_to_be(locator, selected))returnresultdefis_alert_present(self):"""判断页面是否有alert,有返回alert,没有返回False"""result=WebDriverWait(self.driver, self.timeout, self.t).until((EC.alert_is_present()))
text=EC.alert_is_present()(self.driver)iftext:
self.log.info('alert弹框显示文本是:%s' %text.text)else:
self.log.warning('没有发现alert弹框。')returnresultdefis_visibility(self, locator):"""元素可见返回本身,不可见返回False"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.visibility_of_element_located(locator))returnresultdefis_invisibility(self, locator):"""元素可见返回本身,不可见返回True,没有找到元素也返回True"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.invisibility_of_element_located(locator))returnresultdefis_clickAble(self, locator):"""元素可以点击返回本身,不可点击返回False"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.element_to_be_clickable(locator))returnresultdefis_locator(self, locator):"""判断元素有没有被定位到(并不意味着可见),定位到返回element,没有定位到返回False"""result=WebDriverWait(self.driver, self.timeout, self.t).until(EC.presence_of_element_located(locator))returnresultdefmove_to_element(self, locator):"""鼠标悬停操作"""element=self.find_element(locator)
ActionChains(self.driver).move_to_element(element).perform()defmove(self, locator, locator1):"""循环调用鼠标事件,死循环"""self.move_to_element(locator)
time.sleep(2)
element=self.find_element(locator1)
self.move_to_element(locator1)try:ifelement.is_displayed:
self.click(locator1)else:
self.move(locator, locator1)exceptElementNotVisibleException as e:
self.log.error('鼠标点击事件失败:%s' %e)defdrag_and_drop(self, element, element1):"""拖拽"""ActionChains(self.driver).drag_and_drop(element, element1).perform()
ActionChains(self.driver).click_and_hold(element).release(element1).perform()defswitch_frame(self, frame):"""切换ifarm"""
try:
self.driver.switch_to_frame(self.find_element(frame))
self.log.info('切换iframe成功!')except:
self.log.warning('没有发现iframe元素%s' %frame)defcurrent_window_handle(self):"""浏览器handle"""
returnself.driver.current_window_handledefswitch_window_handle(self, n):"""切换handle"""
if notisinstance(n, int):
self.driver.switch_to.window(n)else:
all_handle=self.driver.window_handles
self.driver.switch_to.window(all_handle[n])defback(self):"""返回之前的网页"""self.driver.back()defforward(self):"""前往下一个网页"""self.driver.forward()defclose(self):"""关闭当前网页"""self.driver.close()defquit(self):"""关闭所有网页"""self.driver.quit()defget_title(self):"""获取title"""
returnself.driver.titledefget_texts(self, locator, n):"""获取一组相同元素中的指定文本"""element=self.find_elements(locator)[n]ifelement:returnelement.textelse:returnNonedefget_text(self, locator):"""获取文本"""element=self.find_element(locator)ifelement:returnelement.textelse:returnNonedefget_attribute(self, locator, name):"""获取属性"""element=self.find_element(locator)ifelement:returnelement.get_attribute(name)defjs_execute(self, js):"""执行js"""
returnself.driver.execute_script(js)defjs_focus_element(self, locator):"""聚焦元素"""target=self.find_element(locator)
self.driver.execute_script("arguments[0].scrollIntoView();", target)defjs_scroll_top(self):"""滚动到顶部"""js= "var q=document.documentElement.scrollTop=0"self.driver.execute_script(js)defjs_scroll_bottom(self):"""滚动到底部"""js= "var q=document.documentElement.scrollTop=10000"self.driver.execute_script(js)defselect_by_index(self, locator, index):"""通过索引,index是第几个,从0开始, 下拉框"""element=self.find_element(locator)
Select(element).select_by_index(index)defselect_by_value(self, locator, value):"""通过value属性"""element=self.find_element(locator)
Select(element).select_by_value(value)defselect_by_text(self, locator, text):"""通过text属性"""element=self.find_element(locator)
Select(element).select_by_visible_text(text)defsave_screenshot(self, img_path):"""获取电脑屏幕截屏"""self.driver.save_screenshot(img_path)defsave_report_html(self):"""可以在html报告中使用的截图"""self.driver.get_screenshot_as_base64()defsave_element_img(self, locator, img_path):"""获取元素截图"""self.driver.save_screenshot(img_path)
element=self.find_element(locator)
left= element.location['x']
top= element.location['y']
right= element.location['x'] + element.size['width']
bottom= element.location['y'] + element.size['height']
im=Image.open(img_path)
im=im.crop((left, top, right, bottom))
im.save(img_path)defget_cookies(self):"""获取cookies"""
returnself.driver.get_cookies()def swipeDown(self, t=500, n=1):'''向下滑动屏幕'''l=self.driver.get_window_size()
x1= l['width'] * 0.5 #x坐标
y1 = l['height'] * 0.25 #起始y坐标
y2 = l['height'] * 0.75 #终点y坐标
for i inrange(n):
self.driver.swipe(x1, y1, x1, y2, t)def swipeUp(self, t=500, n=1):'''向上滑动屏幕'''l=self.driver.get_window_size()
x1= l['width'] * 0.5 #x坐标
y1 = l['height'] * 0.65 #起始y坐标
y2 = l['height'] * 0.25 #终点y坐标
for i inrange(n):
self.driver.swipe(x1, y1, x1, y2, t)if __name__ == '__main__':
driver=open_browser()
driver.get('file:///D:/UIAutomation/report/2019-01-15%2017-40-10report.html')