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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
__author__ = "zhouzhikai"
from robot.api import TestSuite from robot.api import ResultWriter from robot.model import keyword from robot.api.deco import not_keyword,keyword import sys from robot.api import ExecutionResult, ResultVisitor
class ExecutionTimeChecker(ResultVisitor): ''' 对执行时间的控制 '''
def __init__(self, max_seconds): self.max_milliseconds = max_seconds * 1000
def visit_test(self, test): if test.status == 'PASS' and test.elapsedtime > self.max_milliseconds: test.status = 'FAIL' test.message = 'Test execution took too long.'
class BaiduSearchTest: def __init__(self, name, librarys=["SeleniumLibrary"]):
self.suite = TestSuite(name)
for lib in librarys: self.suite.resource.imports.library(lib)
def create_variables(self): variables = { "${baidu}": "http://www.baidu.com", "${browser}": "Chrome", "${searchWord}": "搭建自动化测试平台", "${search_input}": "id=kw", "${search_btn}": "id=su"}
for k, v in variables.items(): self.suite.resource.variables.create(k, v)
def open_browsers(self): test_01 = self.suite.tests.create("启动浏览器") test_01.body.create_keyword("Open Browser", args=["${baidu}", "${browser}"]) test_01.body.create_keyword("Title Should Be", args=["百度一下,你就知道"])
def search_word(self): test_02 = self.suite.tests.create("百度搜索测试") test_02.body.create_keyword("Input Text", args=["${search_input}", "${searchWord}"]) test_02.body.create_keyword("Click Button", args=["${search_btn}"]) test_02.body.create_keyword("Sleep", args=["5s"])
def assert_title(self): test_03 = self.suite.tests.create("断言验证搜索结果标题") test_03.body.create_keyword("Title Should Be", args=["百度搜索"])
def close_browsers(self): test_04 = self.suite.tests.create("关闭浏览器") test_04.body.create_keyword("Close All Browsers")
def run(self): self.create_variables() self.open_browsers() self.search_word() self.assert_title() self.close_browsers()
result = self.suite.run(output="output.xml") result.visit(ExecutionTimeChecker(float(10))) result.save()
if __name__ == "__main__": suite = BaiduSearchTest("百度搜索测试套件") suite.run()
|