2019-04-24 13:55:26 +00:00
|
|
|
# A simple example code for wexpect written in python-2.7
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
import wexpect
|
2019-09-05 15:51:27 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
here = os.path.dirname(os.path.realpath(__file__))
|
2019-04-24 13:55:26 +00:00
|
|
|
|
|
|
|
# Path of python executable:
|
|
|
|
pythonInterp = sys.executable
|
|
|
|
prompt = ': '
|
|
|
|
|
|
|
|
# Start the child process
|
2019-09-05 15:51:27 +00:00
|
|
|
p = wexpect.spawn(pythonInterp, [here + '\\foo.py'])
|
2019-04-24 13:55:26 +00:00
|
|
|
|
|
|
|
# Wait for prompt
|
|
|
|
p.expect(prompt)
|
2019-09-05 15:51:27 +00:00
|
|
|
print(p.before)
|
2019-04-24 13:55:26 +00:00
|
|
|
|
|
|
|
# Send the 'small integer'
|
|
|
|
p.sendline('3')
|
|
|
|
p.expect(prompt)
|
2019-09-05 15:51:27 +00:00
|
|
|
print(p.before)
|
2019-04-24 13:55:26 +00:00
|
|
|
|
|
|
|
# print the texts
|
|
|
|
print(p.before, end='')
|
|
|
|
print(p.match.group(0), end='')
|
|
|
|
|
|
|
|
# Send the name
|
|
|
|
p.sendline('Bob')
|
|
|
|
|
|
|
|
# wait for program exit.
|
|
|
|
p.wait()
|
|
|
|
|
|
|
|
# print the texts
|
|
|
|
print(p.read(), end='')
|
|
|
|
|