Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Automating user input

by Nishchita (Initiate)
on Feb 06, 2013 at 12:30 UTC ( [id://1017404]=perlquestion: print w/replies, xml ) Need Help??

Nishchita has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks. Ok, so, I have a situation. There is a script which needs user input. & I need to automate running of this script. Which means I cannot provide the user inputs as <STDIN>. Expect module is not an option for me as the scripts need to run on Windows. Any suggestions? Here is a sample code.

TobeAutomated.pl my $count = 0; while ($count < 2) { $count ++; print "Question? y/n: "; chomp(my $answer = <>); print "You said `$answer`\n"; }

I am not allowed to edit the above script. Now I need to automate running the above script. And my answer needs to be "y" followed by "n" I tried `echo y|perl TobeAutomated.pl` This takes care of One user input as "y" . I don't know how to feed in the second input "n" Any leads will be much appreciated !

Replies are listed 'Best First'.
Re: Automating user input
by BrowserUk (Patriarch) on Feb 06, 2013 at 12:38 UTC

    C:\test>copy con answers.txt y n ^Z 1 file(s) copied. C:\test>type tobeautomated.pl my $count = 0; while ($count < 2) { $count ++; print "Question? y/n: "; chomp(my $answer = <>); print "You said `$answer`\n"; } C:\test>type answers.txt | perl tobeautomated.pl Question? y/n: You said `y` Question? y/n: You said `n` C:\test>

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Automating user input
by kennethk (Abbot) on Feb 06, 2013 at 15:45 UTC
    If you need to be actually interactive rather than just stream inputs, there are a number of solutions discussed in perlipc. IPC::Open2 is my guess on your best bet in that case.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Automating user input
by Plankton (Vicar) on Feb 06, 2013 at 21:10 UTC
Re: Automating user input
by sam_bakki (Pilgrim) on Feb 07, 2013 at 06:23 UTC

    Hi

    You can try Win32::GuiTest http://search.cpan.org/~karasik/Win32-GuiTest-1.60/lib/Win32/GuiTest.pm and using FindWindowLike , SendKeys APIs, you can send key strokes to Any window (including cmd prompt)

    I have done a script some time back to do the similar job.

    1. start the app / script which needs an input in background and redirect the output to file, (Ex:start perl.exe abc.pl > out.txt 2>&1 )
    2. Monitor out.txt in your automate script and look for patterns to enter your input
    3. use Win32::GuiTest and send keystroke to the app/script which was started in background.

    Thanks & Regards,
    Bakkiaraj M
    My Perl Gtk2 technology demo project - http://code.google.com/p/saaral-soft-search-spider/ , contributions are welcome.

Re: Automating user input
by Nishchita (Initiate) on Feb 10, 2013 at 08:37 UTC

    Wow, thanks! The type method suggested by BrowserUk is what I have implemented as of now. I will look at the other solutions provided & update y'all!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1017404]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found