1.安装
python -m pip install poium
或:https://github.com/defnngj/poium
如果报如下错误:
更新pip和 setuptools:
python -m pip install --upgrade pip
python -m pip install --upgrade setuptools
再安装poium即可成功
2.以百度为例:
import unittest from poium import Page, PageElement from selenium import webdriver class BaiduPage(Page): """百度Page层,百度页面封装操作到的元素""" search_input = PageElement(id_="wd") search_button = PageElement(id_='su') class TestBaidu(unittest.TestCase): """百度搜索测试用例""" @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() def test_baidu_search_case1(self): page = BaiduPage(self.driver) page.get("https://www.baidu.com") page.search_input = "selenium" page.search_button.click() def test_baidu_search_case2(self): page = BaiduPage(self.driver) page.get("https://www.baidu.com") page.search_input = "unittest" page.search_button.click() def test_baidu_search_case3(self): page = BaiduPage(self.driver) page.get("https://www.baidu.com") page.search_input = "page object" page.search_button.click() @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == '__main': unittest.main(verbosity=2)
执行结果为: