简体   繁体   中英

Robot Framework import library instance does not contain defined methods

I have written a test case in Robot Framework which creates an instance of a class in the middle of Test Suite using Builtin.Import_Library keyword, then calls its methods using Builtin.Call_Method :

*** Settings ***
Resource            MyKeywords.robot
Test Suite          Initiate My Test


*** Keywords ***
Initiate My Test
    ${ip} =     SET VARIABLE     localhost
    ${port} =   SET VARIABLE     2020
    IMPORT LIBRARY      src/Interface/Utility/WebServiceUtil.py
    ...             ws_ip=${ip}     ws_port=${port}     WITH NAME   webserviceutil


*** Test Cases ***
Test Report A
    ${result} =     CALL METHOD     webserviceutil      get_report_a
    LOG    Result: ${result}        console=${TRUE}

File src/Interface/Utility/WebServiceUtil.py contains:

# -*- encoding: utf-8 -*-
import requests
import json
from robot.api import logger


class WebServiceUtil(object):

    ROBOT_LIBRARY_SCOPE = 'TEST SUITE'

    def __init__(self, ws_ip, ws_port):
        self.reporter_a = ReportA(ip=ws_ip, port=ws_port)
        self.reporter_b = ReportB(ip=ws_ip, port=ws_port)
        self.reporter_c = ReportC(ip=ws_ip, port=ws_port)
        logger.console('>> ZiZi >> webserviceutil has been initialized successfully!')
        logger.console('>> ZiZi >> self.__dict__: ' + str(self.__dict__))
        logger.console('>> ZiZi >> dir(self): ' + str(dir(self)))

    def get_report_a(self):
        return self.reporter_a.get_report()

    def get_report_b(self):
        return self.reporter_b.get_report()

    def get_report_c(self):
        return self.reporter_c.get_report()


class Report(object):

    def get_report():
        return 'This is abstract class!'


class ReportA(Report):

    def get_report():
        return 'This is class A!'


class ReportB(Report):

    def get_report():
    return 'This is class B!'


class ReportC(Report):

    def get_report():
    return 'This is class C!'

I get this error in test execution:

Object 'webserviceutil' does not have method 'get_sponsor_report'.

The console prints which I have put in the __init__ of class WebServiceUtil returns:

>> ZiZi >> webserviceutil has been initialized successfully!

>> ZiZi >> self.__dict__: {'reporter_a': <WebServiceUtil.ReportA object at 0x7fc18d96a8d0>, 'reporter_b': <WebServiceUtil.ReportB object at 0x7fc18d96abd0>, 'reporter_c': <WebServiceUtil.ReportC object at 0x7fc18d96a910>}

>> ZiZi >> dir(self): ['ROBOT_LIBRARY_SCOPE', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'get_report_a', 'get_report_b', 'get_report_c', 'reporter_a', 'reporter_b', 'reporter_c']

As you can see, class methods are listed in the output of dir() , but not shown in the output of self.__dict__ .

I also tried changing ROBOT_LIBRARY_SCOPE to GLOBAL , but it didn't change anything.

Any idea what is the cause?

EDIT 1:

I also tried calling __init__ method of super class in the beginning of method __init__ of class WebServiceUtil :

super(WebServiceUtil, self).__init__()

Same results.

EDIT 2:

I tried calling WebServiceUtil methods without CALL METHOD as @Bryan said with two approaches:

  1. ${result} = webserviceutil get_report_a
  2. ${result} = get_report_a

The first one returned No keyword with name 'webserviceutil.get_report_a' found. and the second returned No keyword with name 'get_report_a' found. .

EDIT 3:

There are two things that seems to be creating the issue in my mind:

  1. I have overwritten __init__ method.
  2. Methods aren't static methods.

I have used classes in Robot Framework before and none of them had above specs; so, I guess maybe these are making the issue here.

If you are importing it, the methods become keywords. You don't need to use call method . In your example, when you import WebServiceUtil , you have access to keywords named get report A , get report B , and get report C .

*** Test Cases ***
Test Report A
    ${result} =     get report A
    LOG    Result: ${result}        console=${TRUE}

As I mentioned in the question edits, the issue was related to the overwritten __init__ method and used my class variables in other ways. I don't know why, but removing __init__ solved the problem. Methods are still class methods; which means both static and class methods are treated the same here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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