如何将show命令的输出发送到Robot框架的自定义库?

时间:2015-01-28 08:07:39

标签: python robotframework

我打算自动执行以下方案:

STEP 1 : Launch gmail page in firefox browser
STEP 2 : Enter username and password
STEP 3 : Press signin button
STEP 4 : When above 3 steps are happening, my router will recognize as gmail traffic and show in "show dpi app stats gmail on AVC/test-15E8CC" command output.
STEP 5 : Custom library should check the output of above show command and decide whether test is FAIL or PASS

出于上述目的,我写了“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/switch-15E8CC
    ${x}                    Read Until Regexp       .*#

Check Pass Fail Criteria
    pass fail criteria



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

我的自定义库“regexp_def.py”如下所示:

import re

'''

#Following is the sample content of x variable should get from robot framework:


x="""router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
  --------------------------------------------------------------------------------
     APPLICATION           BYTES_IN         BYTES_OUT           NUM_FLOWS
--------------------------------------------------------------------------------
  gmail                0                 0                  0
--------------------------------------------------------------------------------
router-7F2C13#
---------------------------------------------------------------
router-7F2C13#"""
'''


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

pass_fail_criteria()

当我运行此测试时,我收到以下错误:

C:\Users\test\Desktop\Projects\gmail_stats_with_pass_fail_criteria>pybot pass_
fail.robot
    [ ERROR ] Error in file 'C:\Users\test\Desktop\Projects\gmail_stats_with_pass_
fail_criteria\pass_fail.robot': Importing test library 'C:\Users\test\Desktop\
Projects\gmail_stats_with_pass_fail_criteria\regexp_def.py' failed: NameError: g
lobal name 'x' is not defined
Traceback (most recent call last):
  File "C:\Users\test\Desktop\Projects\gmail_stats_with_pass_fail_criteria\reg
    exp_def.py", line 26, in <module>
pass_fail_criteria()
  File "C:\Users\test\Desktop\Projects\gmail_stats_with_pass_fail_criteria\reg
exp_def.py", line 21, in pass_fail_criteria
    if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
PYTHONPATH:
  C:\Python27\lib\site-packages\robot\libraries
  c:\python27\lib\site-packages\pycrypto-2.6-py2.7-win32.egg
  C:\Python27\lib\site-packages
  C:\Python27\Lib
  C:\Windows\system32\python27.zip
  C:\Python27\DLLs
  C:\Python27\lib\plat-win
  C:\Python27\lib\lib-tk
  C:\Python27
  C:\Python27\lib\site-packages\win32
  C:\Python27\lib\site-packages\win32\lib
  C:\Python27\lib\site-packages\Pythonwin
  C:\Python27\lib\site-packages\wx-2.8-msw-unicode
  .
    ==============================================================================
Pass Fail
    ==============================================================================
Login into gmail                                                      | PASS |
------------------------------------------------------------------------------
Check Deep Packet Inspection Stats                                    | PASS |
------------------------------------------------------------------------------
Check Pass Fail Criteria                                              | FAIL |
No keyword with name 'pass fail criteria' found.
------------------------------------------------------------------------------
Pass Fail                                                             | FAIL |
3 critical tests, 2 passed, 1 failed
3 tests total, 2 passed, 1 failed
 ==============================================================================
Output:  C:\Users\test\Desktop\Projects\gmail_stats_with_pass_fail_criteria\ou
tput.xml
Log:     C:\Users\test\Desktop\Projects\gmail_stats_with_pass_fail_criteria\lo
g.html
Report:  C:\Users\test\Desktop\Projects\gmail_stats_with_pass_fail_criteria\re
port.html

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

任何人都可以调查一下。

我发现将所需数据(show命令输出到“$ {x}”)发送到自定义库时出现问题。

还有其他问题吗?请指导我。

根据“Bryan Oakley”修改的新文件评论:

regexp_def.py文件内容:

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文件内容:

*** 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}             10.1.1.2
${USERNAME}         admin
${PASSWORD}         moto

*** Test Cases ***
Login into gmail
    Go to gmail page
    Login Page Should Be Open
    Click Signin Button
    Input Username      zebradtc@gmail.com
    Input Password      Zebra@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       .*#

