【问题标题】:How to pass variables from a Robot Test script to Python script如何将变量从机器人测试脚本传递到 Python 脚本
【发布时间】:2020-02-22 15:40:11
【问题描述】:

机器人测试

*** Settings ***
Variables   test_if_case_sensitive.py
Documentation    Suite description
Library     Process

*** Variables ***
${pw1}   "HeLLo1234"
${pw2}   "hello1234"
${test3}

*** Test Cases ***
Test1
    ${test1}=    set variable   ${passwordWithCaps}
    ${test2}=    set variable   ${passwordWithoutCaps}
    ${test1}=    set variable   ${pw1}
    ${test2}=    set variable   ${pw2}
    ${test3}=    set variable   ${message}
    Start Process   python3     test_if_case_sensitive.py
    Check If It Works
*** Keywords ***
Check If It Works
   PASS EXECUTION IF    ${test3} == "Passwords do not match"

这是python脚本

#test_if_case_sensitive.py
import socket
import string
import random

global passwordWithCaps
global passwordWithoutCaps
global message

def randomString(stringLength):
    """Generate a random string of fixed length """
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(stringLength))

def test_check_if_passwords_match_case_sensitive_when_creating_them(passwordWithCaps, passwordWithoutCaps):
    username = randomString(random.randint(10, 20))

    confirm_password = False
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    IP_address = "127.0.0.1"
    Port = 8081
    server.connect((IP_address, Port))

    while True:
        message = server.recv(2048).decode("utf-8")
        if message:
            print("Message 1" + message)
            if confirm_password:
                print("Message 2" + message)
                if message.__contains__("You have successfully registered"):
                    raise Exception("Passwords match when they shouldn't")
                break
            if message.__contains__("Please enter username"):
                server.send("--register".encode())
            if message.__contains__("Please enter a username to register"):
                server.send(username.encode())
            if message.__contains__("Please create a password"):
                server.send(passwordWithCaps.encode())
            if message.__contains__("Please confirm password"):
                confirm_password = True
                server.send(passwordWithoutCaps.encode())

            print(message)

test_check_if_passwords_match_case_sensitive_when_creating_them(passwordWithCaps, passwordWithoutCaps)

我正在尝试使用变量 ${pw1} 和 ${pw2} 从机器人测试脚本中分配全局变量 passwordWithCaps 和 passwordWithoutCaps。相反,我收到一条错误消息,提示未定义“passwordWithCaps”,这表明问题源于 python 脚本。但是,我在机器人测试中也得到了这个 -

Test1                                                                 | FAIL |
Variable '${passwordWithCaps}' not found.

【问题讨论】:

  • 您是否看过 Robot Framework User Guide 部分Creating Test Libraries,其中包含用于创建自定义 Python 库的实用 Pyhton 示例?

标签: python robotframework


【解决方案1】:

到目前为止,您的设计还行不通;您正在将变量文件的概念与库混合在一起。您错误地接近它的几点:

  • 变量文件在套件解析开始时进行评估;此时,Variables 部分仍未评估。当 RF/python 到达方法调用并尝试执行它时,它会传递 2 个它一无所知的变量 - 除了它们在全局范围内的事实。例如。在这一点上,除了保留它们的名称之外,这两个变量、它们的数据类型(如何使用它们)和它们的值(它们是什么)都是事实。
  • 变量文件的目的是在Robot Framework的范围内插入新的变量:值对;这发生在赋值运算符 (variable=value)、特殊函数 get_variables()、覆盖模块属性 __all__ 或使用类时(让我们不要到达那里)。您在 .py 文件中都没有这些。这可能会被global var 作用域规避(我从未尝试过),但它仍然需要在某个地方设置它的值。反过来——在变量文件范围中插入听起来很难(如果不是不可能的话,我不知道;它可能与 RF_var_the_name 一起工作)——在文件评估的那一刻,没有那么多设置变量,当然没有测试变量。
  • 在导入期间,变量文件只评估一次;即使这发生在稍后阶段 - 该测试功能也只会运行一次,因此它只能用于一种情况。
  • 您将变量${test3} 设置为等于message,即在函数中定义和使用的变量。但是该变量的范围是严格本地的,它只存在于函数中并且只存在于它的执行中;它永远不会泄漏(这是 python 的基本点)。
  • 调用Start Process 将创建一个新的python 进程,与运行该案例的进程完全隔离-它无法访问您的变量-并且passwordWithCaps & 另一个将是未定义的(因为-如果它们是,这本来是在运行一个)和执行失败的情况下。将来时,因为我们根本没有达到这一点,所以更早地失败了。
  • (个人) 全局变量在 python 中是不受欢迎的,这是一种危险(且笨拙)的通信机制。

那么你应该(恕我直言) 怎么办? “关注点分离” - 说“有一个功能可以在一个地方进行检查,而测试数据存储在不同的地方”的奇特方式:) 从 py 文件中删除全局变量声明和方法调用,并将其移至 Library 导入。现在,您将可以访问类似于名为“测试检查密码是否在创建密码时区分大小写”的关键字,您可以使用 any 数据进行输入。
将其最后的print() 语句更改为return message - 这样它就可以通过返回消息字符串与外界进行通信。

然后,在您的情况下,使用不同的值调用关键字:

${test3}=   test check if passwords match case sensitive when creating them    ${pw1}    ${pw2}
Should Be Equal    ${test3}    Passwords do not match

${test3}=   test check if passwords match case sensitive when creating them    samecase    samecase
Should Be Equal    ${test3}    Passwords do not match    # will supposedly fail

(顺便说一句,我没有看到这条消息“密码不匹配”从函数中退出,唯一打破循环的是服务器是否成功响应 - 你可能想要检查那里的流逻辑,对于这个否定测试。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多