robot_framework的robot语法与python脚本之间的语法转换

Robot Framework是一个开源的自动化测试框架,支持关键字驱动和数据驱动的测试方法。它具有简单易学的语法和丰富的库,可以与多种语言进行集成,包括Python。

1. robot 的关键字

Robot Framework 是一个用于自动化测试和自动化任务的开源框架。它使用简洁的关键字驱动的语法,支持多种扩展库和插件,以及跨平台的运行环境。

以下是 Robot Framework 的常见语法和关键字含义的概述:

测试用例组织:

*** Settings ***:全局设置区块。
*** Variables ***:定义变量区块。
*** Test Cases ***:定义测试用例区块。
*** Keywords ***:定义关键字区块。

关键字定义:

Keyword Name:定义一个自定义关键字。
${var_name} 或者 @{list_name}:定义变量或列表。
Run Keyword:调用其他关键字执行。
Arguments:关键字的参数定义。

测试用例定义:

Test Case Name:定义一个测试用例。
Documentation:用于给测试用例添加文档说明。
Tags:为测试用例添加标签。
Setup:定义每个测试用例的前置条件。
Teardown:定义每个测试用例的后置条件。

断言和验证:

Should Be Equal:判断两个值是否相等。
Should Be True:判断给定的表达式的值是否为真。
Should Be False:判断给定的表达式的值是否为假。
Should Contain:判断一个字符串是否包含另一个字符串。
Should Match:使用正则表达式匹配字符串。

条件和循环:

Run Keyword If:根据条件判断是否执行关键字。
Run Keyword Unless:根据条件判断是否不执行关键字。
Run Keyword If All Critical Tests Passed:当所有关键测试通过时执行关键字。
Repeat Keyword:重复执行某个关键字。

文件和日志:

Create File:创建文件。
Delete File:删除文件。
Append To File:向文件中追加内容。
Log:输出日志信息。

