zoukankan      html  css  js  c++  java
  • Python3+RobotFramework自动化测试九:用Python写Roboot Framework测试

    下面还是以百度搜索为例
    创建robot.py文件,内容如下:
    from robot.api import TestSuite
    from robot.api import ResultWriter
    from robot.model import Keyword
     
    # 百度搜索测试
    class BaiduSearchTest:
        def __init__(self,name,librarys=["SeleniumLibrary"]):
            # 创建测试套件
            self.suite = TestSuite(name)
            
            # 导入seleniumLibrary
            for lib in librarys:
                self.suite.resource.imports.library(lib)
     
        # 定义变量
        def create_variables(self):
            variables = {
                "${baidu}": "https://www.baidu.com",            
                "${browser}": "Chrome",
                "${search_input}": "id=kw",
                "${search_btn}": "id=su"
            }
     
            for x,y in variables.items():
                self.suite.resource.variables.create(x,y)
     
        # 测试用例:启动浏览器
        def open_browsers(self):
            test_01=self.suite.tests.create("启动浏览器")
            test_01.keywords.create("Open_Browser", args=["${baidu}","${browser}"])
            test_01.keywords.create("Title Should Be", args=["百度一下,你就知道"])
     
        # 测试用例:百度搜索测试
        def search_word(self):
            test_02 = self.suite.tests.create("百度搜索测试")
            test_02.keywords.create("Input Text", args=["${search_input}","测试教程网"])
            test_02.keywords.create("Click Button", args=["${search_btn}"])
            test_02.keywords.create("Sleep", args=["5s"])
     
        # 测试用例:断言验证搜索结果标题
        def assert_title(self):
            test_03 = self.suite.tests.create("断言验证搜索结果标题")
            test_03.keywords.create("Title Should Be",
                args=["测试教程网_百度搜索"])
     
        # 测试用例:关闭测试用例
        def close_browsers(self):
            test_04 = self.suite.tests.create("关闭浏览器")
            test_04.keywords.create("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(critical="百度搜索",
                output="output.xml")
     
            # 生成日志、报告文件
            ResultWriter(result).write_results(
               report="report.html", log="log.html")
     
     
    if __name__ == "__main__":
        print("用Python写Robot Framework测试")
        suite = BaiduSearchTest("百度搜索测试套件")
        suite.run()
    进入到项目目录下,执行
    python robot.py
     
    运行结果:
     
  • 相关阅读:
    ssh访问控制,多次失败登录即封掉IP,防止暴力破解
    经常用到的一些命令行
    自定义控件
    委托线程三部曲(引用)
    关于委托
    三个调用的例子(转)
    同一网段的两台电脑通信(转)
    SOCKET原理(转载)
    C#winform和百度API互动-----之JS读取中C#中的函数
    C#winform和百度API互动-----之读取中js的参数
  • 原文地址:https://www.cnblogs.com/daydayup-lin/p/12923020.html
Copyright © 2011-2022 走看看