当前位置: 首页 > news >正文

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

selenium and appium 使用文档

Page 和 Element

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

# page层封装
from poium import Page, Element


class BaiduPage(Page):
    input = Element(id_="kw", describe="搜索输入框")
    button = Element(id_="su", describe="搜索按钮")
  1. 创建页面类继承Page类。
  2. 通过Element类定义元素。

Element 类参数

Element类提供了几个参数。

from poium import Page, Element


class 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, Elements


class 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, Element


class 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, Element


class 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" this
    would select an option like:

    <option value="foo">Bar</option>

    :Args:
    - value - The value to match against

    throws 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 an
    element, and not merely by counting.

    :Args:
    - index - The option at this index will be selected

    throws 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" this
    would select an option like:

    <option value="foo">Bar</option>

    :Args:
    - text - The visible text to match against

    throws NoSuchElementException If there is no option with specisied text in SELECT
"""
page.elem.select_by_visible_text(text="text")
  • appium 扩展操作
from poium import Page, Element


class 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, Element


class 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, CSSElement


class BaiduIndexPage(Page):
    elem = CSSElement('#kw')
    elem2 = CSSElement('#su')

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

CSSElement类提供的操作

from poium import Page, CSSElement


class 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()

相关文章:

  • MySQL增删改查操作 -- CRUD
  • BMP180中文手册解读:在 BOOSTXL-SENSHUB 开发板中的应用解析
  • 程序化广告行业(10/89):价值、发展阶段与未来展望
  • PA2203A IntegraVision 功率分析仪
  • HOT100——链表篇Leetcode160. 相交链表
  • 基于SpringBoot+Vue的在线自主学习平台设计与实现
  • SSM基础专项复习6——Spring框架AOP(3)
  • Appium高级操作--ActionChains类、Toast元素识别、Hybrid App操作、手机系统API的操作
  • 【前端动态列表渲染:如何正确管理唯一标识符(Key)?】
  • 前端面试:axios 是否可以取消请求?
  • Linux下部署前后端分离项目
  • Java Class类文件结构
  • 如何在 PostgreSQL 中运行 TLS 回归测试
  • 电子电气架构 --- 智能电动汽车的品牌竞争转变
  • JVM之Arthas的jvm命令
  • OpenCV(应用) —— 凸包检测的实战应用
  • 新鲜速递:OpenAI-Agents-Python:构建智能代理系统的轻量级框架
  • Blueprint —— 蓝图的使用
  • c++之STL库
  • .net 6程序在IIS中部署后点击IIS设置报错“执行此操作时出错”
  • 哈马斯同意释放剩余所有以色列方面被扣押人员,以换取停火五年
  • 著名统计学家、北京工业大学应用数理学院首任院长王松桂逝世
  • 解放军仪仗司礼大队参加越南纪念南方解放50周年庆典活动
  • 商务部就海外社交平台上发布从所谓中国“代工厂”低价购买国际知名品牌事答问
  • 蚂蚁集团将向全体股东分红
  • 长三角数智文化产业基金意向签约会成功举办