2011-09-28 213 views
2

我在bash中開發了一個應用程序,它使用「whiptail」在終端中顯示對話框。 (我個人不喜歡這種UI,但我只是一個開發者,我不會做出決定...)。無論如何,現在我必須測試它,我想模擬一個類型值的用戶,按「Enter」,「Echap」,「Tab」,「向下箭頭」,「向上箭頭」如何模擬鍵盤輸入到控制檯應用程序?

我didn'噸得到預計工作,它似乎是不可能的(http://oldsite.debianhelp.org/node/11812)。

編輯:機器上沒有X,所以xdotool不適用。我正在尋找一種不需要安裝任何東西的解決方案(因爲我們不允許向系統添加程序來測試它)。

長話短說,我正在尋找一種解決方案,如「寫入字節到進程的標準輸入」或「寫在/ dev上的鍵盤設備」,類似的東西。

感謝

+0

你可以用xdotool來做到這一點。看到這個答案:http://stackoverflow.com/questions/7312527/sending-a-keypress-to-an-application/7312865#7312865 – holygeek

+0

系統上沒有X,它只有一個控制檯應用程序 – user368507

+1

[這是一種方法](http://stackoverflow.com/questions/7370538/ask-a-running-bash-interactive-to-run-a-command-from-outside/7370822#7370822) –

回答

1

您應該能夠在輸入文件傳似

$ yourscript.sh < inputfile 
+0

我試過了,它不起作用: root @ antec#echo「user input here」>/tmp/test; whiptail - 輸入框「測試」20 50 user368507

1

你猛砸應用程序需要pseudo terminal才能正常運行。它需要屏幕大小和光標位置,但是如果使用管道輸入(<|)運行它,則不會創建僞終端。

虛擬終端在Unix上被日常應用程序創建,如ssh,xtermscreen。 (Expect會爲應用程序創建一個僞終端,並允許您運行自動測試。它支持測試生成與autoexpect,並存在紙張上using Expect for terminal screen-scraping。)

如果您不能使用期望,你可以嘗試使用screen用於自動化終端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 
相關問題