3

language: python - 3.7.3 Robot framework - 3.1.1 proficiency: Novice

I want to pass arguments to python file variables from command prompt and then want to print the same from robot file by adding python file as variable.

cmd (Variables) --> python file --> robot (print those variables) 

But apparently none of the try has yielded expected results. Any guidance will be highly appreciated.

Note: Both Robot and Python files are under same directory in pycharm IDE.

Command Line:-

1st try: robot -d Report -V test.py Test_sample.robot|py test.py test@test.com test@123 QA1

2nd try: robot -d Report Test_sample.robot|py test.py test@test.com test@123 QA1

3rd try: robot -d Report --Variablefile py test.py Test_sample.robot|py test.py test@test.com test@123 QA1

4th try: robot -d Report -V | py test.py test@test.com test@123 QA1| Test_sample.robot

Python:-

import sys

username = str(sys.argv[1])
password = str(sys.argv[2])
environment = str(sys.argv[3])

Robot:-

*** Settings ***
Library     SeleniumLibrary
Library     Collections
Variables  test.py

*** Test Cases ***
Pass variables from Python File

        log to console  The usr is: "${username}"
        log to console  The pwd is: "${password}"
        log to console  The env is: "${environment}"

Expected:

The usr is: test@test.com The pwd is: test@123 The env is: QA1

Actual:

[ ERROR ] Unexpected error: OSError: [Errno 22] Invalid argument
Traceback (most recent call last):
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\utils\application.py", line 83, in _execute
    rc = self.main(arguments, **options)
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\run.py", line 439, in main
    result = suite.run(settings)
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\running\model.py", line 222, in run
    self.visit(runner)
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\model\testsuite.py", line 168, in visit
    visitor.visit_suite(self)
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\model\visitor.py", line 84, in visit_suite
    if self.start_suite(suite) is not False:
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\running\runner.py", line 83, in start_suite
    test_count=suite.test_count))
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\output.py", line 51, in start_suite
    LOGGER.start_suite(suite)
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\logger.py", line 200, in start_suite
    logger.start_suite(suite)
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\console\verbose.py", line 35, in start_suite
    self._writer.suite_separator()
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\console\verbose.py", line 104, in suite_separator
    self._fill('=')
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\console\verbose.py", line 110, in _fill
    self._stdout.write('%s\n' % (char * self._width))
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\console\highlighting.py", line 53, in write
    self.flush()
  File "<<Dir path>>\python\python37-32\lib\site-packages\robot\output\console\highlighting.py", line 66, in flush
    self.stream.flush()
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
OSError: [Errno 22] Invalid argument
Learner
  • 481
  • 1
  • 15
  • 28
  • Are you aware that robot has a mechanism for setting variables from the command line with the --variable option? There's no need for you to get information out of `sys.argv`. – Bryan Oakley May 19 '19 at 14:59
  • @BryanOakley: Yes, I tried with -v (Variable) and -V(Variable File). I am able to get the variable printed if I am running python file alone but problem starts only when I am trying to print the same from robot file as well at the same time as I described above. – Learner May 19 '19 at 15:10
  • It's unclear exactly what problem you're trying to solve. You can set variables from the command line with --variable. Are you actually asking how to print the value of a robot value from a python library? Or are you trying to figure out something that works both with robot and without robot? – Bryan Oakley May 19 '19 at 15:15
  • @BryanOakley: Sorry if my description is not clear enough. My aim is to pass credentials from command line. So that, other colleagues can run my automation script with their own credentials. So for the operation, I decided to get the credentials from command line initially, store it in python and pass it on to robot file for further operation. Now, it looks like python clears variable once execution done. CMD LINE>>robot -d Report test_sample.robot && py Methods.py Prasad Rajassekaran 33 ;;; Result:: The Query is: select * from table where username=-d and password=Report – Learner May 19 '19 at 15:22

2 Answers2

5

In a comment you wrote:

My aim is to pass credentials from command line.

Robot has support for that using the --variable (and -v) command line option. Using this will overwrite any imported variable or variable defined in a robot file.

For example, you can set defaults for your credentials inside the test itself:

*** Variables ***
${USER}         test@test.com
${PASSWORD}     test@123
${ENVIRONMENT}  QA1

*** Test cases ***
Example
    log to console  \n*****
    log to console  username: ${USER}
    log to console  password: ${PASSWORD}
    log to console  environment: ${ENVIRONMENT}

You can modify those values from the command line using -v or --variable:

$ robot -v USER:bob -v PASSWORD:bob@123  -v ENVIRONMENT:QA2 ...

If you need to access these variables in a python function without passing them in, you can use the built-in keyword get_variable_value to fetch the value:

from robot.libraries.BuiltIn import BuiltIn
...
user = BuiltIn().get_variable_value('${USER}')
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
1

I am not quite sure about this, I am guessing we should also pass the name of the script to sys.argv

sys.argv[0] in the below example, is the name of the script passed argument.

import sys
print "This is the name of the script: ", sys.argv[0]
print "Number of arguments: ", len(sys.argv)
print "The arguments are: " , str(sys.argv)

reference: https://www.pythonforbeginners.com/system/python-sys-argv