gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:36 25 4
gpt4 key购买 nike

我计划自动化以下场景:

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>

最佳答案

看来您遇到了几个问题。我们将一次解决一个问题。

导入库时不要调用该函数

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

解决方法是从库文件末尾删除 pass_fail_criteria(),以便文件包含 def,但不直接调用该函数.

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

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

您的 pass_fail_criteria 关键字需要返回“通过”或“失败”,或者它应该在成功时静默返回并在出错时抛出异常。您选择哪个有点偏好问题。您是希望关键字始终成功以便稍后测试结果,还是希望它在条件失败时失败?

如果你希望它总是成功,这里有一个例子:

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} 一直读到 Regexp .*#

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

有了上面的代码,机器人变量 ${x} 将成为函数 pass_fail_criteria 中的 python 变量 x

创建一个名为 ${result}

的关键字

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

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

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

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

关于python - 如何将 show 命令的输出发送到 Robot 框架的自定义库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28187394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com