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

2024-09-28 22:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python模块,它有两个类。每个类都定义了一组函数或方法。如何从ROBOT框架中的类调用特定方法。不过,我正在尝试下面的方法,它给出了以下错误。有人能帮我解决这个问题吗。Python模块和Robot文件位于同一路径中。我已尝试将library语句更改为CheckCode.employee,名称为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

Tags: 方法inselfdefemployeerobotcodeclass
3条回答

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

从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()

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

*** 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路径上获取模块。请参阅this section以获取有关该操作的帮助。

从用户指南中的Using physical path to library

A limitation of this approach is that libraries implemented as Python classes must be in a module with the same name as the class.

相关问题 更多 >