繁体   English   中英

从ROBOT框架中的python模块调用特定方法

[英]Calling a particular method from python module in ROBOT framework

我有2个类的Python模块。 每个类都有一组定义的函数或方法。 我们如何从ROBOT框架的类中调用特定方法。 我正在尝试下面的方法,但是它给出了以下错误。 有人可以帮我解决这里的问题。 Python模块和机器人文件在同一路径中。我尝试将库语句更改为CheckCode.employee WITH_NAME xyz。 这没有帮助。 谢谢。

ERRORS
==============

[ WARN ] Imported library '/homes/user/New/CheckCode.py' contains no keywords.
==============================================================================
CheckCode :: Checking small built in code                                     
==============================================================================
Verify we can call a particular class from a Python Module in Robot   | FAIL |
No keyword with name 'my_code.employee.staff info' found.
------------------------------------------------------------------------------
CheckCode :: Checking small built in code                             | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================


Python Module File output
******************************

import re
import collections
import math

class person():
    def __init__(self,first,last):
        self.firstname = first
        self.lastname = last

    def emp_name(self):
        return self.firstname + " " + self.lastname

class employee(person):
    def __init__(self,first,last,empId):
        person.__init__(self,first,last)
        self.staffId = empId

    def staff_info(self):
        return self.Name() + " " + self.staffId

ROBOT FILE 
******************************

*** Settings ***
Documentation    Checking small built in code
Library   BuiltIn
Library   Collections
Library   CheckCode.py     WITH NAME   my_code

*** Test Cases ***
Verify we can call a particular class from a Python Module in Robot
    Log     Hello World
    ${var} =    my_code.employee.staff info     Maggi       Nestle      20000


*** Keywords ***
Init
    Set Log Level    DEBUG

Robot不会自动创建库文件中的类的实例,只有一个例外:如果名称与不带.py扩展名的文件名匹配,它将自动创建类的实例。 例如,如果您的文件CheckCode.py定义了一个名为CheckCode的类,robot将自动创建一个实例,并使用该实例将每个方法公开为关键字。

如果要在文件中创建某个类的实例,则必须创建一个执行该操作的关键字。 例如:

# CheckCode.py
class person()
    ...
...
def create_person(first, last):
    return person(first, last)

然后可以像这样使用它:

*** Settings ***
Library    CheckCode.py

*** Test Cases ***
Example
    ${person}=    create person    Maggi    Nestle
    Should be equal as strings    ${person.emp_name()}    Maggi Nestle

您还可以使用Call Method关键字在对象上调用方法

${name}=    Call method    ${person}    emp_name

听起来您可能正在使用物理路径导入库。 为了从同一模块导入两个库,必须按如下名称导入它们:

*** Settings ***
Library    CheckCode.person    firstname    lastname
Library    CheckCode.employee    firstname    lastname    someid

或动态地:

Import Library    CheckCode.person    firstname    lastname
Import Library    CheckCode.employee    firstname    lastname    someid

为了这样导入,您需要将模块放在Python路径上。 请参阅本节以获取帮助。

从用户指南的使用库使用物理路径

这种方法的局限性在于,实现为Python类的库必须位于与该类同名的模块中。

从ROBOT框架中的python模块调用特定方法

机器人文件

*** Settings ***

Library     Selenium2Library
Variables    hello.py

*** Test Cases ***

LoginTest
   Open Browser to the Login Page

*** Keywords ***

Open Browser to the Login Page
   ${var}=   call method    ${s}    brow
   ${SiteUrl}=  call method  ${s}   url
   open browser    ${SiteUrl}    ${var}
   Maximize Browser Window
   sleep   1s
   close browser

Python文件hello.py

class simple:
  def brow(self):
     return("Chrome")
  def url(self):
     return("https://www.google.com/")

s=simple()

暂无
暂无

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

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