3

I have developed an application in bash that uses "whiptail" to display dialogs in a terminal. (I personnally don't like this kind of UI but i'm only a developer, i don't make decisions ...). Anyway, now i have to test it, and i would like to simulate a user that types values, press "Enter", "Echap", "Tab", "down arrow", "up arrow"

I didn't get expect working and it seems it is not possible (http://oldsite.debianhelp.org/node/11812).

Edit: There is no X on the machine, so xdotool is not suitable. I'm looking for a solution that doesn't need to install anything (because we are not allowed to add programs to the system to test it).

Long story made short, i'm looking for a solution like "writing bytes to the process's stdin" or "writing on the keyboard device in /dev", something like that.

Thanks

user368507
  • 1,388
  • 1
  • 13
  • 25
  • You can do it with xdotool. See this answer: http://stackoverflow.com/questions/7312527/sending-a-keypress-to-an-application/7312865#7312865 – holygeek Sep 28 '11 at 22:26
  • There is no X on the system, its only a console application – user368507 Sep 29 '11 at 07:30
  • 1
    [Here's one method](http://stackoverflow.com/questions/7370538/ask-a-running-bash-interactive-to-run-a-command-from-outside/7370822#7370822) – n. m. could be an AI Oct 01 '11 at 15:45
  • You are a life saver ! You found the answer i was looking for, can you make a true "answer" so that i can accept it ? thanks. – user368507 Oct 01 '11 at 17:21

2 Answers2

2

You should be able to pass in an input file like

 $ yourscript.sh < inputfile
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • I tried this and it doesn't work: root@antec# echo "user input here" > /tmp/test; whiptail --inputbox "test" 20 50 < /tmp/test – user368507 Sep 28 '11 at 22:30
1

Your Bash application requires a pseudo terminal in order to run properly. It needs a screen size and a cursor position, but if you run it with piped input (< or |), no pseudo terminal gets created.

Pseudo terminals get created in Unix by everyday applications like ssh, xterm, and screen. (Expect will create a pseudo terminal for your application and allow you to run automated tests. It supports test generation with autoexpect, and there is a paper on using Expect for terminal screen-scraping.)

If you can't use Expect, you can try using screen for automated terminal I/O:

# Create a detached screen
screen -S screenname -d -m -s ./my_app

# Send input to it
screen -S screenname -p windownum -X eval \
  "register . \"arbitrary\ntext, newlines and control chars\n\"" paste

# Wait for the application to process the input
sleep 0.1s

# Dump the screen to a file
screen -S screenname -p windownum -X hardcopy ./screen_dump

# Check the dump
grep 'Login successful' ./screen_dump || exit 1

# Rinse and repeat

# Close the screen
screen -S screenname -X quit
Dan Cecile
  • 2,403
  • 19
  • 21