我如何解决“NameError: global name 'x' is not defined"?使用自定义库运行机器人框架测试用例时出错? [英] How can I resolve "NameError: global name 'x' is not defined" error while running a Robot Framework testcase using a custom library?

查看:65
本文介绍了我如何解决“NameError: global name 'x' is not defined"?使用自定义库运行机器人框架测试用例时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到NameError: global name 'x' is not defined";在 Robot Framework 中运行测试用例时出错.

I am seeing the "NameError: global name 'x' is not defined" error while running a testcase in Robot Framework.

以下是我的自定义库文件(根据 Bryan Oakley 的评论修改):

Following is my custom library file (modified as per Bryan Oakley's comments):

import re

def pass_fail_criteria():
    if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+", x)[0]):
        return "pass"
    else:
        return "fail"

以下是pass_fail.robot"文件内容:

Following is the "pass_fail.robot" file contents:

*** Settings ***
Library         Selenium2Library
Library         SSHLibrary
Library         regexp_def.py
Suite Setup     Go to gmail page
Suite Teardown  Close All Browsers

*** Variables ***
${HOMEPAGE}     https://www.gmail.com/intl/en/mail/help/about.html
${BROWSER}      firefox
${LOGINPAGE}    https://www.gmail.com/intl/en/mail/help/about.html
${FINALURL}     https://mail.google.com/mail/
${FINALURL1}    https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/'

${HOST}         1.1.1.1
${USERNAME}     test
${PASSWORD}     test



*** Test Cases ***
Login into gmail
    Go to gmail page
    Login Page Should Be Open
    Click Signin Button
    Input Username        test@gmail.com
    Input Password        test@123
    Submit Credentials
    Inbox page should open

Check Deep Packet Inspection Stats
    Open Connection         ${HOST}
    enable ssh logging      XYZ
    Login    ${USERNAME}    ${PASSWORD}
    Write                   enable
    Write                   show dpi app stats gmail on AVC/ap7532-15E8CC
    ${x}                    Read Until Regexp   .*#


Pass fail Criteria
    ${status}               pass fail criteria
    should be equal         ${status}           pass
    ${result}               Pass fail criteria  ${x}

*** Keywords ***
Go to gmail page
    Open Browser    ${HOMEPAGE}     ${BROWSER}
    Maximize Browser Window

Login Page Should Be Open
    Location Should Be        ${LOGINPAGE}

Click Signin Button
    Click Element     id=gmail-sign-in

Input Username
    [Arguments]       ${username}
    Input Text        id=Email    ${username}


Input Password
    [Arguments]       ${password}
    Input Text        id=Passwd    ${password}

Submit Credentials
    Click Button    id=signIn

Inbox page should open
    Location Should Be        ${FINALURL}

运行此文件时出现以下错误:

I am getting the following error while running this file:

C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria>pybot pass_
fail.robot
==============================================================================
Pass Fail
==============================================================================
Login into gmail                                                      | PASS |
------------------------------------------------------------------------------
Check Deep Packet Inspection Stats                                    | PASS |
------------------------------------------------------------------------------
Pass fail Criteria                                                    | FAIL |
NameError: global name 'x' is not defined
------------------------------------------------------------------------------
Pass Fail                                                             | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
==============================================================================
Output:  C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria\ou
tput.xml
Log:     C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria\lo
g.html
Report:  C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria\re
port.html

C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria>

以下代码存在问题:

Pass fail Criteria
    ${status}           pass fail criteria
    should be equal     ${status}             pass
    ${result}           Pass fail criteria    ${x}

我该如何解决这个问题?

How can I fix this issue?

推荐答案

您遇到了几个问题.您似乎对基于 Python 的关键字的工作方式有根本的误解.

You have several problems working against you. It seems like you have a fundamental misunderstanding of how Python-based keywords work.

您正在定义和导入名为 regexp_def.py 的库.其中有一个关键字pass_fail_criteria".Robot 会去掉下划线,所以从 Robot 的角度来看,这个关键字被命名为Pass fail criteria".

You are defining and importing a library named regexp_def.py. In it there is one keyword, "pass_fail_criteria". Robot will remove the underscores, so from Robot's perspective, this keyword is named "Pass fail criteria".

在您的测试用例中,您还创建了一个名为通过失败标准"的关键字.目前还不清楚你为什么这样做.不要那样做.删除该关键字;这是不必要的.

In your test case you are also creating a keyword called "Pass fail criteria". It is unclear why you're doing that. Do not do that. Remove that keyword; it is unnecessary.

您在 pass_fail_criteria 中使用了一个变量 x,但您还没有定义它.这就是错误告诉你的.你需要定义它,或者传入它.要传入它,你需要把它变成一个参数,然后你需要为那个参数提供一个值.这与任何其他关键字或任何其他功能没有什么不同.

You are using a variable x in pass_fail_criteria, but you haven't defined it. That is what the error is telling you. You need to define it, or pass it in. To pass it in you need to make it a parameter, and then you need to provide a value for that parameter. This is no different than any other keyword, or any other function.

在文件 regexp_def.py 中:

import re

def pass_fail_criteria(x):
    if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
        return "pass"
    else:
        return "fail"

(注意定义中添加的参数)

(notice the added parameter in the definition)

在您的测试用例中:

Pass fail Criteria
    ${status}               pass fail criteria    ${x}

(注意第二行末尾的参数)

(notice the argument on the end of the second line)

按照您当前构建测试用例的方式,您是在一个测试用例中定义 ${x},然后尝试在另一个测试用例中使用它.我不知道这是不是故意的,但很多人认为这种测试用例设计很糟糕.测试用例应该尽可能独立.

The way you currently have your test cases structured, you are defining ${x} in one test case, and then attempting to use it in another. I don't know if this was intentional or not, but many people consider this bad test case design. Test cases should be as independent as possible.

虽然您可以这样做(使用内置关键字设置套件变量),我建议在名为Check Deep Packet Inspection Stats"的测试用例中调用pass fail criteria,其中${x} 已定义.

While you can do this (using the built-in keyword Set Suite Variable), I recommend calling pass fail criteria in the test case named "Check Deep Packet Inspection Stats", where ${x} is defined.

例如:

Check Deep Packet Inspection Stats
    ...
    ${x}                    Read Until Regexp       .*#
    ${status}               pass fail criteria      ${x}
    Run keyword if          "${status}" == "pass"   ...

这篇关于我如何解决“NameError: global name 'x' is not defined"?使用自定义库运行机器人框架测试用例时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