以上仅是 Robot Framework 的一部分语法和关键字的示例,还有很多其他功能和扩展库可以使用。如果需要更详细的语法和关键字说明,建议查阅 Robot Framework 官方文档(https://robotframework.org/)。

关于使用 Python 代码语法转换为 Robot Framework 语法,你可以使用 Robot Framework 提供的 SeleniumLibrary、RequestsLibrary 等库进行自动化测试的编写。具体的代码案例可以参考这些库的官方文档和示例。

2.Robot Framework的语法包括以下几个部分:

  1. Settings(设置):配置测试环境和引入相关文件

    示例:

    *** Settings ***
    Documentation   This is a sample test suite.
    Library         SeleniumLibrary
    Suite Setup     Open Browser    https://www.example.com    chrome
    Suite Teardown  Close Browser
    
  2. Variables(变量):定义全局或局部变量,方便在测试中重复使用

    示例:

    *** Variables ***
    ${USERNAME}     example_user
    ${PASSWORD}     example_password
    
    *** Test Cases ***
    Valid Login
        Input Text      username_field    ${USERNAME}
        Input Password  password_field    ${PASSWORD}
        Click Button    login_button
    
  3. Test Cases(测试用例):定义具体的测试步骤和断言

    示例:

    *** Test Cases ***
    Login with valid credentials
        [Documentation]    This is a sample test case.
        [Tags]             Smoke
        Open Browser       https://www.example.com    chrome
        Input Text         username_field             ${USERNAME}
        Input Password     password_field             ${PASSWORD}
        Click Button       login_button
        Page Should Contain Element    welcome_message
    
    Logout
        [Documentation]    This is another sample test case.
        [Tags]             Smoke
        Log                 Logging out from the application.
        Click Element       logout_button
        Page Should Contain Element    login_button
    
  4. Keywords(关键字):定义可重复使用的测试步骤

    示例:

    *** Keywords ***
    Input Text
        [Arguments]      ${locator}    ${text}
        Input Text       ${locator}    ${text}
    
    Input Password
        [Arguments]      ${locator}    ${text}
        Input Password   ${locator}    ${text}
    
    Click Button
        [Arguments]      ${locator}
        Click Button     ${locator}
    
    Page Should Contain Element
        [Arguments]      ${locator}
        Page Should Contain Element      ${locator}
    
    Click Element
        [Arguments]      ${locator}
        Click Element      ${locator}
    

以上是Robot Framework的基本语法,可以根据具体需求进行扩展和定制。关于将Python的class和方法转换为Robot Framework的语法代码,可以按照以下方式操作:

  1. 将Python类转换为Robot Framework的库(Library)

    示例:

    class MyLibrary:
        ROBOT_LIBRARY_VERSION = 1.0
    
        def __init__(self):
            pass
    
        def keyword_one(self, arg1, arg2):
            # Python code here
            pass
    
        def keyword_two(self, arg1, arg2):
            # Python code here
            pass
    

    转换为Robot Framework的库:

    *** Settings ***
    Library    MyLibrary
    
    *** Test Cases ***
    Example Test Case
        Keyword One    arg1    arg2
        Keyword Two    arg1    arg2
    
  2. 将Python类中的方法转换为Robot Framework的自定义关键字

    示例:

    class MyLibrary:
        ROBOT_LIBRARY_VERSION = 1.0
    
        def __init__(self):
            pass
    
        def _internal_method(self):
            # Python code here
            pass
    
        def keyword_one(self, arg1, arg2):
            self._internal_method()
            # Python code here
            pass
    
        def keyword_two(self, arg1, arg2):
            self._internal_method()
            # Python code here
            pass
    

    转换为Robot Framework的关键字:

    *** Keywords ***
    Keyword One
        [Arguments]    ${arg1}    ${arg2}
        MyLibrary.Keyword One    ${arg1}    ${arg2}
    
    Keyword Two
        [Arguments]    ${arg1}    ${arg2}
        MyLibrary.Keyword Two    ${arg1}    ${arg2}
    
    *** Test Cases ***
    Example Test Case
        Keyword One    arg1    arg2
        Keyword Two    arg1    arg2
    

以上是将Python的类和方法转换为Robot Framework的语法代码案例。请根据具体需求进行调整和定制。

3. Robot关键字库是按照一定的分类进行的

  • Builtln
    变量定义、循环控制、数据计算、进制转换、断言判断、日志等等
  • Collections
    处理python列表和字典的相关操作
  • DateTime
    处理日期和时间转换的相关操作
  • Dialogs
    提供暂停执行和获取用户输入的方法
  • OperatingSystem
    提供和操作系统相关的关键字
  • Process
    操作进程相关的关键字,比如运行一个 .py脚本
    Invoke Python Script
    KaTeX parse error: Expected 'EOF', got '#' at position 47: … test.py #̲{CURDIR}
  • Remote
    特殊库充当 Robot Framework 和其他地方的库之间的代理。实际的库可以在不同的机器上运行, 并且可以使用任何支持 XML-RPC 协议的编程语言来实现
  • Screenshot
    提供用于截取桌面屏幕截图的关键字
  • String
    用于生成、修改和验证字符串的库
  • Telnet
    可以连接到 Telnet 服务器并在打开的连接上执行命令
  • XML
    用于生成、修改和验证 XML 文件的库

加粗样式疑问: robot自动化测试,一定要先使用python实现自动化脚本,才能转成robot脚本呢 可以不用写自动化脚本直接写robot脚本吗

不是必须要先使用Python实现自动化脚本,然后再转换成Robot脚本。事实上,你可以直接编写Robot Framework的脚本,而无需使用其他语言编写自动化脚本。

Robot Framework提供了一种简洁的、易于阅读和编写的语法,称为Robot语法。你可以直接使用Robot语法编写测试用例、关键字和测试套件,而无需事先编写自动化脚本。

使用Robot语法编写的脚本示例:

*** Test Cases ***
Example Test
    [Tags]  Example
    Open Browser  https://www.example.com  chrome
    Input Text  id=txtUsername  username
    Input Text  id=txtPassword  password
    Click Button  xpath=//input[@type='submit']
    Page Should Contain Text  Welcome, user!

*** Keywords ***
Open Browser
    [Arguments]  ${url}  ${browser}
    # 自动化步骤实现

Input Text
    [Arguments]  ${locator}  ${text}
    # 自动化步骤实现

Click Button
    [Arguments]  ${locator}
    # 自动化步骤实现

Page Should Contain Text
    [Arguments]  ${text}
    # 自动化断言实现

请注意,如果你需要使用Robot Framework中未提供的自定义关键字或库,那么你可能需要编写一些Python代码来支持这些功能。然而,这并不意味着你必须先实现所有的自动化脚本,然后再转换成Robot脚本。你可以根据需要在任何时候编写和使用Python代码。

总结来说,你可以直接使用Robot Framework的语法编写自动化测试脚本,不一定需要先使用Python实现自动化脚本再转成Robot脚本。

相关推荐

  1. vim脚本语言语法

    2024-01-12 16:48:04       38 阅读
  2. C语言中各进制之间转换

    2024-01-12 16:48:04       32 阅读
  3. C++C语言之间区别

    2024-01-12 16:48:04       46 阅读
  4. Python条件语句循环语句语法

    2024-01-12 16:48:04       43 阅读
  5. HalconC++之间数据转换

    2024-01-12 16:48:04       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 16:48:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 16:48:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 16:48:04       87 阅读
  4. Python语言-面向对象

    2024-01-12 16:48:04       96 阅读

热门阅读

  1. C# 获取文件信息大全

    2024-01-12 16:48:04       47 阅读
  2. Redis优化和解决缓存问题

    2024-01-12 16:48:04       53 阅读
  3. ECMAScript6详解

    2024-01-12 16:48:04       56 阅读
  4. 汽车出海业务专业术语

    2024-01-12 16:48:04       53 阅读
  5. 海外動態IP在價格監控方面的應用 - okey proxy

    2024-01-12 16:48:04       56 阅读
  6. R语言【base】——sample():随机取样和排列

    2024-01-12 16:48:04       49 阅读