import os import time from PIL import Image from selenium import webdriver from appium import webdriver as app from selenium.common.exceptions import * from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.select import Select from selenium.webdriver.support.wait import WebDriverWait from common.logger import Log from common import readConfig def 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) return driver elif browser == ‘chrome‘: # 加启动配置,忽略 Chrome正在受到自动软件的控制 提示 option = webdriver.ChromeOptions() option.add_argument(‘disable-infobars‘) # chrome启动静默模式;默认显示浏览器界面 if readConfig.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) return driver elif browser == ‘ie‘: driver = webdriver.Ie() return driver elif browser == ‘js‘: driver = webdriver.PhantomJS() return driver else: Log().warning(‘额,暂不支持此浏览器诶。先试试firefox、chrome、ie、phantomJS浏览器吧。‘) return except Exception as msg: Log().error(‘浏览器出错了呀!%s‘ % msg) return def open_app(): try: desired_caps = { ‘platformName‘: readConfig.platform_name, ‘deviceName‘: readConfig.device_name, ‘platformVersion‘: readConfig.platform_version, ‘appPackage‘: readConfig.app_package, ‘appActivity‘: readConfig.app_activity, ‘noReset‘: True, # 隐藏手机默认键盘 ‘unicodeKeyboard‘: True, ‘resetKeyboard‘: True } # 关联appium driver = app.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps) return driver except Exception as e: raise Exception(‘连接 Appium 出错:{}‘.format(e)) class Crazy: """基于原生的selenium框架做二次封装""" def __init__(self, driver): """启动浏览器参数化,默认启动chrome""" self.driver = driver def open(self, url, t=‘‘, timeout=5): """get url,最大化浏览器,判断title""" self.driver.get(url) self.driver.implicitly_wait(10) # 是否最大化浏览器 if readConfig.maximize != ‘True‘: self.driver.maximize_window() try: WebDriverWait(self.driver, timeout, 1).until(EC.title_contains(t)) Log().info(‘打开网页成功!‘) except TimeoutException: Log().error(‘打开%s title错误,超时‘ % url) except Exception as msg: Log().error(‘打开网页产生的其他错误:%s‘ % msg) def find_element(self, locator, timeout=5): """重写元素定位方法""" try: element = WebDriverWait(self.driver, timeout, 1).until(EC.presence_of_element_located(locator)) y = element.is_displayed() if y: return element except: # Log().info(‘%s页面中未能找到元素%s‘ % (self, locator)) return "" def find_elements(self, locator, timeout=5): """定位一组元素""" elements = WebDriverWait(self.driver, timeout, 1).until(EC.presence_of_all_elements_located(locator)) return elements def click_coordinate(self, coordinate, timeout=10): """点击坐标""" self.driver.tap(coordinate, timeout) def clicks(self, locator, n): """点击一组元组中的一个""" element = self.find_elements(locator)[n] element.click() def click(self, locator, timeout=5): """点击操作""" element = self.find_element(locator, timeout=timeout) element.click() def send_keys(self, locator, text): """发送文本,清空后输入""" element = self.find_element(locator) element.clear() element.send_keys(text) def is_text_in_element(self, locator, text, timeout=5): """判断文本在元素里,没定位到元素返回False,定位到返回判断结果布尔值""" try: result = WebDriverWait(self.driver, timeout, 1).until(EC.text_to_be_present_in_element(locator, text)) Log().info(‘is_text_in_element 成功‘) return result except TimeoutException: Log().error(‘%s元素没有定位到‘ % str(locator)) return False def is_text_in_value(self, locator, value, timeout=5): """判断元素的value值,没有定位到返回False,定位到返回判断结果布尔值""" try: result = WebDriverWait(self.driver, timeout, 1).until( EC.text_to_be_present_in_element_value(locator, value)) except TimeoutException: Log().error(‘元素没有定位到:%s‘ % str(locator)) return False else: return result def is_title(self, title, timeout=5): """判断title完全等于""" result = WebDriverWait(self.driver, timeout, 1).until(EC.title_is(title)) return result def is_title_contains(self, title, timeout=5): """判断title包含""" result = WebDriverWait(self.driver, timeout, 1).until(EC.title_contains(title)) return result def is_selected(self, locator, timeout=5): """判断元素被选中,返回布尔值, 一般用在下拉框""" result = WebDriverWait(self.driver, timeout, 1).until(EC.element_located_to_be_selected(locator)) return result def is_selected_be(self, locator, selected=True, timeout=5): """判断元素的状态,selected是期望的参数True/False,返回布尔值""" result = WebDriverWait(self.driver, timeout, 1).until( EC.element_located_selection_state_to_be(locator, selected)) return result def is_alert_present(self, timeout=5): """判断页面是否有alert,有返回alert,没有返回False""" result = WebDriverWait(self.driver, timeout, 1).until((EC.alert_is_present())) text = EC.alert_is_present()(self.driver) if text: Log().info(‘alert弹框显示文本是:%s‘ % text.text) else: Log().warning(‘没有发现alert弹框。‘) return result def is_visibility(self, locator, timeout=5): """元素可见返回本身,不可见返回False""" result = WebDriverWait(self.driver, timeout, 1).until(EC.visibility_of_element_located(locator)) return result def is_invisibility(self, locator, timeout=5): """元素可见返回本身,不可见返回True,没有找到元素也返回True""" result = WebDriverWait(self.driver, timeout, 1).until(EC.invisibility_of_element_located(locator)) return result def is_clickAble(self, locator, timeout=5): """元素可以点击返回本身,不可点击返回False""" result = WebDriverWait(self.driver, timeout, 1).until(EC.element_to_be_clickable(locator)) return result def is_locator(self, locator, timeout=10): """判断元素有没有被定位到(并不意味着可见),定位到返回element,没有定位到返回False""" result = WebDriverWait(self.driver, timeout, 1).until(EC.presence_of_element_located(locator)) return result def move_to_element(self, locator): """鼠标悬停操作""" element = self.find_element(locator) ActionChains(self.driver).move_to_element(element).perform() def move(self, locator, locator1): """循环调用鼠标事件,死循环""" self.move_to_element(locator) time.sleep(2) element = self.find_element(locator1) self.move_to_element(locator1) try: if element.is_displayed: self.click(locator1) else: self.move(locator, locator1) except ElementNotVisibleException as e: Log().error(‘鼠标点击事件失败:%s‘ % e) def switch_frame(self, frame): """切换ifarm""" try: self.driver.switch_to_frame(self.find_element(frame)) Log().info(‘切换iframe成功!‘) except: Log().warning(‘没有发现iframe元素%s‘ % frame) def back(self): """返回之前的网页""" self.driver.back() def forward(self): """前往下一个网页""" self.driver.forward() def close(self): """关闭当前网页""" self.driver.close() def quit(self): """关闭所有网页""" self.driver.quit() def get_title(self): """获取title""" return self.driver.title def get_texts(self, locator, n): """获取一组相同元素中的指定文本""" element = self.find_elements(locator)[n] if element: return element.text def get_text(self, locator): """获取文本""" element = self.find_element(locator) if element: return element.text def get_attribute(self, locator, name): """获取属性""" element = self.find_element(locator) if element: return element.get_attribute(name) def js_execute(self, js): """执行js""" return self.driver.execute_script(js) def js_focus_element(self, locator): """聚焦元素""" target = self.find_element(locator) self.driver.execute_script("arguments[0].scrollIntoView();", target) def js_scroll_top(self): """滚动到顶部""" js = "window.scrollTo(0,document.body.scrollHeight)" self.driver.execute_script(js) def select_by_index(self, locator, index): """通过索引,index是第几个,从0开始, 下拉框""" element = self.find_element(locator) Select(element).select_by_index(index) def select_by_value(self, locator, value): """通过value属性""" element = self.find_element(locator) Select(element).select_by_value(value) def select_by_text(self, locator, text): """通过text属性""" element = self.find_element(locator) Select(element).select_by_visible_text(text) def save_screenshot(self, img_path): """获取电脑屏幕截屏""" self.driver.save_screenshot(img_path) def save_report_html(self): """可以在html报告中使用的截图""" self.driver.get_screenshot_as_base64() def save_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) def get_cookies(self): """获取cookies""" return self.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 in range(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 in range(n): self.driver.swipe(x1, y1, x1, y2, t) if __name__ == ‘__main__‘: driver = open_app()
原文地址:https://www.cnblogs.com/changqing8023/p/10153156.html
时间: 2024-11-10 14:14:03