Check Pass Fail Criteria
    ${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}


#${status}          pass fail criteria
should be equal    ${status}             pass


# pass ${x} to the custom keyword
${result}          Pass fail criteria      ${x} 

修改这些文件后,我收到以下错误:

C:\Users\symbol\Desktop\Projects\gmail_stats_with_pass_fail_criteria>pybot pass_
fail.robot
==============================================================================
Pass Fail
==============================================================================
Login into gmail                                                      | FAIL |
Parent suite setup failed:
Keyword '${result}' expected 0 arguments, got 2.
------------------------------------------------------------------------------
Check Deep Packet Inspection Stats                                    | FAIL |
Parent suite setup failed:
Keyword '${result}' expected 0 arguments, got 2.
------------------------------------------------------------------------------
Check Pass Fail Criteria                                              | FAIL |
Parent suite setup failed:
Keyword '${result}' expected 0 arguments, got 2.
------------------------------------------------------------------------------
Pass Fail                                                             | FAIL |
Suite setup failed:
Keyword '${result}' expected 0 arguments, got 2.

Also suite teardown failed:
Variable '${x}' not found.

3 critical tests, 0 passed, 3 failed
3 tests total, 0 passed, 3 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>

3 个答案:

答案 0 :(得分:2)

看起来你有几个问题在继续。我们一次解决一个问题。

导入库

时不要调用该函数

库用于定义关键字,而不是调用它们。 当您只想在测试运行时调用它时,您在导入库时调用pass_fail_critiera

对此的修复是从库文件的末尾删除pass_fail_criteria(),以便该文件包含def,但不直接调用该函数。

这就是为什么在尝试在测试用例中使用关键字之前获得堆栈跟踪的原因 - 错误发生在导入时,因为在导入文件时正在调用该函数。

关键字应返回结果或引发异常

您的pass_fail_criteria关键字需要返回&#34;传递&#34;或者&#34;失败&#34;,或者它应该在成功时静默返回并在出错时抛出异常。你选择哪个有点偏好。您是否希望关键字始终成功,以便您可以稍后测试结果,或者如果您的标准失败,您是否希望它失败?

以下是一个示例,如果您希望它始终成功:

def pass_fail_criteria():
    if int(...):
        return "pass"
    else:
        return "fail"

然后,您可以在测试用例或关键字中使用这样的关键字:

${status}          pass fail criteria
should be equal    ${status}             pass

访问变量$ {x}

第三个问题是即使您从测试脚本中调用pass_fail_criteria,它也无法访问机器人变量x。您需要在调用关键字时传递它,或者需要通过内置关键字Get Variable Value获取它。有一个如何在用户指南中调用内置关键字的示例(请参阅标题为Using Robot Framework's internal modules的部分)。

要接受x作为关键字参数,您需要修改您的函数,如下所示:

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

要传入值,您可以像任何其他关键字一样进行传递。您没有显示您如何调用该关键字,但它应该如下所示:

检查深度包检测统计信息     ...     #设置值$ {x}     $ {x} Read to Regexp。*#

# pass ${x} to the custom keyword
${result}               Pass fail criteria      ${x}

通过上述内容,机器人变量${x}将成为函数x

中的python变量pass_fail_criteria

创建名为$ {result}

的关键字

在阅读原始答案后,您修改了问题以报告新错误,即Keyword '${result}' expected 0 arguments, got 2.

这是因为您在这些代码行中创建了一个名为${result}的关键字:

*** Keywords ***
...
# pass ${x} to the custom keyword
${result}          Pass fail criteria      ${x}   

我不知道你在那里做什么,但无论如何,这都是错误的。您不应该创建与变量同名的关键字。您需要删除这些代码行。

答案 1 :(得分:0)

lobal name&#39; x&#39;未定义

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

您在参数列表中忘记了x。

答案 2 :(得分:0)

变量不能自动用于自定义库(您定义X,但不将其传递给库。

你可以将X传递给pass fail关键字(作为输入) - 推荐。

或者你可以通过导入内置机器人库并以这种方式提取X值(不推荐)来实现愚蠢的方式