From 5254e826aca827bdd33dd82c1b6accf9e684c9b9 Mon Sep 17 00:00:00 2001 From: Joo Date: Wed, 25 Oct 2017 13:57:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96selenium=20basePageUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/commonTool/configUtil.py | 20 ++++++++--- Util/seleniumTool/__init__.py | 1 + Util/seleniumTool/basePageUtil.py | 57 +++++++++++++++++++++--------- Util/seleniumTool/loginPageUtil.py | 17 +++++---- 4 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Util/commonTool/configUtil.py b/Util/commonTool/configUtil.py index 20089aa..e1f06f9 100644 --- a/Util/commonTool/configUtil.py +++ b/Util/commonTool/configUtil.py @@ -19,15 +19,27 @@ def GetPath(module=None): class ConfigUtil(object): @classmethod - def get(cls, section, option, path='/config/test.yml'): + def getall(cls, path='/config/test.yml'): + """获取配置文件中的配置,返回string""" + filepath = ROOT + path + return yaml.load(file(filepath, 'r')) + + @classmethod + def get(cls, section, option='', path='/config/test.yml'): """获取配置文件中的配置,返回string""" filepath = ROOT + path config = yaml.load(file(filepath, 'r')) - return str(config[section][option]) + if option: + return str(config[section][option]) + else: + return str(config[section]) @classmethod - def getint(cls, section, option, path='/config/test.yml'): + def getint(cls, section, option='', path='/config/test.yml'): """获取配置文件中的配置,返回int""" filepath = ROOT + path config = yaml.load(file(filepath, 'r')) - return int(config[section][option]) + if option: + return int(config[section][option]) + else: + return int(config[section]) diff --git a/Util/seleniumTool/__init__.py b/Util/seleniumTool/__init__.py index 04d0f61..782d22c 100644 --- a/Util/seleniumTool/__init__.py +++ b/Util/seleniumTool/__init__.py @@ -1,3 +1,4 @@ # coding=utf-8 from basePageUtil import * from loginPageUtil import * +from indexPageUtil import * \ No newline at end of file diff --git a/Util/seleniumTool/basePageUtil.py b/Util/seleniumTool/basePageUtil.py index ebacc8e..3fbabbf 100644 --- a/Util/seleniumTool/basePageUtil.py +++ b/Util/seleniumTool/basePageUtil.py @@ -9,6 +9,7 @@ from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver import ActionChains class basePage(object): @@ -20,10 +21,12 @@ class basePage(object): #__init__方法不能有返回值,只能返回None # self只实例本身,相较于类Page而言。 - def __init__(self, selenium_driver, url, pagetitle): + def __init__(self, selenium_driver, url, pagetitle='',downloadir='',pageurl=''): self.driver = selenium_driver self.url = url self.pagetitle = pagetitle + self.downloadir = downloadir + self.pageurl=pageurl # 通过title断言进入的页面是否正确。 # 使用title获取当前窗口title,检查输入的title是否在当前title中,返回比较结果(True 或 False) @@ -32,31 +35,47 @@ def on_page(self, pagetitle): # 打开页面,并校验页面链接是否加载正确 # 以单下划线_开头的方法,在使用import *时,该方法不会被导入,保证该方法为类私有的。 - def _open(self, url, pagetitle): + def _open(self, url, pagetitle='',pageurl=''): # 使用get打开访问链接地址 self.driver.get(url) self.driver.maximize_window() - # 使用assert进行校验,打开的窗口title是否与配置的title一致。调用on_page()方法 - assert self.on_page(pagetitle), "Open Page Error:\t%s" % url + print self.driver.title,self.driver.current_url + if pagetitle: + # 使用assert进行校验,打开的窗口title是否与配置的title一致。调用on_page()方法 + assert self.on_page(pagetitle), "Check Page Error:\t%s" % url + if pageurl: + # 校验打开后的url与传入url是否一致 + assert pageurl==self.driver.current_url,'{0}!={1}'.format(pageurl,self.driver.current_url) # 定义open方法,调用_open()进行打开链接 def open(self): - self._open(self.url, self.pagetitle) + self._open(self.url, self.pagetitle,self.pageurl) # 重写元素定位方法 - def find_element(self, *loc): - # return self.driver.find_element(*loc) + def find_element(self, loc): try: - # 确保元素是可见的。 - WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc)) - return self.driver.find_element(*loc) + # 等待元素可见 + return WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located(loc)) + except Exception as e: + elements = WebDriverWait(self.driver, 5).until(EC.visibility_of_any_elements_located(loc)) + return elements[0] if elements else False + + # 重写元素定位方法 + def find_elements(self, loc): + try: + # 等待元素可见 + return WebDriverWait(self.driver, 5).until(EC.visibility_of_any_elements_located(loc)) except BaseException: - print '%s page does not have "%s" locator' % (self, loc) + print 'page {0} does not have locator {1}'.format(self, loc) # 重写switch_frame方法 def switch_frame(self, loc): return self.driver.switch_to_frame(loc) + # 重写switch_frame方法 + def switch_window(self, loc): + return self.driver.switch_to_window(loc) + # 定义script方法,用于执行js脚本 def script(self, src): self.driver.execute_script(src) @@ -64,13 +83,19 @@ def script(self, src): # 重写定义send_keys方法 def send_keys(self, loc, vaule, clear_first=True, click_first=True): try: - # getattr相当于实现self.loc - loc = getattr(self, "_%s" % loc) if click_first: - self.find_element(*loc).click() + self.find_element(loc).click() if clear_first: - self.find_element(*loc).clear() + self.find_element(loc).clear() - self.find_element(*loc).send_keys(vaule) + self.find_element(loc).send_keys(vaule) except AttributeError: print '%s page does not have "%s" locator' % (self, loc) + + # 重写鼠标悬停方法 + def move_to_element(self, element='', loc=''): + if loc: + element = self.find_element(loc) + elif not element: + assert False,'Not Found Element' + ActionChains(self.driver).move_to_element(element).perform() diff --git a/Util/seleniumTool/loginPageUtil.py b/Util/seleniumTool/loginPageUtil.py index ceb1841..eb772ab 100644 --- a/Util/seleniumTool/loginPageUtil.py +++ b/Util/seleniumTool/loginPageUtil.py @@ -28,22 +28,20 @@ def open(self): # 输入用户名:调用send_keys对象,输入用户名 def input_username(self, username): - self.find_element(*self.username_loc).clear() - self.find_element(*self.username_loc).send_keys(username) + self.send_keys(self.username_loc,username,clear_first=True) # 输入密码:调用send_keys对象,输入密码 def input_password(self, password): - self.find_element(*self.password_loc).clear() - self.find_element(*self.password_loc).send_keys(password) + self.send_keys(self.password_loc,password,clear_first=True) # 点击登录:调用send_keys对象,点击登录 def click_submit(self): - self.find_element(*self.submit_loc).click() + self.find_element(self.submit_loc).click() # 用户名或密码不合理是Tip框内容展示 def show_span(self): try: - tips = self.find_element(*self.span_loc) + tips = self.find_element(self.span_loc) if tips: return tips.text else: @@ -53,12 +51,13 @@ def show_span(self): # 切换登录模式为动态密码登录(IE下有效) def swich_DynPw(self): - self.find_element(*self.dynpw_loc).click() + self.find_element(self.dynpw_loc).click() # 登录成功页面中的用户ID查找 def show_userid(self): - return self.find_element(*self.userid_loc).text + return self.find_element(self.userid_loc).text # 登陆退出:调用send_keys对象,点击退出 def click_logout(self): - self.find_element(*self.logout_loc).click() + self.find_element(self.logout_loc).click() +