0%

RobotFramework自动化框架(一)

RobotFramework自动化框架,基础教程整理。

robotframework本身可以用ride编辑写用例,但那交互真是太别扭了。
我宁愿手撸脚本。这里我是用pycharm写的,配置好后,写了个最基础的Web自动化脚本。

1
2
3
4
5
6
7
8
*** Test Cases ***
test01
[Documentation] 基础用例:打开网站,点击元素
Open Browser https://www.made-in-china.com/ firefox
Input Text name=word led
Click Element xpath = //button[@class='m-search-btn']
sleep 5
Close browser

其实robotframework还可以通过robot api来些python脚本。
同样的Web自动化脚本,还可以这样写。

1
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
32
33
34
35
36
37
38
39
40
41
#-*-coding:utf-8-*-
from robot.api import TestSuite,ResultWriter

class WebTest:
def __init__(self, name, librarys=["SeleniumLibrary"]):
# 创建测试套件,相当于关键字模式的Settings
self.suite = TestSuite(name)
# 导入SeleniumLibrary
for lib in librarys:
self.suite.resource.imports.library(lib)

# 测试用例:01
def search_word(self):
test_01 = self.suite.tests.create(u"基础用例:打开网站,点击元素")
test_01.keywords.create("Open Browser",
args=["https://www.made-in-china.com/", "firefox"])
test_01.keywords.create("Input Text",
args=["name=word","led"])
test_01.keywords.create("Click Element",
args=["xpath = //button[@class='m-search-btn']"])
test_01.keywords.create("sleep",
args=["5"])
test_01.keywords.create("Close browser")

# 运行
def run(self):
self.search_word()

# 运行套件
result = self.suite.run(critical=u"搜索测试",
output="./results/output.xml")

# 生成日志、报告文件
ResultWriter(result).write_results(
report="./results/report.html", log="./results/log.html")


if __name__ == "__main__":
print("用Python写Robot Framework测试")
suite = WebTest(u"搜索测试套件")
suite.run()