nokia / RED

RED - Robot Editor
Other
339 stars 111 forks source link

Debugging Python Files and Import Errors #400

Open ac57486 opened 3 years ago

ac57486 commented 3 years ago

Hello,

I am facing two problems with imports and debugging customized python libraries(user created python libraries):

1) I have some keywords that are defined in customized Python libraries. e.g. 'Open Browser Maximized' which takes 3 arguments and is defined in the python library created by me which is Selenium.py

    Open Browser Maximized    ${browser}    ${user_profile}    ${non_incognito_status}

Please refer screenshot:

a) Here problem is this python file Selenium.py is not imported directly into this keyword definition file. Instead I am 
 importing all the common libraries in one CommonImports.robot file and then importing this file into robot tests.
Due to this I am getting this error. Same error comes for keywords from other libraries such as String Python library which is 
imported in Common Imports instead of directly importing into step definition.
**Is there any way to remove error without importing files directly into the current file?**

The below screenshot shows the step definition files where we call keywords from different Python filess

image

The below screenshot refers to the CommonImport.robot file which is imported only in tests not into keyword definitions 
shown above.

image

b) If you see argument ${browser}, it shows error, reason is this variable is not defined here, but defined in common variables 
file which is a global file

2) Another problem is, When I am debugging user python code, I could see screen to Click here (please refer screenshot) while debugging using pydev. Bur when I click on "Click Here", it does nothing and remains on the same page. But if I debug the build in Python libraries provided by robot, it takes me inside that Python Library but fail to do so into user created Python Libaries. May I know how to fix this debugging issue? image

michalanglart commented 3 years ago

Hi,

Regarding 1: the transitive imports are supported by RED. I'm not sure if I understand this correctly but it seems that the file of unknown name visible at your first screenshot does not import CommonImport.robot, so RED do not see the keywords/variables defined by this file or imported by this file. How do you attach this file when you launch the first file? Is the first file only a resource file or a tests suite?

  1. Clicking on this link should open you the python file where keyword Login To Sfc is defined. This may likely not work if for some reason RED is not able to properly recognize this library. Actually this should have the same effect as if you would find Sfc_Common library in project explorer, then the Login To Sfc keyword and clicked Show keyword source from context menu like here: image is it possible for you?
ac57486 commented 3 years ago

FIRST ISSUE: This is how a flow goes --> Test (here CommonImports.robot is added)--> Step Definition (step declared in Tests is defined here) --> Keyword Definitions in Python Library (Keyword definitions, eg Open Browser Maximized) I will add screenshots one by one to show where CommonImports.robot has been added:

Test : Here Test case is written which is Chat.robot file. We import CommonImport.robot here so that it is available for test and step definitions image

Step Definitions: Here the step written in Test case is defined which is GeneralActions.robot

image

The below screenshot shows the Common variables files which is available globally. This ${browser} variable also shows error if you refer above screenshot

image

Keyword Definitions: : Here Keywords written in step definitions are defined. e.g. Open Browser Maximized

image

SECOND ISSUE : I could see Open Definition for Login To Sfc kyword and also I am able to open this keyword by clicking F3 or doing ctrl+click. But when I debug, I am unable to open this keyword:

Keyword

ac57486 commented 3 years ago

Hi,

Any update on above issues?

michalanglart commented 3 years ago

Hi,

sorry I had no time to look into that further. I'll try to do it today.

ac57486 commented 3 years ago

I would like to add one more point here for SECOND ISSUE i.e. Click here works if the library is in the same project(i.e. sfctests) but if it is in other project(for my case libraries are in sfctools) then Click Here is not working. PFA image

ac57486 commented 3 years ago

@michalanglart could you please help me here? It's been 14 days I have been waiting for the reply

michalanglart commented 3 years ago

Hi,

sorry for the delay but I was on a leave of absence. Additionally the team is now 2 out of 5 members couple of weeks ago + we're busy with other work assigned.

Anyway:

  1. about the ${browser} variable: have you looked in here: https://nokia.github.io/RED/help/user_guide/project_config/variable_files.html ? I'm assuming you're adding the variables file when running tests via --variablefile/-V option, right? You can add this file to red.xml and RED will see those variables in all the files inside this project.

  2. I now do understand your case with unrecognized keywords. You seem to have a setup like this:

tests.robot

*** Settings ***
Resource    common.robot
Resource    other.robot

*** Test Cases ***
test
    ${x}=  keyword
    Log    ${x}

common.robot

*** Keywords ***
keyword
    ${date}=    Get Current Date
    [Return]    ${date}

other.robot

*** Settings ***
Library    DateTime

RED in this case will mark Get Current Date keyword as unrecognized although if you would run tests.robot everything would run and the date would be printed. We do know about this behavior of Robot Framework although we were rather surprised when we found out a year ago or so. At the time it wasn't even documented as far as I remember (I don't know if it is now) and I remember talking to Pekka Klärck about this.

When RED is doing analysis of common.robot it does not know where Get Current Date is defined because there are no imports in this file. This is very slippery because what if you would create another file:

tests2.robot

*** Settings ***
Resource    common.robot

*** Test Cases ***
test
    ${x}=  keyword
    Log    ${x}

Now you would have an error when trying to run it. So what now should RED do? Should it mark as error or not? Or should it make even more complicated analysis and instead of error show a warning telling that: "This Get Current Date is fine when you import the file from tests.robot but would be an error when imported from tests2.robot"? We've decided to leave it as it is, so RED is seeing the keywords/variables from the files/libraries which are imported by this file or by other imported files (transitively). It however does not handle that kind of dependencies like in the example.

IMO apart from confusing IDE this case may also confuse other developers when reading the code even in some notepad - he/she may likely run into a question: "why does this keyword is used if the file I'm looking at does not import another where the keyword is defined? How does it work?".

  1. Regarding "Click here" issue: so your library is defined in different project but where is it added to red.xml? I need to try to reproduce it. Are you able to show us some projects with some dummy code like in my examples above?
ac57486 commented 3 years ago

@michalanglart Thank you for the detailed explanation for issue 1 and 2.

  1. To answer your question, yes I'm adding the variables file when running tests via --variablefile/-V option. I will try to add it in RED.xml and let you know of the result.
  2. Here, my understanding is, the error that comes for such keywords which are not directly imported is the expected behavior by RED. Is my understanding correct?
  3. I will add the dummy code by tomorrow.
ac57486 commented 3 years ago

Here is my sample code for point 3. I have two projects SampleTests and SampleTools. Here I would like to highlight one point i.e. The two projects could interact with each other with the help of python packaging and importing the libraries into the SampleTests project using relative path I have kept the test case and step definition in SampleTests, whereas, in SampleTools, I have defined the keywords used in step definition. eg. I have keyword "Login To Sfc" in step definition in SampleTests as given below:

SampleTests

Settings Variables SFC/PageObjects/Component/Sfc_QuickLinkComp.py Library SFC/Libraries/Sfc_Common.py

Keywords Login to application Login To Sfc ${user_profile}

Here, if you see Library, this is how I have imported Sfc_Common.py Library using relative path of the project. This library is created in SampleTools project. Hence the keyword "Login To Sfc" is defined in Sfc_Common.py library.

SampleTools:

@keyword def login_to_sfc(user_profile=None): sl = BuiltIn().get_library_instance('SeleniumLibrary') sl = BuiltIn().get_library_instance('SeleniumLibrary') sl.input_text(sfc_tbx_user_name, username) sl.input_text(sfc_tbx_password, password) click_element_using_javascript(sfc_btn_login)

PFA for the same, so that the explanation given above could be understood better:

SampleTests:

Please refer the highlighted parts: image

SampleTools:

Please refer the highlighted parts:

image

Please let me know in case of any confusion

michalanglart commented 3 years ago
  1. Here, my understanding is, the error that comes for such keywords which are not directly imported is the expected behavior by RED. Is my understanding correct?

Partly. As I said RED supports and will support recognizing keywords/variables which are imported transitively (so you have a.robot which imports b.robot which imports lib and RED will recognize keywords from lib inside first file although strictly speaking this library is not imported by a.robot). On the other hand we are not willing to support this awkward type of dependencies where a.robot is importing both b.robot and lib while b.robot imports nothing but Robot is able to recognize keywords from lib used inside b.robot.

As to the code you've placed here - I'll try to take a look today or tommorow. But just for now: those project does not seem to be configured as RED project, do they? If that's the case this may be the cause of not being able to click the keyword hyperlink. Can you configure the projects which have Robot code as RED projects (available from context menu Robot Framework -> Add Robot nature https://nokia.github.io/RED/help/user_guide/project_config/red_xml_overview.html)

ac57486 commented 3 years ago

Thanks for the clarification. As per the above project that was only a sample project I shared so that I could explain you the two projects. In my real project I have red.xml.

ac57486 commented 3 years ago

Regarding global variable files, I followed the approach suggested by you and added Variable files to red.xml, still that error persists. I followed the link shared. Is there any additional setting required to remove the red error? I have copied the screenshot for the same:

red.xml snapshot with Variable files:

image

.robot file with error:

image

michalanglart commented 3 years ago

Just to confirm:

  1. this red.xml is in the same project as the .robot file with ${browser}?
  2. have you revalidated whole project after saving red.xml?
ac57486 commented 3 years ago
  1. Yes red.xml is in the same project
  2. Yes, I have revalidated the project, restarted the eclipse and also cleaned the project
ac57486 commented 3 years ago

This is the one of the variable file env.py from where we are taking ${browser} variable. I have imported this into red.xml as variable files. So will this file be considered as variable file? I have observed error comes only for this browser variable imported from this file, variables imported from other variable files does not give error.

image

Here argument arg is passed from additional robot framework arguments as given below:

image

michalanglart commented 3 years ago

Lets check it: vars.py

browser = 'chrome'

def get_variables(arg=None):
    return {'var' : 'value'}

suite.robot

*** Test Cases ***
test
    Log    ${var}
    Log    ${browser}

Lets run it: python -m robot.run --variablefile vars.py suite.robot

and we have:

test                                                                  | FAIL |
Variable '${browser}' not found.

but ${var} was found and logged properly. So the answer is: yes it is a valid variables file, but only variables returned by get_variables function are available for Robot in this case. That's why RED also does not see it.

You would need to move your browser variable into the map returned by the function if you want to see it.

michalanglart commented 3 years ago

Also as a side note: you can add some completely dummy variable file into red.xml - it is used just for purpose of validation or content assistance.

In the past (like 2 years ago or so) the files specified in red.xml were also added into command line argument --variablefile when running the tests from the project within RED, but then we have introduced possibility to define Default launch configuration so if someone wanted to have some file added whenever running the tests it was better to use it, additionally it created some problems with ovverriding variables when they were both in default launch configuration and in red.xml so we've removed this possibility and now:

ac57486 commented 3 years ago

Actually, you have tried using ${browser} like this:

browser = 'chrome'

def get_variables(arg=None): return {'var' : 'value'}

That is why robot is not identifying it.

Could you please try like this:

browser = 'chrome'

def get_variables(arg=None): return {'browser' : browser}

Now robot will identify the ${browser} variable but Red would not. This is the way I am using ${browser} in my case.

image

michalanglart commented 3 years ago

Sorry I've missed your reply. Actually the file like this:

browser = 'chrome'

def get_variables(arg=None):
    return {'var' : 'value', 'browser' : browser}

does work for me and RED can see both ${var} and ${browser}