我是机器人框架的新手 - 我尝试将此代码调用到机器人框架,但无济于事。我只需要一些帮助才能在机器人框架中运行我的 python 脚本并在该应用程序中返回 PASS 和 FAIL 。对此的任何帮助将不胜感激。
# -*- coding: utf-8 -*-
import paramiko
import time,sys
from datetime import datetime
from time import sleep
prompt = "#"
datetime = datetime.now()
ssh_pre = paramiko.SSHClient()
ssh_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_pre.connect("192.168.0.1",22, "admin", "admin")
output=""
ssh = ssh_pre.invoke_shell()
sys.stdout=open("ssh_session_dump.txt","w")
print("Script Start Date and Time: ", '%s/%s/%s' % (datetime.month, datetime.day, datetime.year), '%s:%s:%s' % (datetime.hour, datetime.minute, datetime.second))
model="XV4-17034"
ssh.send("more off\n")
if ssh.recv_ready():
output = ssh.recv(1000)
ssh.send("show system-info\n")
sleep(5)
output = ssh.recv(5000)
output=output.decode('utf-8')
lines=output.split("\n")
for item in lines:
if "Model:" in item:
line=item.split()
if line[1]==model+',':
print("Test Case 1.1 - PASS - Model is an " + model)
else:
print("Test Case 1.1 - FAIL - Model is not an " + model)
ssh.send( "quit\n" )
ssh.close()
datetime = datetime.now()
print("")
print("Script End Date and Time: ", '%s/%s/%s' % (datetime.month, datetime.day, datetime.year), '%s:%s:%s' % (datetime.hour, datetime.minute, datetime.second))
print("")
sys.stdout.close()
如果这是我的项目,我会将代码转换为函数,然后创建一个包含该函数的关键字库。
例如,您可以创建一个名为 CustomLibrary.py 的文件,其函数定义如下:
def verify_model(model):
prompt = "#"
datetime = datetime.now()
ssh_pre = paramiko.SSHClient()
...
for item in lines:
if "Model:" in item:
line=item.split()
if line[1]==model+',':
return True
else:
raise Exception("Model was %s, expected %s" % (line[1], model))
...
然后,您可以像这样创建一个机器人测试:
*** Settings ***
Library CustomLibrary
*** Test cases ***
Verify model is Foo
verify model foo
当然,事情比这稍微复杂一点。例如,您可能需要更改函数中的逻辑以确保在返回之前关闭连接。不过,总的来说,这是通用方法:创建一个或多个函数,将它们作为库导入,然后从机器人测试中调用这些函数。
要从 Robot Framework 调用 Python 代码,您需要使用与 Robot Framework 库相同的语法,但一旦这样做,就非常简单。这是一个示例,位于与测试位于同一文件夹中的名为 CustomLibrary.py 的文件中:
from robot.libraries.BuiltIn import BuiltIn
# Do any other imports you want here.
class CustomLibrary(object):
def __init__(self):
self.selenium_lib = BuiltIn().get_library_instance('ExtendedSelenium2Library')
# This is where you initialize any other global variables you might want to use in the code.
# I import BuiltIn and Extended Selenium2 Library to gain access to their keywords.
def run_my_code(self):
# Place the rest of your code here
我在测试中经常使用这个。为了调用它,你需要类似这样的东西:
*** Settings ***
Library ExtendedSelenium2Library
Library CustomLibrary
*** Test Cases ***
Test My Code
Run My Code
这将运行您在 Python 文件中放置的任何代码。据我所知,Robot Framework并不直接实现Python,但它是用Python编写的。因此,只要您以 Python 可以识别的形式向它提供 Python,它就会像BuiltIn 或 Selenium2Library 中的任何其他关键字一样运行它。
请注意,ExtendSelenium2Library 与 Selenium2Library 完全相同,只是它包含处理 Angular 网站的代码。相同的关键字,所以我只是将其用作严格的升级。如果您想使用旧的 Selenium2Library,只需将文本“ExtendedSelenium2Library”的所有实例替换为“Selenium2Library”即可。
请注意,为了使用BuiltIn或ExtendSelenium2Library中的任何关键字,您将需要分别使用语法
BuiltIn().the_keyword_name(arg1, arg2, *args)
或selenium_lib().the_keyword_name(arg1, arg2, *args)
。
最简单的方法是使用相对路径方法将
.py
文件导入到您的测试套件中,例如./my_lib.py
(假设您的python文件与TC文件位于同一文件夹中)
在您的
.py
文件中,只需定义一个函数,例如:
def get_date(date_string, date_format='%Y-%m-%d'):
return datetime.strptime(date_string, date_format)
然后在你的 TC 文件中:
*** Settings ***
Library ./my_lib.py
*** Test Cases ***
TEST CASE 1
Get Date | ${some_variable}
您可以下载 RobotCode VS Code 扩展并将其添加到 launch.json:
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "RobotCode: Run Current",
"type": "robotcode",
"request": "launch",
"cwd": "${workspaceFolder}",
"target": "${file}"
},
{
"name": "RobotCode: Run All",
"type": "robotcode",
"request": "launch",
"cwd": "${workspaceFolder}",
"target": "."
},
{
"name": "RobotCode: Default",
"type": "robotcode",
"request": "launch",
"purpose": "default",
"presentation": {
"hidden": true
},
"attachPython": false,
"pythonConfiguration": "RobotCode: Python"
},
{
"name": "RobotCode: Python",
"type": "python",
"request": "attach",
"presentation": {
"hidden": true
},
"justMyCode": false
}
]