robotcodedev / robotcode

RobotFramework support for Visual Studio Code
https://robotcode.io
Apache License 2.0
173 stars 14 forks source link

[BUG] <Define Library with Variable> #81

Closed qiangge199551 closed 1 year ago

qiangge199551 commented 1 year ago

Describe the bug A clear and concise description of what the bug is. i define a library with a robot variable, that define with a python file. we will get it at robot runing. i add the variable file, library file to the robotcode extension in vscode settings, however, the robotcode extension can not find the library. all keywords underline with ant line. can you tell me why? and how to resolve it.

Desktop (please complete the following information):

d-biehl commented 1 year ago

Can you be a little more specific? I do not understand exactly yet? Maybe with an example?

qiangge199551 commented 1 year ago

I sincerely apologize to you that there was no detailed project description in the previous question due to lack of time.

this is my project: ├─library │ ├─Lib_A │ │ └─Keyword_1.py # robot keyword defined by python file │ └─Lib_B │ │ └─Keyword_2.py ├─Project │ ├─Keywords │ │ └─User_Defined_Keywords # robot keyword defined with keywords in Keyword_1.py │ ├─TC │ │ ├─Project_1 │ │ └─tc.robot # robot script │ │ ├─variable.py # robot variable │ │ └─config.yaml # robot variable │ │ ├─start.py # start up

we will use python start.py to start robot, that define robot parameters and robot scripts. in the tc.robot, i import library likes Resource ${User_Defined_Keywords}, that defined in config.yaml. we will get config.yaml by variable.py through function get_variables. i add the config.yaml, variable.py, Keyword_1, User_Defined_Keywords to the robotcode extension in vscode settings, however, the robotcode extension can not find the library, keywords and variables.

d-biehl commented 1 year ago

Ok I understand a little bit more. If I get it correctly you want to use a variable that ist defined in a dynamic variable file. For robotcode it is not possible at the moment to use the dynamic variable files, because robotcode makes only a static code analysis and cannot get dynamically calculated values.

The question that I ask me is, why you implemented this in that way? What is the purpose of that? Maybe we can find another another way to get what you need.

Here some other question:

qiangge199551 commented 1 year ago

Sincerely thanks for your help!The questions are answered as follows:

  1. We both have variables config.yml and variables.py in my project for different purpose, but not only these two config files. My customer(non developer) will modify some variable in config.yaml. And developer will define some variables in variable.py.
  2. Robot runs in a suite, that with many robot files and config files. All the robot files may compose different suites.It's difficult for customer to add parameters to robot.So, we need the start.py, that will compose parameters automatically for different suite.
  3. I know the argument -P could add python path to robot. The reason is same to previous.
  4. For example, Lib_A is a keyword collection that expands SeleniumLibrary.But Lib_B is a keyword collection that cotrols printer.
  5. My project has dozens of python libraries.In addition, robot resource is the same to that.The libraries and robot scripts belong to different project.Using variables to import library but not impoting library directly, the robot script will be more succinct and easier to understand.We are committed to making robot scripts easier to understand for customer.
d-biehl commented 1 year ago

Sorry for the late response,...

The problem is, RobotCode does not know about your start.py and for RobotCode reading variables from .py files is not really possible. RobotCode can get the name, but not all values, depending what type of variables you use. I don't know if its really easier for you customers to write variables, instead of using the real resource or library names. And also RobotCode must know where it can find your Libraries/Resource files to include them correctly.

Instead of using a start.py use an argument file (see -A option of robot) here you can also define variables as commandline variables. Define ${User_Defined_Keywords} with the correct path and in RobotCode there is a setting For RoboCode you can use the robotcode.robot.variables setting to specify the same. You start.py becomes then maybe a start.cmd or start.sh depending on your os with one line robot -A arguments.txt

But I think the easiest way is using the python path argument, instead if using variables. Just put every folder where robot should look for python libraries or resource file to the python path, (-P option for robot, robotcode.robot.pythonPath setting for robotcode) then you can write in robot files something like:

*** settings ***
Library  Keyword_1
Library  Keyword_2
Resource  User_Defined_Keywords.resource

and you don't need variables for that. Another point for RobotCode you have also code completion support for imports, just type Library or Resource or press controlspace after that import statement, to get the list of possible libraries/resources.

I will close this issue, because it is not really an issue, just a complex project configuration. But we can continue to discuss this topic here.