繁体   English   中英

如何将变量从机器人测试脚本传递到 Python 脚本

[英]How to pass variables from a Robot Test script to Python script

机器人测试

*** 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.

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

  • 在套件解析开始时评估变量文件; 在这一点上, 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 现在,您将可以像关键字一样访问“在创建密码时测试检查密码是否区分大小写”,您可以使用任何数据提供该关键字。
将其最后一个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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM