开发者

How to simulate keyboard input to a console application?

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 ...).开发者_如何学C 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


You should be able to pass in an input file like

 $ yourscript.sh < inputfile


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