poium库简介
poium库是基于selenium、appium的Page Objects设计模式的库,同时支持selenium和appium,同时也通过javaScript扩展了selenium API
poium支持的定位
LOCATOR_LIST = {
# selenium
'css': By.CSS_SELECTOR,
'id_': By.ID,
'name': By.NAME,
'xpath': By.XPATH,
'link_text': By.LINK_TEXT,
'partial_link_text': By.PARTIAL_LINK_TEXT,
'tag': By.TAG_NAME,
'class_name': By.CLASS_NAME,
# appium
'ios_uiautomation': MobileBy.IOS_UIAUTOMATION,
'ios_predicate': MobileBy.IOS_PREDICATE,
'ios_class_chain': MobileBy.IOS_CLASS_CHAIN,
'android_uiautomator': MobileBy.ANDROID_UIAUTOMATOR,
'android_viewtag': MobileBy.ANDROID_VIEWTAG,
'android_data_matcher': MobileBy.ANDROID_DATA_MATCHER,
'android_view_matcher': MobileBy.ANDROID_VIEW_MATCHER,
'windows_uiautomation': MobileBy.WINDOWS_UI_AUTOMATION,
'accessibility_id': MobileBy.ACCESSIBILITY_ID,
'image': MobileBy.IMAGE,
'custom': MobileBy.CUSTOM,
}
同时也提供了一套javaScript封装的API,在poium库的一个类(CSSElement)中,并且只支持css定位【如何使用CSS定位】
class CSSElement(object):
"""
Only CSS selectors are supported.
Please see help: http://www.w3school.com.cn/cssref/css_selectors.asp
>> from page_objects import Page, CSSElements
>> class MyPage(Page):
input = CSSElements('.s_ipt')
button = CSSElements('#su')
"""
poium库使用详解
poium库中常用的两个类NewPageElement、CSSElement
NewPageElement:可用的定位方式有id、xpath、css、name、class_name等等
下图部分NewPageElement源码中,NewPageElement类初始化参数中包含timeout、index、describe,
设置timeout我们可减少在脚本中使用等待延时
设置index,默认为0,即第一个,当我们定位到一组相同元素时,也可根据需求选择索引
设置describe,没有实际作用,只是增加脚本的可读性
class NewPageElement(object):
"""
new Page element class
"""
def __init__(self, timeout=5, describe="undefined", index=0, **kwargs):
self.timeout = timeout
self.index = index
self.desc = describe
if not kwargs:
raise ValueError("Please specify a locator")
if len(kwargs) > 1:
raise ValueError("Please specify only one locator")
self.kwargs = kwargs
self.k, self.v = next(iter(kwargs.items()))
if self.k not in LOCATOR_LIST.keys():
raise FindElementTypesError("Element positioning of type '{}' is not supported.".format(self.k))
CSSElement:js操作浏览器,目前只支持css定位
下图部分CSSElement源码,同样初始化参数也包含了index、describe,从源码中click方法可以知道,其实操作的也是js脚本
class CSSElement(object):
"""
Only CSS selectors are supported.
Please see help: http://www.w3school.com.cn/cssref/css_selectors.asp
>> from page_objects import Page, CSSElements
>> class MyPage(Page):
input = CSSElements('.s_ipt')
button = CSSElements('#su')
"""
driver = None
def __init__(self, css, index=None, describe=None):
self.css = css
if index is None:
self.index = "0"
else:
self.index = str(index)
self.desc = describe
def __get__(self, instance, owner):
if instance is None:
return None
global driver
driver = instance.driver
return self
def click(self):
"""
JavaScript API, Only support css positioning
Click element.
"""
logging.info("Element of the current operation: {desc}".format(desc=self.desc))
js = """var elm = document.querySelectorAll("{css}")[{index}];
elm.style.border="2px solid red";
elm.click();""".format(css=self.css, index=self.index)
driver.execute_script(js)
应用场景
NewPageElement应用场景
下面是我们对[设置分享标题]、[获取分享标题]这两个动作封装在H5_manage类中,使用了NewPageElement中的css定位,还使用了NewPageElement类中的获取元素属性的方法get_attribute
from poium import Page, NewPageElement
class H5_Manage(Page):
share_title = NewPageElement(css='input[class="control"][type="text"]', describe='设置作品分享标题')
def set_sharetitle(self, title):
"""设置分享标题"""
self.share_title.select_all()
self.share_title.delete()
self.share_title.send_keys(title)
def get_sharetitle(self, attribute):
"""获取分享标题"""
title = self.share_title.get_attribute(attribute)
return title
CSSElement应用场景
下面是我们对一个[复制作品短链]这个动作封装在H5_manage类中,
一般假设在一个页面中,我需要去滑动屏幕之类的操作,直接用CSSElement定位-点击此元素是最方便不过的事了,
就减少了还需要先滑动屏幕到该元素在页面可见再去点击
from poium import Page, CSSElement
class H5_Manage(Page):
copy_btn = CSSElement(css="button.input-btn", describe="复制作品短链接")
def copy_short_url(self):
"""复制作品短链接"""
self.copy_btn.click()
poium库中常用方法
NewPageElement类
send_keys:输入内容
click:点击
text:获取文本
get_attribute:获取元素属性
switch_to_frame:切换iframe
move_to_element:移动到元素中间
select_all:全选
delete:删除
copy:复制
paste:粘贴
CSSElement类
click:点击
display:显示隐藏元素
click_display:点击显示的元素
Page类
get_title:获取当前标题
get_url:获取当前url
switch_to_parent_frame:切换到父窗口
new_window_handle:获取最新窗口
current_window_handle:返回当前窗口句柄
window_handles:返回当前所有会话窗口句柄
switch_to_window:切换到指定窗口