Running external batch file with Robot

1,718 views
Skip to first unread message

Pasi

unread,
Jun 16, 2009, 4:55:57 AM6/16/09
to robotframework-users
Hi,

I'm trying to run external DOS batch file which has 2 different
choices presented to user on Windows XP. I'm able to run the batch
file, and automatically input the answer to the first choice question,
but the second automatic answer is always unsuccessful - meaning that
the second choice just stays waiting for input without continuing.

Here's a snippet of the batch file, should work as such:
@ECHO OFF
choice /n CHANGE DEPLOYMENT [Y-N]:
if errorlevel 2 (
echo NO selected!
) else (
echo YES selected!
)

choice REBOOT
IF ERRORLEVEL 2 (
echo No reboot selected!
) else (
echo Reboot selected!
)
----

And here's the Python code:
import subprocess

script = """NN"""
commandtorun = ["test.bat"]
process = subprocess.Popen(commandtorun, stdin=subprocess.PIPE,
shell=True)
process.communicate(input=script)
---

So, the first choice is automatically answered "no" as ordered in
Python, second answer goes to some place unknown to me. Any ideas how
I could automate this whole thing?

I'm running Robot Framework 2.1 (Python 2.6.2 on win32).

Thank you in advance!

Cheers,
Pasi Tiainen

Juha Rantanen

unread,
Jun 22, 2009, 7:28:14 AM6/22/09
to pasi.t...@nsn.com, robotframework-users
2009/6/16 Pasi <pasi.t...@nsn.com>:
Hi,

The Popen.communicate can be used to give only one value. It cannot be
used interactively. One option is to use stdin.write() and
stdout.read() methods. See example below:

import subprocess

process = subprocess.Popen(["test.bat"], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, shell=True)
process.stdin.write('N')
out = ''
while not 'REBOOT[Y,N]?' in out:
out += process.stdout.read(1)
print out,
process.stdin.write('N')
print process.stdout.read()


Br,
Juha
Reply all
Reply to author
Forward
0 new messages