Dwayne Pindling
Dwayne Pindling

Reputation: 77

Calling Robot Framework files from within Python

Have been in a bind over the weekend.

I am attempting to create a Python Application that will give users the ability to run .Robot files (or Test Cases) (from within Python)

I am attempting to run these files through the use of the 'subprocess' module, but I continually receive the following error message :

'FileNotFoundError: [WinError 2] The system cannot find the file specified'

I have included 'import subprocess'

I have also declared the location of the .Robot Test Case as a variable:

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

Within my code, I've attempted to call the Robotframework file with the following statement :

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

I am currently using Python 3.6.1 and Robot Framework version 3.0.2

Any help would be appreciated. If you know of a better means to accomplish the same task, please let me know.

Upvotes: 1

Views: 2009

Answers (2)

pankaj mishra
pankaj mishra

Reputation: 2615

lets assume that you are on windows platform and your robot file which you want t o run is in some other directory

you can use subprocess module to do the task for you like mentioned below

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

Upvotes: 1

jozefow
jozefow

Reputation: 786

My generic method for running external command from python scripts with stdout output:

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

I still don't know what happens when you try to run @Verv command (maybe pybot is not accessible in command prompt), so I would suggest try following:

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)

Check the output which may indicate what is wrong (tested locally).

Upvotes: 2

Related Questions