poium

Apache License 2.0
python
mac
虫师 · August 05, 2019 · 1019 次阅读 · 1 条评论

poium

基于 selenium/appium 的 Page Objects 设计模式测试库。

  • 极大的简化了Page层的元素定义。
  • 同时支持selenium和appium
  • 通过JavaScript扩展了selenium API
  • 对原生 API 无损

Installation

pip install:

> pip install poium

If you want to keep up with the latest version, you can install with github repository url:

> pip install -U git+https://github.com/defnngj/poium.git@master

Sample

支持Selenium的例子。

from poium import Page, PageElement
from selenium import webdriver


class BaiduIndexPage(Page):
search_input = PageElement(name='wd')
search_button = PageElement(id_='su')


driver = webdriver.Chrome()

page = BaiduIndexPage(driver)
page.get("https://www.baidu.com")

page.search_input = "poium"
page.search_button.click()

driver.quit()

还提供了一套JavaScript封装的API。

from poium import Page, CSSElement
from selenium import webdriver


class BaiduIndexPage(Page):
search_input = CSSElement('#kw')
search_button = CSSElement('#su')


driver = webdriver.Chrome()

page = BaiduIndexPage(driver)
page.get("https://www.baidu.com")

page.search_input.set_text("poium")
page.search_button.click()

driver.quit()

支持appium的例子。

from poium import Page, PageElement
from appium import webdriver

class CalculatorPage(Page):
number_1 = PageElement(id_="com.android.calculator2:id/digit_1")
number_2 = PageElement(id_="com.android.calculator2:id/digit_2")
add = PageElement(id_="com.android.calculator2:id/op_add")
eq = PageElement(id_="com.android.calculator2:id/eq")

# APP定义运行环境
desired_caps = {
'deviceName': 'Android Emulator',
'automationName': 'appium',
'platformName': 'Android',
'platformVersion': '7.0',
'appPackage': 'com.android.calculator2',
'appActivity': '.Calculator',
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

page = CalculatorPage(driver)
page.number_1.click()
page.add.click()
page.number_2.click()
page.eq.click()

driver.quit()

使用poium将元素 定位操作 分离,这将会非常有助于规模化自动化测试用例的编写与维护。

评论列表
dajuntest 发表于 October 20, 2019

在你的基础上我再结合 生产者模式来进行一些页面基本的操作的封装,比如点击和send_keys:
这样好像还能省下不少代码:

class BaiduIndexPage(Page):

search_input = PageElement(css="#kw", describe="搜索框")
search_button = PageElement(css="#su", describe="搜索按钮")

class CPCLoginPage(Page):
login = PageElement(id_='id', describe='登录')
pass

class ToutolPage(BaiduIndexPage, CPCLoginPage):
pass

class WebAction(object):

page = ToutolPage(webdriver.Chrome())

@classmethod
def with_click(cls, click_element):
if hasattr(cls.page, click_element):
getattr(cls.page, click_element).click()
return cls

@classmethod
def with_input(cls):
cls.page.search_input.send_keys('poium')
return cls

@classmethod
def with_open(cls):
cls.page.get('https://www.baidu.com')
return cls

if name == 'main':
weba = WebAction()
weba.with_open().with_input().with_click('search_button')