Sharing

2013年6月6日 星期四

DevOp 工具: twill - a simple scripting language for Web browsing

由於需要 monitor 一些 Service 的狀況, 所以必須寫一些 script 去測試網站能不能登入之類的, 所以需要模擬 browser 的行為。今天看到同事用這個工具來做這件事,當我發現他是用 Python 寫的工具,馬上試用了一下,覺得還滿好用的,簡單的操作應該都沒問題。而且他也承襲了 Python 的風格 => Interactive Prompt, 立即寫立即測試, 不然等所有的 code 寫完再測.

官網: http://twill.idyll.org/

以下先來示範一下如何登入 yahoo

>>> import twill.shell
>>> twill.shell.main()

 -= Welcome to twill! =-

current page:  *empty page*

>> help

Undocumented commands:
======================
add_auth             fa           info             save_html           title
add_extra_header     find         load_cookies     setglobal           url
agent                follow       notfind          setlocal
back                 formaction   redirect_error   show
clear_cookies        formclear    redirect_output  show_cookies
clear_extra_headers  formfile     reload           show_extra_headers
code                 formvalue    reset_browser    showforms
config               fv           reset_error      showhistory
debug                get_browser  reset_output     showlinks
echo                 getinput     run              sleep
exit                 getpassword  runfile          submit
extend_with          go           save_cookies     tidy_ok


>> go https://login.yahoo.com/config/login
==> at https://login.yahoo.com/config/login
current page: https://login.yahoo.com/config/login

>> showforms
Form name=login_form (#2)
## ## __Name__________________ __Type___ __ID________ __Value__________________
29    login                    text      username
30    passwd                   password  passwd
31    .persistent              checkbox  persistent   [] of ['y']
32 1  .save                    submi ... .save

>> setlocal login pjack1981
current page: https://login.yahoo.com/config/login
>> setlocal passwd xxxxxxxx
current page: https://login.yahoo.com/config/login
>> formvalue 2 login $login
current page: https://login.yahoo.com/config/login
>> formvalue 2 passwd $passwd
current page: https://login.yahoo.com/config/login

>> showforms
Form name=login_form (#2)
## ## __Name__________________ __Type___ __ID________ __Value__________________
29    login                    text      username     pjack1981
30    passwd                   password  passwd       xxxxxxxx
31    .persistent              checkbox  persistent   [] of ['y']
32 1  .save                    submi ... .save

>> submit
Note: submit is using submit button: name=".save", value=""

Following HTTP-EQUIV=REFRESH to http://my.yahoo.com
current page: http://my.yahoo.com
>> code 200
current page: http://my.yahoo.com

>> code 20

ERROR: code is 200 != 20

current page: http://my.yahoo.com


所以你可以先用 interactive mode 測一下你要的功能, 接下來就把你剛剛輸入的指令集合起來, 這樣就變成一個很簡單的測試 script 囉!

$ cat test.twill
setlocal login pjack1981
setlocal passwd xxxxxxxx

go https://login.yahoo.com/config/login
formvalue 2 login $login
formvalue 2 passwd $passwd
submit

code 200

$ twill-sh test.twill
>> EXECUTING FILE test.twill
AT LINE: test.twill:1
AT LINE: test.twill:2
AT LINE: test.twill:4
==> at https://login.yahoo.com/config/login
AT LINE: test.twill:5
AT LINE: test.twill:6
AT LINE: test.twill:7
Note: submit is using submit button: name=".save", value=""

Following HTTP-EQUIV=REFRESH to http://my.yahoo.com
AT LINE: test.twill:9
--
1 of 1 files SUCCEEDED.

其它 Example

http://stackoverflow.com/questions/2688408/how-can-i-put-all-twill-commands-together-into-one-piece-of-code-in-a-py-file

補充:
其它 testing tool for web browsing.
https://pypi.python.org/pypi/zope.testbrowser/4.0.2

沒有留言: