Unable to use Robot Framework custom library which has written using Python

与世无争的帅哥 提交于 2021-02-07 08:43:25

问题


Created a sample Python script( Elements.py) with a function like below:

from robot.api.deco import keyword

@keyword("join two strings")
def join_two_strings(arg1, arg2):
   return arg1 + " " + arg2

Then I have imported into Robot Framework script(.robot file) as a Library:

*** Settings ***
Library                 AppiumLibrary
Library                 Selenium2Library
Library                 BuiltIn
#Here is the import of Custom Lib
Library                 Elements.py 

*** Variable ***

*** Test Cases ***
Example that calls a Python keyword
   ${result}=   join two strings   hello world
   Should be equal     ${result}    hello world

After running above the script, getting error like "No keyword with name 'join two strings' found." even though where I have imported the Custom library.

Error Message:

[ ERROR ] Error in file C:\Users\ramana.gouda\PycharmProjects\SafeMobile\Test_Suite\TestCase_346.robot: Test library 'Elements.py' does not exist.

TestCase 346 :: Creating internal cases using device                          

Example that calls a Python keyword                                   | FAIL |
No keyword with name 'join two strings' found.

回答1:


I always have to use relative paths unless the file is in the same directory as my test case and based on the error it looks like yours is not.

So in your case it would look something like the following (Not exactly this as I don't know where Elements.py is located):

Library    ../../SafeMobile/Elements.py

Hope this helps!




回答2:


The other option if you do not want to include the relative path in all your robot file is to use the --pythonpath command line argument when starting a test.

-P, --pythonpath

Additional locations to add to the module search path.

This way you can have:

Library    Elements.py

in your code while you have to launch it like:

robot  --pythonpath C:/Users/ramana.gouda/PycharmProjects/SafeMobile/ Test_Suite/TestCase_346.robot

from the SafeMobile folder.


You can go further and create an argument file in which you can collect all path settings. For example custom_libraries.txt:

--pythonpath C:/Users/ramana.gouda/PycharmProjects/SafeMobile/
--pythonpath C:/Users/ramana.gouda/PycharmProjects/SafeMobile/libs/

And you can use it when launching tests:

robot --argumentfile custom_libraries.txt Test_Suite/TestCase_346.robot

This way when a new library is intorduced by you or anyone else, there is no need to change the way a test is started. You only have to make sure to add the path of the new library into the argument file.



来源:https://stackoverflow.com/questions/54759821/unable-to-use-robot-framework-custom-library-which-has-written-using-python

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