Relative path and importing python classes

2,076 views
Skip to first unread message

Gu Ru

unread,
Aug 9, 2015, 7:18:06 AM8/9/15
to robotframework-users

Relative path to import python classes appears not to work. Could someone confirm please


I have the following test cases as below and I have used the demo package to illustrate the issue


Test case 1:

Step 1: pumbed.py module is in same directory as test_pubmed.robot



Library  pubmed.PubmedHomePage
Library  pubmed.PubmedDocsumPage
Library  pubmed.PubmedArticlePage


pybot -vbrowser:firefox -vbaseurl:http://www.ncbi.nlm.nih.gov test_pubmed.robot


Observed: it works . Test case is executed



Test case 2:

step 1: Create a lib directory. Place the pubmed module inside the folder
step 2: change test_pubmed.robot as below
step 3: execute pybot testfile.robot

Library  ${CURDIR}${/}lib${/}pubmed.PubmedHomePage
Library  ${CURDIR}${/}lib${/}pubmed.PubmedDocsumPage
Library  ${CURDIR}${/}lib${/}pubmed.PubmedArticlePage


pybot -vbrowser:firefox -vbaseurl:http://www.ncbi.nlm.nih.gov test_pubmed.robot


Observed:  it fails and an error is thrown

20150809 13:03:41.940ERRORError in file 'C:\Users\rramasubramanian\robotframework-pageobjects-master\demo\test_pubmed.robot': Importing test library 'C:\Users\rramasubramanian\robotframework-pageobjects-master\demo\lib\pubmed.PubmedHomePage' failed: File or directory does not exist.
20150809 13:03:41.944ERRORError in file 'C:\Users\rramasubramanian\robotframework-pageobjects-master\demo\test_pubmed.robot': Importing test library 'C:\Users\rramasubramanian\robotframework-pageobjects-master\demo\lib\pubmed.PubmedDocsumPage' failed: File or directory does not exist.
20150809 13:03:41.947ERRORError in file 'C:\Users\rramasubramanian\robotframework-pageobjects-master\demo\test_pubmed.robot': Importing test library 'C:\Users\rramasubramanian\robotframework-pageobjects-master\demo\lib\pubmed.PubmedArticlePage' failed: File or directory does not exist.



Test case 3:
Step 1: I also created a directory within lib folder as: pubmed. Added pubmed.py and __init__.py
Step2:  in __init__.py, imported all the classes

 
from pubmed import *

Step 3: navigate to folder where test_pubmed.robot is
Step 4: execute

 pybot -vbrowser:firefox -vbaseurl:http://www.ncbi.nlm.nih.gov test_pubmed.robot

Observed: it fails with same error message as above


I would like to confirm if I am doing something wrong





 With regards,
Rama

Hélio Guilherme

unread,
Aug 9, 2015, 1:10:05 PM8/9/15
to robotframework-users
You did a nice description of your case, and mentioned the reference doc, but you did not pay attention to the doc contents:

Using physical path to library

Another mechanism for specifying the library to import is using a path to it in the file system. This path is considered relative to the directory where current test data file is situated similarly as paths to resource and variable files. The main benefit of this approach is that there is no need to configure the module search path.

If the library is a file, the path to it must contain extension. For Python libraries the extension is naturally .py and for Java libraries it can either be .class or .java, but the class file must always be available. If Python library is implemented as a directory, the path to it must have a trailing forward slash (/). Following examples demonstrate these different usages.

*** Settings ***
Library    PythonLib.py
Library    /absolute/path/JavaLib.java
Library    relative/path/PythonDirLib/    possible    arguments
Library    ${RESOURCES}/Example.class

A limitation of this approach is that libraries implemented as Python classes must be in a module with the same name as the class. Additionally, importing libraries distributed in JAR or ZIP packages is not possible with this mechanism.

----
This last sentence would translate to (I think):
Library    Pubmed

Gu Ru

unread,
Aug 9, 2015, 4:13:52 PM8/9/15
to robotframework-users
Thank you very much for the highlightened text !!! Its clear for me now
Reply all
Reply to author
Forward
0 new messages