WebUI automation testing framework based on Selenium and unittest.
Web UI test automation framework based on selenium and the unittest.
Feature
- Default CSS positioning, while supporting multiple positioning methods (id \ name \ class \ link_text \ xpath \ css).
- Selenium based on the second package, easier to use.
- Provide scaffolding, to quickly generate test automation project.
- Automatic generation / reports / directory, and HTML test report generation.
- Own assertions, assertions title, URL and text.
- Example parameterized with the support.
installation
> pip install -U git+https://github.com/defnngj/pyse.git@master
pyse command
1, see Help:
> pyse -h
usage: pyse [-h] [-V] [--startproject STARTPROJECT] [-r R]
WebUI automation testing framework based on Selenium.
optional arguments:
-h, --help show this help message and exit
-V, --version show version
--startproject STARTPROJECT
Specify new project name.
-r R run test case
2, create a project:
>pyse --startproject mypro
2019-11-07 00:24:57,783 - INFO - Start to create new test project: mypro
2019-11-07 00:24:57,784 - INFO - CWD: D:\
2019-11-07 00:24:57,785 - INFO - created folder: mypro
2019-11-07 00:24:57,786 - INFO - created folder: mypro\test_dir
2019-11-07 00:24:57,787 - INFO - created folder: mypro\reports
2019-11-07 00:24:57,788 - INFO - created file: mypro\test_dir\test_sample.py
2019-11-07 00:24:57,789 - INFO - created file: mypro\run.py
3, run the project:
> cd mypro\
> pyse -r run.py
Python 3.7.1
______ __ __ _______ _______
| _ || | | || _____|| ____|
| |_| || |_| || |_____ | |____
| ___||_ _||_____ || ____|
| | | | _____| || |____
|__| |___| |_______||_______|
generated html file: file:///D:\mypro\reports\2019_11_12_22_28_53_result.html
.1
4. View Report
You can go to the mypro\reports\
directory view the test report.
API Documents
simple demo
See the test_sample.py
file
import pyse
class BaiduTest(pyse.TestCase):
def test_baidu(self):
''' baidu search key : pyse '''
self.open("https://www.baidu.com/")
self.type("#kw", "pyse")
self.click("#su")
self.assertTitle("pyse_百度搜索")
if __name__ == '__main__':
pyse.main("test_sample.py", debug=True)
Description:
- Create a test class must inherit
pyse.TestCase
. - Test case file must be named
test
at the beginning. - Positioning elements using CSS syntax default mode
#kw
, you can also use the displaycss=>#kw
. - pyse encapsulates
assertTitle
,assertUrl
andassertText
other assertions. - By
main()
running test case method.
API
pyse provided API
self.accept_alert()
self.clear("css=>#el")
self.click("css=>#el")
self.click_text("新闻")
self.dismiss_alert()
self.double_click("css=>#el")
self.drag_and_drop("css=>#el","css=>#ta")
self.get_alert_text()
self.get_attribute("css=>#el","type")
self.get_display("css=>#el")
self.get_text("css=>#el")
self.get_title()
self.get_url()
self.js("window.scrollTo(200,1000);")
self.max_window()
self.move_to_element("css=>#el")
self.open("https://www.baidu.com")
self.open_new_window("link_text=>注册")
self.close()
self.quit()
self.refresh()
self.right_click("css=>#el")
self.screenshots('/Screenshots/foo.png')
self.select("#nr", '20')
self.select("xpath=>//[@name='NR']", '20')
self.set_window(wide, high)
self.submit("css=>#el")
self.switch_to_frame("css=>#el")
self.switch_to_frame_out()
self.type("css=>#el","selenium")
self.wait(10)
main () method
pyse.main(path="./",
browser="chrome",
title="百度测试用例",
description="测试环境:Firefox",
debug=True
)
Description:
- path: Specifies the test directory.
- browser: Specifies the test browser, the default Chrome.
- title: Specifies the test project title.
- description: Specifies the test description.
- debug: debug mode, set to True without generating HTML test test.
Run the test
pyse.main(path="./") # 当前目录下的所有测试文件
pyse.main(path="./test_dir/") # 指定目录下的所有测试文件
pyse.main(path="./test_dir/test_sample.py") # 指定目录下的测试文件
pyse.main(path="test_sample.py") # 指定当前目录下的测试文件
Description:
- If the specified directory, test files must
test
begin with. - If you want to run the file in the subdirectory must be added in a subdirectory
__init__.py
file.
Supported browsers and drive
If you want to specify test cases to run in different browsers, it is very simple, only need to pyse.main()
pass the method browser
set.
if __name__ == '__main__':
pyse.main(browser="firefox")
Supported browsers "chrome"
include: "firefox"
, "ie"
, "opera"
, "edge"
, , "chrome_headless"
and so on.
Different browsers driver download address:
geckodriver(Firefox):https://github.com/mozilla/geckodriver/releases
Chromedriver(Chrome):https://sites.google.com/a/chromium.org/chromedriver/home
IEDriverServer(IE):http://selenium-release.storage.googleapis.com/index.html
operadriver(Opera):https://github.com/operasoftware/operachromiumdriver/releases
MicrosoftWebDriver(Edge):https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver
==========================================================
Positioning elements
pyse support a variety of targeting methods, id, name, class, link text, xpath and css. The positioning method and positioning content integration, and wrote more concise.
<form id="form" class="fm" action="/s" name="f">
<span class="bg s_ipt_wr quickdelete-wrap">
<input id="kw" class="s_ipt" name="wd">
Positioning mode (recommended CSS):
# 默认支持CSS语法
self.type(".s_ipt","pyse") #css
self.type("#su","pyse") #css
# id
self.type("id=>kw", "pyse") #id
# class name
self.type("class=>s_ipt", "pyse") #class定位
# name
self.type("name=>wd", "pyse") #name
# xpath
self.type("xpath=>//*[@class='s_ipt']","pyse") #xpath
self.type("xpath=>//*[@id='kw']","pyse") #xpath
# link text
self.click_text("新闻") #link text (点击百度首页上的"新闻"链接)
css selector Reference Manual:
http://www.w3school.com.cn/cssref/css_selectors.asp
Parameterized test case
pyse supports parametric test integrated Parameterized .
import pyse
from pyse import ddt
class BaiduTest(pyse.TestCase):
@ddt.data([
(1, 'pyse'),
(2, 'selenium'),
(3, 'unittest'),
])
def test_baidu(self, name, keyword):
"""
used parameterized test
:param name: case name
:param search_key: search keyword
"""
self.open("https://www.baidu.com")
self.clear("id=>kw")
self.type("id=>kw", keyword)
self.click("css=>#su")
self.assertTitle(keyword)