如何从机器人框架中的不同文件导入功能?

How to import functions from different files in robot framework?

我正在使用 Robot Framework 和 Selenium2Library 编写自动化测试脚本来测试我们的 Web 应用程序(in .txt 格式)。

机器人框架中是否有任何选项可以导入其他文件中编写的函数/模块?

例如 :

我已经编写了将近 300 个测试用例来测试我们的 Web 应用程序。所有测试用例都包含一个用于登录验证的通用函数。

如果我必须对这个特定的 (logIn) 函数进行小的更改,我已经检查了每个测试用例并进行了必要的更改。

这真是一个耗时的过程。

所以,我只想知道,机器人框架中是否有任何选项可以以模块化方式编写测试用例?

就像,如果我为登录验证编写一个“login.txt”函数 - 机器人框架中是否有任何选项可以将此特定函数导入任何其他测试用例?

是否有像 'Import Function' / 'Import Module' 这样的关键字可用于此特定场景?

您可以在 resource files and libraries 中放置共享关键字。资源文件几乎与测试用例文件完全一样,只是它们没有测试用例。库包含写在 python.

中的关键字

例如,您可以创建一个名为 login.txt 并带有关键字 "Log In To My App" 的文件,如下所示:

# login.txt
*** Keywords ***
Log In To My App
    <your code to log in to the app>

然后您可以将其包含在您的其他测试用例中,如下所示:

# test_suite_1.txt
*** Settings ***
Resource     login.txt
Suite setup  log in to my app

# test_suite_2.txt
*** Settings ***
Resource     login.txt
Suite setup  log in to my app

基本语法

 *** Settings ***
Library    OperatingSystem
Library    my.package.TestLibrary
Library    MyLibrary    arg1    arg2
Library    PythonLibrary.py
Library    /absolute/path/JavaLibrary.java
Library    relative/path/PythonDirLib/    possible    arguments
Library    ${RESOURCES}/Example.class

与大多数其他数据不同,库名称区分大小写和 space。 如果库在包中,则必须使用包含包名称的全名。

另外,正如您所问,我们也可以通过关键字从测试用例中导入库 "Import Library"。 例如:

*** Test Case ***
My Testcase
     Open Browser   https://facebook.com
    Import Library     My Library
    My Library.Login     ##Keyword from My Library