【问题标题】:Calling Robot Framework files from within Python从 Python 中调用 Robot Framework 文件
【发布时间】:2018-05-03 17:12:25
【问题描述】:

周末一直处于困境中。

我正在尝试创建一个 Python 应用程序,让用户能够运行 .Robot 文件(或测试用例)(在 Python 中)

我正在尝试通过使用“子进程”模块来运行这些文件,但我不断收到以下错误消息:

'FileNotFoundError: [WinError 2] 系统找不到文件 指定'

我已包含“导入子流程”

我还将 .Robot 测试用例的位置声明为变量:

Location = r'C:/Users/verti/PycharmProjects/ChristSax.com/ChristSaxTestSuite/PageSmokeTest.Robot'

在我的代码中,我尝试使用以下语句调用 Robotframework 文件:

def AutomatedSmokePage():
    print("Automated Page Smoke Tests now running...")
    subprocess.call(['pybot',Location])

我目前使用 Python 3.6.1 和 Robot Framework 3.0.2 版

任何帮助将不胜感激。如果您知道完成相同任务的更好方法,请告诉我。

【问题讨论】:

  • 我会首先调用 Robot 而不是通过 pybot 而是作为“python -m robot.run”命令,而不是尝试将机器人调用与 Location 连接为一个字符串。
  • 谢谢你的例子,如果不是太麻烦,你能提供一个简单的例子来说明如何做到这一点吗?
  • 如果你只打开一个shell/cmd并输入pybot C:/Users/verti/PycharmProjects/ChristSax.com/ChristSaxTestSuite/PageSmokeTest.Robot会发生什么
  • @Verv:当我在命令提示符中键入位置并单击“Enter”时,.Robot 测试用例开始运行。如果我将位置复制到命令提示符中,空间将显示在 Suite & Robot 中(即 ChristSaxTestSu ite、PageSmokeTest.Ro bot)
  • 对于为什么相同的位置变量适用于某些命令而不适用于其他命令,我仍然有点困惑。我发现以下内容也适用于我:os.chdir("C:/Users//verti/PycharmProjects/ChristSax.com/Chr‌​istSaxTestSuite/")os.system('pybot -N PageSmokeTest -r ' 'AutomatedSmokeTest.html -l AutomatedSmokeTest.html -o ' 'AutomatedSmokeTest.xml AutomatedSmokeTest.robot')

标签: python python-3.x robotframework


【解决方案1】:

我使用标准输出从 python 脚本运行外部命令的通用方法:

import sys
import subprocess

def run_process(command):
    print("Running command: " + command)
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

    while True:
        if sys.version_info >= (3, 0):
            nextline = str(p.stdout.readline(),"utf-8")
        else:
            nextline = p.stdout.readline()
        if nextline == '' and p.poll() is not None:
            break
        sys.stdout.write(nextline)
        sys.stdout.flush()

我仍然不知道当您尝试运行@Verv 命令时会发生什么(可能在命令提示符下无法访问 pybot),所以我建议您尝试以下操作:

python_path = 'c:/Python36/python.exe -m robot.run'
Location ='C:/Users/verti/PycharmProjects/ChristSax.com/ChristSaxTestSuite/PageSmokeTest.Robot'
command=python_path+' '+Location
run_process(command)

检查可能表明有什么问题的输出(本地测试)。

【讨论】:

  • 不幸的是,当我使用上面的代码时,我收到的错误信息是:系统找不到指定的路径。
  • 尝试限制“command”部分以调试系统找不到指定路径的原因 - 从 command=' c:/Python36/python.exe --version' 开始,稍后在 command=' c:/ Python36/python.exe -m 机器人.libdoc --help'。一定有一些小而愚蠢的问题隐藏在显而易见的地方。
【解决方案2】:

假设您在 Windows 平台上,并且您想要运行的机器人文件位于其他目录中

您可以使用子流程模块为您完成如下所述的任务

from subprocess import Popen 
from subprocess import Popen, CREATE_NEW_CONSOLE,PIPE,STDOUT
import platform 
class Server:
    def __init__(self):
        pass
    def run_robotfiles(self,terminal,command1):
        if platform.system()=='Windows':
            self.terminal=terminal
            self.command1=command1
            self.command_server1=self.terminal+' '+self.command1
            self.server1_start=Popen(self.command_server1,creationflags=CREATE_NEW_CONSOLE)

abc=Server()
#you can give path till the robot file or you can use a batch file
#K option tells cmd to run the command and keep the command window from closing. You may use /C instead to close the command window after the command finishes.
abc.run_robotfiles('cmd','/K pybot D:\Users\process.robot')

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2020-07-12
    • 1970-01-01
    • 2019-11-03
    • 2018-01-29
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多