Inner nested class access from robot framework

℡╲_俬逩灬. 提交于 2021-02-05 11:15:11

问题


Robot - 3.1.1 Python - 3.7.3

I wanted to access method that are written in nested inner class from robot framework.

Robot:

*** Settings ***
Library             ../***/***/***/OrderList.py

    *** Keywords ***

    Click from order
        click_order

Python:

class OrderList():
           pass
class Ordertable(OrderList):
       def click_order(self):
            foo

I am getting below error while running the above robot suite.

No keyword with name 'click_order' found.

If I move the click_order method under parent class (OrderList) like below, then robot could recognize.

class OrderList():
    def click_order(self):
            foo
class Ordertable(OrderList):
       pass

Could someone help me as to what changes required at robot suite to make a call to nested inner class methods?


回答1:


Here is an example about how to import a class as a library and not the whole python file:

  1. In a folder named tests there is a test.robot and an OrderList.py file.
  2. OrderList.py:

    class OrderList():
               pass
    
    class Ordertable(OrderList):
           def click_order(self):
                print('foo')
    
  3. test.robot:

    *** Settings ***
    Library        OrderList.Ordertable
    
    *** Test Cases ***
    AA
        click order
    
  4. Launch it with the following command from the parent folder of the tests folder: robot --pythonpath .\tests\ --test AA .\tests\test.robot

  5. Result:

    PS prompt> robot --pythonpath .\tests\ --test AA .\tests\test.robot
    ==============================================================================
    Test
    ==============================================================================
    AA                                                                    | PASS |
    ------------------------------------------------------------------------------
    Test                                                                  | PASS |
    1 critical test, 1 passed, 0 failed
    1 test total, 1 passed, 0 failed
    ==============================================================================
    Output:  C:\Users\myuser\output.xml
    Log:     C:\Users\myuser\log.html
    Report:  C:\Users\myuser\report.html
    



回答2:


When Robot Framework imports a library, it tries to import just the class named the same as the .py file - as in your case, the class OrderList in the OrderList.py file. And it doesn't import any other classes - check the documentation for more details.

The easiest option would be to have the class you are going to use renamed, to the same name as the file.

If that's not applicable - and looks like your intent is to use more than one class, you could have the file in your PYTHONPATH (like, physically moving the file to a dir in it, or extend it to include the module's one) and import the classes separately as OrderList.Ordertable.
Another would be to solve it on the python side - put each class in a module of their own, each importing the one with the base class.

The first is ops nightmare, the second - design & maintenance; your choice :).



来源:https://stackoverflow.com/questions/57660169/inner-nested-class-access-from-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!