robotframwork快速开始

介绍

Robot Framework是一个基于Python的自动化测试框架,它可以帮助测试人员更方便、高效地进行自动化测试。

这个框架的主要特点是“关键字驱动”,也就是说,测试人员不需要自己编写复杂的测试代码,而是可以使用预定义的关键字来编写测试用例。这些关键字实际上是一些操作或功能的描述,测试人员只需要根据测试需求选择合适的关键字组合,就可以完成测试工作。

Robot Framework支持多种类型的测试,包括接口测试、GUI界面测试、数据库测试、APP测试等。测试人员可以根据需要选择相应的测试库,或者使用Python或Java来扩展测试功能。同时,Robot Framework也提供了丰富的测试库和工具,使得测试工作更加便捷。

总的来说,Robot Framework通过关键字驱动的方式,简化了自动化测试的流程,提高了测试效率。对于不懂编程的测试人员来说,也可以轻松上手,参与到自动化测试中来。

一些推荐视频以及文档

【RobotFrameWork做Web自动化测试】11-执行脚手架_哔哩哔哩_bilibili

使用python自定义关键字 · 语雀

robot_framework的robot语法与python脚本之间的语法转换-CSDN博客

语法

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

示例

*** Settings ***
Documentation   This is a sample test suite.
Library         SeleniumLibrary
Suite Setup     Open Browser    https://www.example.com    chrome
Suite Teardown  Close Browser

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

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

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

一,下载

pip install robotframework     
若要检查安装是否成功,请运行
robot --version

pycharm配置robot插件 (可不安装,插件辅助写代码更容易)

打开【File】-【Settings】

安装对应的插件 搜索RobotFramework Support

 

二,简单的使用案例

1,操控谷歌浏览器打开百度
使用依赖库SeleniumLibrary

SeleniumLibrary的github地址

https://github.com/robotframework/SeleniumLibrary/

SeleniumLibrary关键字文档

https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html

下载
pip install --upgrade robotframework-seleniumlibrary
下载chrome  

Google Chrome 网络浏览器  

 

查看浏览器的版本  

 

找到自己浏览器的版本信息

记录下当前浏览器的版本号。

下载驱动

可以从淘宝镜像上下载: CNPM Binaries Mirror

下载的时候根据自己的浏览器版本下载对应的驱动

 

最新版本谷歌浏览器驱动可访问Chrome for Testing availability下载

将驱动放在环境变量  

 

 

因为安装python的时候已经将python放在环境变量中了,所以现在将chromedriver直接放在python的安装路径即可。  

 目录以及文件创建

 

baidu.robot  

*** Settings ***
Library           SeleniumLibrary

*** Test Cases ***
打开百度
    Open Browser    https://www.baidu.com/    browser=chrome
运行
robot .\test_cases\baidu.robot

在Robot Framework中,你可以通过命令行参数来设置日志的输出位置。当你运行Robot Framework测试用例时,可以使用--output--log--report--xunit等参数来分别指定输出文件、日志文件、报告文件和xUnit兼容的结果文件的路径。  

robot --output ./output/output.xml --log ./log/log.html --report ./report/report.html --xunit ./xunit/xunit.xml .\test_cases\baidu.robot
运行优化

通过python去运行robot

创建一个run.py文件

 

#run.py代码
import argparse
import subprocess
import os


def run_robot_tests(test_file, output_dir, log_dir, report_dir, xunit_dir):
    # 构建命令行参数
    cmd = [
        'robot',
        '--output', os.path.join(output_dir, 'output.xml'),
        '--log', os.path.join(log_dir, 'log.html'),
        '--report', os.path.join(report_dir, 'report.html'),
        '--xunit', os.path.join(xunit_dir, 'xunit.xml'),
        test_file
    ]

    # 执行Robot Framework测试
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError as e:
        print(f"Robot Framework tests failed with exit code {e.returncode}")
        return e.returncode
    else:
        print("Robot Framework tests executed successfully.")
        return 0


if __name__ == '__main__':
    # 解析命令行参数
    parser = argparse.ArgumentParser(description='Run Robot Framework tests with custom output directories.')
    parser.add_argument('test_file', type=str, help='Path to the Robot Framework test file (.robot)')
    parser.add_argument('--output-dir', type=str, default='./output',
                        help='Directory for output files (default: ./output)')
    parser.add_argument('--log-dir', type=str, default='./log', help='Directory for log files (default: ./log)')
    parser.add_argument('--report-dir', type=str, default='./report',
                        help='Directory for report files (default: ./report)')
    parser.add_argument('--xunit-dir', type=str, default='./xunit',
                        help='Directory for xUnit compatible result files (default: ./xunit)')
    args = parser.parse_args()

    # 确保输出目录存在
    for dir_name in [args.output_dir, args.log_dir, args.report_dir, args.xunit_dir]:
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)

            # 运行Robot Framework测试
    exit_code = run_robot_tests(args.test_file, args.output_dir, args.log_dir, args.report_dir, args.xunit_dir)

    # 根据退出码退出脚本
    exit(exit_code)

 运行

传递目录会运行整个目录下的.robot文件
python run.py ./test_cases

传递单个.robot文件会运行单个.robot文件
python run.py ./test_cases/baidu.robot

--output-dir;--log-dir;--report-dir;--xunit-dir参数指定输出文件、日志文件、报告文件和xUnit兼容的结果文件的路径。
python main.py ./test_cases --output-dir ./output --log-dir ./log --report-dir ./report --xunit-dir ./xunit

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值