简体   繁体   English

API 处理机器人和 Python Class

[英]API handling with Robot and Python Class

What is the best way to handle objects with robot framework?使用机器人框架处理对象的最佳方法是什么? I am starting to write a python class to handle API interactions, which I can therefore use as keywords in robot framework (RF).我开始编写 python class 来处理 API 交互,因此我可以将其用作机器人框架 (RF) 中的关键字。 My question is how does one pass data from one method to another?我的问题是如何将数据从一种方法传递到另一种方法? Do I have to pass the object back to every function to get the data?我是否必须将 object 传回每个 function 才能获取数据?

In the example below, I call the class and it initializes, but can I reference an instance of the class if I wanted?在下面的示例中,我调用了 class 并对其进行了初始化,但是如果需要,我可以引用 class 的实例吗? Or am I supposed to write every method to handle the entire object I get back from another method?还是我应该编写每种方法来处理我从另一种方法返回的整个 object? Hopefully this makes sense, I basically want to use python like I normally would but inside of RF.希望这是有道理的,我基本上想像往常一样使用 python 但在 RF 内部。

More specifically, is it feasible to distinguish between several instances if I call them all at once?更具体地说,如果我一次调用它们是否可以区分多个实例?

Test python foo.py:测试 python foo.py:

class foo:
   def intialize(self, api):
        self.api_item = api 
   def get_api():
         return self.api_item
   def do_something_with_api
        # doing something with an API, then return results
   def do_something_else_with_api
        # doing something with an API, then return results
   

Test Robot file:测试机器人文件:

*** Settings ***
Library             /path/foo.py

*** Variables ***
${api_url}   "https://apiurl.com/"


*** Tasks ***
Setup Initialize Settings
     ${session}=    MgsRestApiHandler.intialize     ${api_url}
    

In RF when a class is loaded as a Library there's always an instance object that's created for it.在 RF 中,当 class 作为库加载时,总会有一个为其创建的实例 object。 Thus if you have state variables within it, they'll be present for all class methods ("keywords") in your RF source.因此,如果您在其中有 state 变量,它们将出现在您的 RF 源中的所有 class 方法(“关键字”)中。 In other words, in your example all methods will have access to self.api_item (after initialize() is called);换句话说,在您的示例中,所有方法都可以访问self.api_item (在调用initialize()之后); by the way, why don't you add a normal constructor __init()__ and define the var there, even with None value, so it's cleaner?顺便说一句,你为什么不添加一个普通的构造函数__init()__并在那里定义 var,即使是None值,所以它更干净?


is it feasible to distinguish between several instances区分几个实例是否可行

You can instantiate several instances of the same class ("Library") by importing them multiple times and using the WITH NAME Robot Framework syntax:您可以通过多次导入并使用WITH NAME Robot Framework 语法来实例化相同 class(“库”)的多个实例:

*** Settings ***
Library             /path/foo.py    WITH NAME    o1
Library             /path/foo.py    WITH NAME    o2

The "drawback" is you now have to prefix the method call with the instance name - otherwise the framework doesn't know for which object you want to call it: “缺点”是您现在必须在方法调用前加上实例名称 - 否则框架不知道您要为哪个 object 调用它:

*** Tasks ***
Setup Initialize Settings
     ${session1}=   o1.intialize     ${api_url}
     ${session2}=   o2.intialize     ${api_url2} 

And if I understand one of your questions correctly (or if not - take this as a general trivia:) - in RF whatever a method/keyword returns - from primitives to complex objects (egnclass instances) is assigned to that variable that in front of the call.如果我正确理解了您的一个问题(或者如果没有正确理解 - 将其作为一般琐事:) - 在 RF 中,无论方法/关键字返回什么 - 从原语到复杂对象(egnclass 实例)都分配给前面的那个变量通话。
So if you have a method/keyword down the line that expects a complex object, you can pass that returned value - the framework will not mangle it in any way, it'll be passing around a normal pyhton object.因此,如果您有一个方法/关键字需要复杂的 object,您可以传递该返回值 - 框架不会以任何方式破坏它,它将传递一个普通的 pyhton object。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM