[英]Command Line arguments to Robot framework via python file
language : python - 3.7.3 Robot framework - 3.1.1 proficiency : Novice 语言 :python-3.7.3 机器人框架 -3.1.1 能力 :新手
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. 我想从命令提示符处将参数传递给python文件变量,然后通过将python文件添加为变量来从机器人文件中打印相同的参数。
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. 注意: Robot和Python文件都在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 usr是:test@test.com pwd是:test @ 123环境是: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
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. Robot使用--variable
(和-v
)命令行选项对此提供了支持。 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
: 您可以使用-v
或--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: 如果您需要在python函数中访问这些变量而不传递它们,则可以使用内置关键字get_variable_value来获取值:
from robot.libraries.BuiltIn import BuiltIn
...
user = BuiltIn().get_variable_value('${USER}')
I am not quite sure about this, I am guessing we should also pass the name of the script to sys.argv 我对此不太确定,我猜我们也应该将脚本名称传递给sys.argv
sys.argv[0] in the below example, is the name of the script passed argument. 在下面的示例中,sys.argv [0]是脚本传递的参数的名称。
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 参考: https : //www.pythonforbeginners.com/system/python-sys-argv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.