简体   繁体   English

使用自定义库运行机器人框架测试用例时,如何解决“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?

I am seeing the "NameError: global name 'x' is not defined" error while running a testcase in Robot Framework.在 Robot Framework 中运行测试用例时,我看到“NameError: global name 'x' is not defined”错误。

Following is my custom library file (modified as per Bryan Oakley's comments):以下是我的自定义库文件(根据 Bryan Oakley 的评论修改):

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"

Following is the "pass_fail.robot" file contents:以下是“pass_fail.robot”文件内容:

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

There are issues in the below code:以下代码存在问题:

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

How can I fix this issue?我该如何解决这个问题?

You have several problems working against you.你有几个问题对你不利。 It seems like you have a fundamental misunderstanding of how Python-based keywords work.您似乎对基于 Python 的关键字的工作方式有根本的误解。

Two keywords with the same name两个同名关键字

You are defining and importing a library named regexp_def.py.您正在定义和导入名为 regexp_def.py 的库。 In it there is one keyword, "pass_fail_criteria".其中有一个关键字,“pass_fail_criteria”。 Robot will remove the underscores, so from Robot's perspective, this keyword is named "Pass fail criteria". Robot 会去掉下划线,所以从Robot 的角度来看,这个关键字被命名为“通过失败标准”。

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.这是不必要的。

The variables "x" and "${x}"变量“x”和“${x}”

You are using a variable x in pass_fail_criteria , but you haven't defined it.您在pass_fail_criteria中使用了变量x ,但您尚未定义它。 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.这与任何其他关键字或任何其他功能没有什么不同。

In file regexp_def.py :在文件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) (注意定义中添加的参数)

In your test case:在您的测试用例中:

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

(notice the argument on the end of the second line) (注意第二行末尾的参数)

Independent test cases独立测试用例

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.按照您当前构建测试用例的方式,您是在一个测试用例中定义${x} ,然后尝试在另一个测试用例中使用它。 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.测试用例应该尽可能独立。

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.虽然您可以这样做(使用内置关键字Set Suite Variable ),但我建议在名为“Check Deep Packet Inspection Stats”的测试用例中调用pass fail criteria ,其中定义了${x}

For example:例如:

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

x is not defined and you are using x in the following statement. x未定义,您在以下语句中使用x

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

Pass x as an argument to function pass_fail_criteria(x) and use try exceptx作为参数传递给函数pass_fail_criteria(x)并使用try except

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

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

相关问题 尝试在 Python 2.7 中导入任何模块时,如何解决“NameError: name 'null' is not defined”错误 - How can I resolve a "NameError: name 'null' is not defined" error while trying to import any module in Python 2.7 如何解决NameError:未定义全局名称“ ContactForm” - How to resolve a NameError: global name 'ContactForm' is not defined NameError: 全局名称 'x' 未定义 - NameError: global name 'x' is not defined 使用WITH NAME导入Robot Framework自定义库 - Robot Framework Custom Library Imports using WITH NAME 如何在Robot Framework中创建自定义测试用例设置 - How to create custom testcase settings in Robot Framework NameError:使用类时未定义名称“Robot” - NameError: name 'Robot' is not defined when using class 我该如何解决这个错误? NameError:未定义名称“模型” - How can I solve this error? NameError: name ‘model’ is not defined NameError:未定义全局名称,为什么会收到该错误? - NameError: global name is not defined, why am I getting that error? 为什么会出现错误:“ NameError:未定义全局名称'pupiluserinputbox'” - Why do I get the error: “NameError: global name 'pupiluserinputbox' is not defined” NameError:运行使用 Pyinstaller 转换的 .exe 时未定义名称“defaultParams” - NameError: name 'defaultParams' is not defined while running the .exe converted using Pyinstaller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM