3
votes

I have a Python module having 2 classes. Each class has a set of functions or methods defined. How can we call a particular method from a class in the ROBOT framework. I am trying the below method however, its giving the following errors. Can someone please help me in figuring the issue over here. The Python module and the Robot files are in the same path.I have tried changing the library statement to CheckCode.employee WITH_NAME xyz. This hasnt helped. Thanks.

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
3

3 Answers

11
votes

Robot does not automatically create instances of classes that are in a library file, with one exception: it will automatically create an instance of a class if the name matches the filename without the .py extension. For example, if your file CheckCode.py defined a class named CheckCode, robot will automatically create an instance, and with that instance it will expose every method as a keyword.

If you want to create an instance of some class in a file, you will have to create a keyword that does that. For example:

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

You can then use it like this:

*** Settings ***
Library    CheckCode.py

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

You can also call methods on the object with the Call Method keyword:

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

Calling a particular method from python module in ROBOT framework

Robot file

*** 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 file hello.py

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

s=simple()
1
votes

It sounds like you might be importing the library using the physical path. In order to import two libraries from the same module, you must import them by name like:

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

Or dynamically:

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

In order to import like this, you need to get your module on the Python path. See this section for help with that.

From the Using physical path to library in the user guide:

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