Knitter 测试框架使用教程
TIP
本文档假设您已具有使用 Python 语言以及 Selenium 库的基础知识。
环境
Python 3
Windows/Linux 都支持
安装
> pip install knitter
示范项目
示范项目所测试的页面:http://sleepycat.org/knitter/hobby.html
克隆示范项目到本地
> git clone https://github.com/hw712/hobby.git
详细测试代码位于子目录
hobby/
中。hobby/page/
存放页面控件hobby/testcase/
存放测试用例hobby/conf.py
存放配置文件。
1. 添加页面控件
原则上,一个页面对应一个 Python 文件。复杂情况下可以自行分组。
每个页面控件对应一个 class
,可以通过嵌套 class 的方式进行分组,但最里面的必须继承 WebElement
。
WebElement
类封装了所有的控件操作方法,如: Click()
, Set()
,等。
# hobby/page/Hobby.py
from knitter.webelement import WebElement
from selenium.webdriver.common.by import By
class User:
class Title(WebElement):
(by, value) = (By.ID, 'title')
class Name(WebElement):
(by, value) = (By.ID, 'name')
class SubmitButton(WebElement):
(by, value) = (By.XPATH, '//button[@onclick="do_submit();"]')
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2. 添加测试用例
基于上一步添加的页面及控件,在 hobby/testcase/
中添加测试用例。
可将不同的测试用例分组,不同的分组对应不同的 Python 文件模块。
可以导入
knitter.webelement.WebBrowser
进行一些浏览器方面的操作,如点击页面的 Alert,改变 URL,刷新页面,等待几秒钟,等等。
# hobby/testcase/module1_inputs.py
from hobby.page import Hobby
def TestCase01_UserInformation():
Hobby.User.Title.WaitForAppearing()
Hobby.User.Title.VerifyEnabled(True)
Hobby.User.Title.VerifyVisible(True)
Hobby.User.Title.Select("Mrs.")
Hobby.User.Title.VerifyAttribute("value", "Mrs.")
Hobby.User.Title.SelectByOrder(3)
Hobby.User.Title.VerifyAttribute("value", "Ms.")
Hobby.User.Title.VerifyAttribute("value", "Dr.", assertion="not equal")
Hobby.User.Name.Set("Super Man")
Hobby.User.Name.VerifyAttribute("value", "Super", assertion="contain")
Hobby.User.Name.VerifyAttribute("value", "More", assertion="not contain")
Hobby.User.Name.VerifyAttribute("value", "Here Comes Super Man!!", assertion="in")
def TestCase02_Gender():
Hobby.Gender.Male.VerifyAttribute("checked", None)
Hobby.Gender.Female.VerifyAttribute("checked", None)
Hobby.Gender.Male.Click()
Hobby.Gender.Male.VerifyAttribute("checked", "true")
Hobby.Gender.Female.VerifyAttribute("checked", None)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
3. 设置配置文件
每一个单独的环境配置,对应一个单独的函数。多人协作,或者多个环境测试时,在 hobby/conf.py
中定义多个函数即可。
可以指定多个浏览器,只需要在 Browser.AvailableBrowsers
中添加即可。但目前只支持 Chrome, FireFox, IE 三个。
General.Path.Result
是指生成结果报告的位置,亦可使用绝对路径。
# hobby/conf.py
from knitter.configure import Browser, General
def windows():
# Chrome
Browser.Chrome.Driver = "driver/chromedriver.exe"
Browser.AvailableBrowsers.append(Browser.Chrome)
# FireFox
Browser.FireFox.Driver = "driver/geckodriver.exe"
Browser.AvailableBrowsers.append(Browser.FireFox)
# Only for Chrome. If True, will just run in memory.
Browser.HeadlessMode = False
# URL
Browser.StartURL = "http://sleepycat.org/knitter/hobby.html"
# Result
General.Path.Result = "result"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
4. 运行测试
executor.run()
的第一个参数为上一步定义的配置函数。后面动态多个参数为需要运行的测试 module 或测试函数。
注意:
在将函数或模块作为参数传递时,请勿带 ()
,因为此处传递的是函数的引用。
from knitter import executor
from hobby import conf
from hobby.testcase import module1_inputs, module2_combine
executor.run(conf.windows, module1_inputs, module2_combine)
"""
You can run your specified modules or test cases or their combinations, just append them as the second or next parameters.
For Example:
executor.run(conf.windows, module1_inputs.TestCase01_UserInformation)
Or:
executor.run(conf.windows, module1_inputs.TestCase02_Gender, module2_combine)
"""
2
3
4
5
6
7
8
9
10
11
12
13
14
15
5. 检查报告
报告以 HTML 的格式生成在 conf.py 中配置的 General.Path.Result
目录中。
通过浏览器打开 index.html 查看整体报告。
点击某测试用例可查看每一步的详细日志。如果用例失败了,可以查看失败时的截屏。
6. WebElement 方法详解
在测试用例中可以看到 WebElement
所封装的方法。这些方法可以覆盖绝大部分测试需求。这里列出其基本介绍。
Click()
点击 element。
GetAttribute(attr)
获取控件某属性的值,如:
html = Hobby.Result.GetAttribute("innerHTML")
VerifyAttribute(attribute, value, assertion='equal')
就控件的某个属性与参数
value
作对比。对比条件为参数assertion
。若结果不符则记录当前用例为失败。assertion
可用值:in
属于equal
相等contain
包含not equal
不相等not contain
不包含
举例:
Hobby.User.Name.Set("Super Man")
Hobby.User.Name.VerifyAttribute("value", "Super", assertion="contain")
Hobby.User.Name.VerifyAttribute("value", "More", assertion="not contain")
Hobby.User.Name.VerifyAttribute("value", "Here Comes Super Man!!", assertion="in")
2
3
4
Select(value)
下拉列表选择。一般是
select/option
或ul/li
控件。SelectByOrder(order)
按顺序号选择。
SelectByPartText(value)
选第一个包含此字符串的选项。
IsEnabled()
如果控件是 enabled,则返回 True, 否则返回 False。
IsAttribute(attribue, value, assertion="contain")
类似于
VerifyAttribute()
,返回属性是否符合条件的 Boolean 值。WaitForAttribute(attribue, value, assertion="contain")
类似于
VerifyAttribute()
,等待属性满足符合的条件。如果到了超时时间(一般为 90 秒)依然未达到条件,则记当前用例为失败。
其他的方法与以上都有类似相通,细节可查看 knitter.webelement.WebElement
的源代码查询。