Skip to content

Commit

Permalink
优化selenium basePageUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
lovesoo committed Oct 25, 2017
1 parent f7f1faa commit 5254e82
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
20 changes: 16 additions & 4 deletions Util/commonTool/configUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
1 change: 1 addition & 0 deletions Util/seleniumTool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding=utf-8
from basePageUtil import *
from loginPageUtil import *
from indexPageUtil import *
57 changes: 41 additions & 16 deletions Util/seleniumTool/basePageUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -32,45 +35,67 @@ 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)

# 重写定义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()
17 changes: 8 additions & 9 deletions Util/seleniumTool/loginPageUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

0 comments on commit 5254e82

Please sign in to comment.