跳转到主要内容

实现页面对象模式的一种好方法。

项目描述

PyPI PyPI - Python Version GitHub Updates Build status https://codecov.io/gh/jsfehler/stere/branch/master/graph/badge.svg?token=C1vfu8YgWn https://saucelabs.com/buildstatus/jsfehler

Stere是一个用于编写页面对象的库,旨在在现有自动化库之上工作。

设计理念

许多页面对象模型的实现都侧重于在测试中去除元素定位符的重复。Stere则更进一步,提供了一个完整的自动化驱动代码包装器。

本项目的目标包括:

1 - 消除测试函数中的实现代码。测试应该像行为描述一样读取,而不是Selenium命令。

2 - 减少在页面对象中手动编写辅助方法的需求。常见操作应具有通用的解决方案。

3 - 提供一种简单的模式来编写可维护的页面对象。

项目本身没有直接构建自动化功能;它完全依赖于连接到其他库。然而,使用SplinterAppium的实现是现成的。

文档

https://stere.readthedocs.io/en/latest/

基本用法

本质上,页面对象只是一个Python类。

最小化Stere页面对象应

1 - 继承Page类

2 - 在__init__方法中声明字段和区域

以下是一个维基百科主页的示例

from stere import Page
from stere.areas import Area, RepeatingArea
from stere.fields import Button, Input, Link, Root, Text


class WikipediaHome(Page):
    def __init__(self):
        self.search_form = Area(
            query=Input('id', 'searchInput'),
            submit=Button('xpath', '//*[@id="search-form"]/fieldset/button')
        )

        self.other_projects = RepeatingArea(
            root=Root('xpath', '//*[@class="other-project"]'),
            title=Link('xpath', '//*[@class="other-project-title"]'),
            tagline=Text('xpath', '//*[@class="other-project-tagline"]')
        )

搜索表单被表示为一个包含两个区域字段

字段代表单个项目,而区域代表一组唯一的字段。

查询和提交字段不需要放在区域内部。但是,这样做可以让你使用区域的 perform() 方法。

其他产品的链接表示为 RepeatingArea。RepeatingArea 表示页面上的非唯一字段集合。使用根参数作为非唯一选择器,RepeatingArea 将找到所有该根实例,然后构建适当数量的区域,其中包含所有其他字段。

将每个其他产品分别声明为单独的区域也是完全有效的,如下所示

self.commons = Area(
    root=Root('xpath', '//*[@class="other-project"][1]'),
    title=Link('xpath', '//*[@class="other-project-title"]'),
    tagline=Text('xpath', '//*[@class="other-project-tagline"]')
)

self.wikivoyage = Area(
    root=Root('xpath', '//*[@class="other-project"][2]'),
    title=Link('xpath', '//*[@class="other-project-title"]'),
    tagline=Text('xpath', '//*[@class="other-project-tagline"]')
)

你选择哪种样式完全取决于你如何建模页面。RepeatingArea 在区域数量和/或区域内容不可预测的集合中效果最佳,例如库存列表。

在测试中使用页面对象可以这样进行

def test_search_wikipedia():
    home = WikipediaHome()
    home.search_form.perform('kittens')

许可协议

根据 MIT 许可证分发,"Stere" 是免费的开源软件

问题

如果你遇到任何问题,请提供详细描述并 提交问题

谢谢

由 Sauce Labs 提供,跨浏览器测试平台和开源 <3

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

stere-0.31.0.tar.gz (24.7 kB 查看哈希值)

上传日期

构建分布

stere-0.31.0-py3-none-any.whl (35.0 kB 查看哈希值)

上传日期 Python 3

由以下支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面