4

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.
jia Jimmy
  • 1,693
  • 2
  • 18
  • 38
Raman
  • 444
  • 6
  • 21

2 Answers2

5

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!

cullzie
  • 2,705
  • 2
  • 16
  • 21
  • Tried and that's an awkward. I have done with same way since 3-4 hours, It was not working and its working now :) Thanks for your time! – Raman Feb 19 '19 at 06:44
3

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.

Community
  • 1
  • 1
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
  • Is there a way the custom library can be added to the pythonpath permanently? Just like we add other external libraries. – Sameem Feb 19 '19 at 16:20
  • 1
    @Sameem External libraries are installed under `Python\Python37\site-packages` folder. If you include your library source code here, then python should find it like the others. Other way is to update PYTHONPATH env variable with the path of your library. [How to add to the pythonpath in Windows?](https://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows), there should be answers for the same on Linux as well. Personally I prefer the command line argument here as it is portable and with multiple developers there is no need to set up the PYTHONPATH on every machine. – Bence Kaulics Feb 19 '19 at 17:35