gpt4 book ai didi

python - 通过 python 文件向 Robot 框架传递命令行参数

转载 作者:太空宇宙 更新时间:2023-11-04 08:26:58 25 4
gpt4 key购买 nike

语言:python - 3.7.3机器人框架 - 3.1.1熟练程度:新手

我想从命令提示符将参数传递给 python 文件变量,然后想通过添加 python 文件作为变量从机器人文件打印相同的内容。

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

但显然所有的尝试都没有产生预期的结果。任何指导将不胜感激。

注意:Robot 和 Python 文件在 pycharm IDE 中位于同一目录下。

命令行:-

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])

机器人:-

*** 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}"

预期:

usr 是:test@test.com密码是:test@123环境是:QA1

实际:

[ 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

最佳答案

在您写的评论中:

My aim is to pass credentials from command line.

Robot 支持使用 --variable(和 -v)命令行选项。使用它会覆盖任何导入的变量或机器人文件中定义的变量。

例如,您可以在测试本身中为您的凭据设置默认值:

*** 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}

您可以使用 -v--variable 从命令行修改这些值:

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

如果需要在 python 函数中访问这些变量而不传入它们,可以使用内置关键字 get_variable_value获取值:

from robot.libraries.BuiltIn import BuiltIn
...
user = BuiltIn().get_variable_value('${USER}')

关于python - 通过 python 文件向 Robot 框架传递命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com