简单Web UI 自动化测试框架 seldom

2025-04-27 0 141

pyse 更名为 seldom

简单Web UI 自动化测试框架 seldom

WebUI automation testing framework based on Selenium and unittest.

基于 selenium 和 unittest 的 Web UI自动化测试框架。

特点

  • 提供更加简单API编写自动化测试。
  • 提供脚手架,快速生成自动化测试项目。
  • 自动生成HTML测试报告生成。
  • 自带断言方法,断言title、URL 和 text。
  • 支持用例参数化。
  • 支持用例失败重跑。
  • 用例失败/错误截图。
安装
> pip install seldom

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/seldom.git@master
Quick Start

1、查看帮助:


  1. > seldom -h

  2. usage: seldom [-h] [-V] [--startproject STARTPROJECT] [-r R]

  3. WebUI automation testing framework based on Selenium.

  4. optional arguments:

  5. -h, --help show this help message and exit

  6. -V, --version show version

  7. --startproject STARTPROJECT

  8. Specify new project name.

  9. -r R run test case

2、创建项目:

>seldom --startproject mypro

3、目录结构:


  1. mypro/

  2. ├── test_dir/

  3. │ ├── test_sample.py

  4. ├── report/

  5. └── run.py

  • test_dir/目录实现用例编写。
  • report/ 目录存放生成的测试报告。
  • run.py 文件运行测试用例。

3、运行项目:


  1. > seldom -r run.py

  2. Python 3.7.1

  3. _ _

  4. | | | |

  5. ___ ___ | | __| | ___ _ __ ___

  6. / __| / _ \\| | / _` | / _ \\ | '_ ` _ \\

  7. \\__ \\| __/| || (_| || (_) || | | | | |

  8. |___/ \\___||_| \\__,_| \\___/ |_| |_| |_|

  9. -----------------------------------------

  10. @itest.info

  11. generated html file: file:///D:\\mypro\\reports\\2019_11_12_22_28_53_result.html

  12. .1

4、查看报告

你可以到 mypro\\reports\\ 目录查看测试报告

简单Web UI 自动化测试框架 seldom

API Documents

simple demo

请查看 demo/test_sample.py 文件


  1. import seldom

  2. class YouTest(seldom.TestCase):

  3. def test_case(self):

  4. """a simple test case """

  5. self.open("https://www.baidu.com")

  6. self.type(id_="kw", text="seldom")

  7. self.click(css="#su")

  8. self.assertTitle("seldom")

  9. if __name__ == '__main__':

  10. seldom.main("test_sample.py")

说明:

  • 创建测试类必须继承 seldom.TestCase
  • 测试用例文件命名必须以 test 开头。
  • seldom的封装了assertTitleassertUrl 和 assertText等断言方法。
main() 方法

  1. import seldom

  2. # ...

  3. if __name__ == '__main__':

  4. seldom.main(path="./",

  5. browser="chrome",

  6. title="百度测试用例",

  7. description="测试环境:chrome",

  8. debug=False,

  9. rerun=0,

  10. save_last_run=False

  11. )

说明:

  • path : 指定测试目录或文件。
  • browser: 指定测试浏览器,默认Chrome
  • title : 指定测试报告标题。
  • description : 指定测试报告描述。
  • debug : debug模式,设置为True不生成测试HTML测试,默认为False
  • rerun : 设置失败重新运行次数,默认为 0
  • save_last_run : 设置只保存最后一次的结果,默认为False
Run the test

  1. import seldom

  2. seldom.main(path="./") # 当前目录下的所有测试文件

  3. seldom.main(path="./test_dir/") # 指定目录下的所有测试文件

  4. seldom.main(path="./test_dir/test_sample.py") # 指定目录下的测试文件

  5. seldom.main(path="test_sample.py") # 指定当前目录下的测试文件

说明:

  • 如果指定的目录,测试文件必须以test 开头。
  • 如果要运行子目录下的文件,必须在子目录下加 __init__.py 文件。
支持的浏览器及驱动

如果你想指定测试用例在不同的浏览器中运行,非常简单,只需要在seldom.main()方法中通过browser 参数设置。


  1. import seldom

  2. if __name__ == '__main__':

  3. seldom.main(browser="chrome") # chrome浏览器,默认值

  4. seldom.main(browser="firefox") # firefox浏览器

  5. seldom.main(browser="ie") # IE浏览器

  6. seldom.main(browser="opera") # opera浏览器

  7. seldom.main(browser="edge") # edge浏览器

  8. seldom.main(browser="chrome_headless") # chrome浏览器headless模式

  9. seldom.main(browser="firefox_headless") # Firefox浏览器headless模式

不同浏览器驱动下载地址:

geckodriver(Firefox):Releases · mozilla/geckodriver · GitHub

Chromedriver(Chrome):https://sites.google.com/a/chromium.org/chromedriver/home

IEDriverServer(IE):http://selenium-release.storage.googleapis.com/index.html

operadriver(Opera):Releases · operasoftware/operachromiumdriver · GitHub

MicrosoftWebDriver(Edge):Microsoft Edge WebDriver – Microsoft Edge Developer

==========================================================

元素定位

  1. <form id="form" class="fm" action="/s" name="f">

  2. <span class="bg s_ipt_wr quickdelete-wrap">

  3. <input id="kw" class="s_ipt" name="wd">

定位方式:


  1. self.type(id_="kw", text="seldom")

  2. self.type(name="wd", text="seldom")

  3. self.type(class_name="s_ipt", text="seldom")

  4. self.type(tag="input", text="seldom")

  5. self.type(link_text="hao123", text="seldom")

  6. self.type(partial_link_text="hao", text="seldom")

  7. self.type(xpath="//input[@id='kw']", text="seldom")

  8. self.type(css="#kw", text="seldom")

参数化测试用例

seldom 支持参数化测试用例,集成了parameterized


  1. import seldom

  2. from seldom import ddt

  3. # ...

  4. class BaiduTest(seldom.TestCase):

  5. @ddt.data([

  6. (1, 'seldom'),

  7. (2, 'selenium'),

  8. (3, 'unittest'),

  9. ])

  10. def test_baidu(self, name, keyword):

  11. """

  12. used parameterized test

  13. :param name: case name

  14. :param keyword: search keyword

  15. """

  16. self.open("https://www.baidu.com")

  17. self.type(id_="kw", text=keyword)

  18. self.click(css="#su")

  19. self.assertTitle(search_key+"_百度搜索")

page objects 设计模式

seldom 支持Page objects设计模式,可以配合poium 使用。


  1. import seldom

  2. from poium import Page, PageElement

  3. class BaiduPage(Page):

  4. """baidu page"""

  5. search_input = PageElement(id_="kw")

  6. search_button = PageElement(id_="su")

  7. class BaiduTest(seldom.TestCase):

  8. """Baidu serach test case"""

  9. def test_case(self):

  10. """

  11. A simple test

  12. """

  13. page = BaiduPage(self.driver)

  14. page.get("https://www.baidu.com")

  15. page.search_input = "seldom"

  16. page.search_button.click()

  17. self.assertTitle("seldom_百度搜索")

  18. if __name__ == '__main__':

  19. seldom.main("test_po_demo.py")

AI写代码

poium提供了更多好用的功能,使Page层的创建更加简单。

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

简单Web UI 自动化测试框架 seldom

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

简单Web UI 自动化测试框架 seldom

平台声明:以上文章转载于\”《CSDN》\”,文章全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,仅作参考。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/kk_lzvvkpj/article/details/146420858

遇见资源网 前端 简单Web UI 自动化测试框架 seldom http://www.ox520.com/157284.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务