欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > UI自动化:poium测试库使用文档

UI自动化:poium测试库使用文档

2025/3/14 18:53:22 来源:https://blog.csdn.net/qq_33594579/article/details/146230815  浏览:    关键词:UI自动化:poium测试库使用文档

selenium and appium 使用文档

Page 和 Element

通过Page类和 Element类实现Page层元素层的定义。

# page层封装
from poium import Page, Elementclass BaiduPage(Page):input = Element(id_="kw", describe="搜索输入框")button = Element(id_="su", describe="搜索按钮")
  1. 创建页面类继承Page类。
  2. 通过Element类定义元素。

Element 类参数

Element类提供了几个参数。

from poium import Page, Elementclass BaiduPage(Page):# **kwargs 定位input1 = Element(id_="kw", timeout=3, index=0, describe="搜索输入框")button1 = Element(css="#su", timeout=3, index=0, describe="搜索按钮")# selector 定位input2 = Element("id=kw", timeout=3, index=0, describe="搜索输入框")button2 = Element("#su", timeout=3, index=0, describe="搜索按钮")
  • selector/ **kwgrer: 支持所有Selenium/appium的定位方式
  • timeout: 设置超时检查次数,默认为5
  • index: 设置元素索引,当你的定位方式默认匹配到多个元素时,默认返回第1个,即为0.
  • describe: 设置元素描述,建议为每个元素增加描述,方便维护。

poium 1.3.0 引入新的 selector 定位,弱化了selenium/appium 的定位类型方式。

  • **kwargs 和 selector 定位对比。
类型定位**kwargsselector
selenium/appiumidid_="id""id=id"
seleniummamename="name""name=name"
selenium/appiumclassclass_name="class""class=class"
seleniumtagtag="input""tag=input"
seleniumlink_textlink_text="文字链接""text=文字链接"
seleniumpartial_link_textpartial_link_text="文字链""text~=文字链"
selenium/appiumxpathxpath="//*[@id='11']""//*[@id='11']"
seleniumcsscas="input#id""input#id"
appiumios_predicateios_predicate = "xx""ios_predicate=xx"
appiumios_class_chainios_class_chain = "xx""ios_predicate=xx"
appiumandroid_uiautomatorandroid_uiautomator = "xx""android_uiautomator=xx"
appiumandroid_viewtagandroid_viewtag = "xx""android_viewtag=xx"
appiumandroid_data_matcherandroid_data_matcher = "xx""android_data_matcher=xx"
appiumandroid_view_matcherandroid_view_matcher = "xx""android_view_matcher=xx"
appiumaccessibility_idaccessibility_id = "xx""accessibility_id=xx"
appiumimageimage = "xx""image=xx"
appiumcustomcustom = "xx""custom=xx"

Elements类

如果需要poium返回的是一组元素对象,可以使用Elements类。

from poium import Page, Element, Elementsclass BaiduPage(Page):input = Element(id_="kw", describe="搜索输入框")button = Element(id_="su", describe="搜索按钮")results = Elements(xpath="//div/h3/a", describe="搜索结果")  # 返回一组元素

Element类元素操作方法

当我们定位到一个元素之后,Elements 类除了提供click点击和send_keys输入,还可以丰富的操作。

  • selenium 常规操作
from poium import Page, Elementclass MyPage(Page):elem = Element("id=xx")page = MyPage(driver="your selenium driver!!")""""element is existed """
page.elem.is_exist()"""Clears the text if it's a text entry element."""
page.elem.clear()"""
Simulates typing into the element.
If clear_before is True, it will clear the content before typing.
"""
page.elem.send_keys("value", clear_before=False)"""Clicks the element."""
page.elem.click()"""Submits a form."""
page.elem.submit()"""This element's ``tagName`` property."""
tag = page.elem.tag_name"""Clears the text if it's a text entry element."""
text = page.elem.text"""The size of the element."""
size = page.elem.size"""
Gets the given property of the element.
"""
page.elem.get_property("name")"""Gets the given attribute or property of the element."""
page.elem.get_attribute("name")"""Whether the element is visible to a user."""
page.elem.is_displayed()"""
Returns whether the element is selected.
Can be used to check if a checkbox or radio button is selected.
"""
page.elem.is_selected()"""Returns whether the element is enabled."""
page.elem.is_enabled()"""
selenium API
Switches focus to the specified frame
"""
page.elem.switch_to_frame()"""
selenium API
Moving the mouse to the middle of an element
"""
page.elem.move_to_element()"""
selenium API
Holds down the left mouse button on an element.
"""
page.elem.click_and_hold()"""
selenium API
Holds down the left mouse button on an element.
"""
page.elem.double_click()"""
selenium API
Performs a context-click (right click) on an element.
"""
page.elem.context_click()"""
selenium API
Holds down the left mouse button on the source element,then moves to the target offset and releases the mouse button.
:param x: X offset to move to.
:param y: Y offset to move to.
"""
page.elem.drag_and_drop_by_offset(x=500, y=800)"""
selenium API
Refreshes the current page, retrieve elements.
"""
page.elem.refresh_element(timeout=10)
  • Select 下拉框操作
from poium import Page, Elementclass MyPage(Page):elem = Element("#id")page = MyPage(driver="your selenium driver!!")"""
selenium API
Select all options that have a value matching the argument. That is, when given "foo" thiswould select an option like:<option value="foo">Bar</option>:Args:- value - The value to match againstthrows NoSuchElementException If there is no option with specisied value in SELECT
"""
page.elem.select_by_value(value="value")"""
selenium API
Select the option at the given index. This is done by examing the "index" attribute of anelement, and not merely by counting.:Args:- index - The option at this index will be selectedthrows NoSuchElementException If there is no option with specisied index in SELECT
"""
page.elem.select_by_index(index=1)"""
selenium API
Select all options that display text matching the argument. That is, when given "Bar" thiswould select an option like:<option value="foo">Bar</option>:Args:- text - The visible text to match againstthrows NoSuchElementException If there is no option with specisied text in SELECT
"""
page.elem.select_by_visible_text(text="text")
  • appium 扩展操作
from poium import Page, Elementclass MyPage(Page):elem = Element("#id")page = MyPage(driver="your appium driver")"""
appium API
Gets the location of an element relative to the view.
Returns:dict: The location of an element relative to the view
"""
page.elem.location_in_view
  • 模拟键盘操作
from poium import Page, Elementclass MyPage(Page):elem = Element("#id")page = MyPage(driver="your appium driver!!")page.elem.input(text="")page.elem.enter()page.elem.select_all()page.elem.cut()page.elem.copy()page.elem.paste()page.elem.backspace()page.elem.delete()page.elem.tab()page.elem.space()

CSSElement类

有时我们需要借助JavaScript实现一些特殊的操作。poium提供了CSSElement类,已经帮你封装了这些操作。

from poium import Page, CSSElementclass BaiduIndexPage(Page):elem = CSSElement('#kw')elem2 = CSSElement('#su')

注:CSSElement类不需要指定定位方式,仅支持CSS定位。

CSSElement类提供的操作

from poium import Page, CSSElementclass MyPage(Page):elem = CSSElement("#id")page = MyPage(driver="your selenium driver!!")"""
Clears the text if it's a text entry element, Only support css positioning
"""
page.elem.clear()"""
Simulates typing into the element.
:param value: input text
"""
page.elem.set_text(value="value")"""
Click element.
"""
page.elem.click()"""
Click on the displayed element, otherwise skip it.
"""
page.elem.click_display()"""
Display hidden elements
"""
page.elem.display()"""
Remove element attribute, Only support css positioning
:param attribute:
"""
page.elem.remove_attribute(attribute="attribute")"""
Setting element attribute, Only support css positioning
:param attribute:
:param value:
"""
page.elem.set_attribute(attribute="attribute", value="value")"""
Clear element styles.
"""
page.elem.clear_style()"""
Clear element class
"""
page.elem.clear_class()"""
The innerText property sets the text content of the specified element, Only support css positioning
:param text: Inserted text
"""
page.elem.inner_text(text="text")"""
Remove a node from the child node list
:param child: child of the child node
"""
page.elem.remove_child(child=0)"""
Click the parent element of the element
"""
page.elem.click_parent()"""
scroll the div element on the page
"""
page.elem.scroll(top=0, left=0)"""
Move the mouse over the element
"""
page.elem.move_to()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词