Splinter 快速介紹
官方網站:http://splinter.cobrateam.info/
官方介紹:
Splinter is an open source tool for testingweb applications using Python. It lets you automate browser actions, such asvisiting URLs and interacting with their items
特性:
1、可以類比瀏覽器行為,訪問指定的URL,並且可以指定不同的瀏覽器類型。比如firefox或者chrome等。不同的瀏覽器只要在本地安裝對應的驅動,就可以在代碼中通過名稱指定來訪問。
2、支援cookie操作,可以很方便的添加和刪除cookie;
3、支援類比滑鼠的動作,比如滑動到某個按鈕上,焦點離開某個按鈕等等,對於帶有動態提示的頁面,如搜尋引擎的關鍵字輸入框的動態提示,可以非常方便的測試。
4、支援類比鍵盤的輸入操作,對input等控制項的輸入可以類比使用者的type過程。
5、支援直接運行js或者調用頁面的js。
6、支援類比上傳檔案。
7、對radio和checkbox有專門的api支援,非常方便;
8、支援快速的擷取頁面的元素或者判斷是否存在文本,用於開發判斷頁面提示資訊是否準確非常方便。
9、最重要的,splinter的API非常簡單,配合官方的文檔學習成本幾乎是0,當然你得懂一些python文法。如果你比較瞭解js和css,你可能會像喜歡jquery一樣喜歡它;
功能:
Splinter執行的時候會自動開啟你指定的瀏覽器,訪問指定的URL。
然後你所開發的類比的任何行為,都會自動完成,你只需要坐在電腦面前,像看電影一樣看著螢幕上各種動作自動完成然後收集結果即可。
舉個例子,我們要迴歸登入功能,首先要開發如下類比登入行為的指令碼:
複製代碼 代碼如下:
#!/usr/bin/py2
# -*- coding: utf-8 -*-
#encoding=utf-8
import sys, re
from splinter.browser import Browser
CLOASE_AFTER_TEST = False
reload(sys)
sys.setdefaultencoding('utf8')
encoding = lambda x:x.encode('gbk')
def testLogin(desc, username, password, result):
output(desc)
browser.fill('TPL_username',username.decode('utf8'))
browser.fill('TPL_password',password.decode('utf8'))
browser.find_by_value('登入').first.click()
checkresult(result)
def output(x):
print encoding(x)
def resultMsg(x):
if x == True:
print 'pass'
else:
print '[X]not pass'
def checkresult(x):
""" check result message, x : the error message u want """
resultMsg(browser.is_text_present(x))
__testUrl = 'http://waptest.taobao.com/login/login.htm?tpl_redirect_url=http%3A%2F%2Fm.taobao.com%2F'
# chrome driver : http://code.google.com/p/selenium/wiki/ChromeDriver
browser = Browser() # already support firefox
browser.visit(__testUrl)
output("測試頁面:"+browser.title)
try:
# test login
testLogin('測試未輸入使用者名稱','','','請輸入會員名')
testLogin('測試未輸入密碼','qd_test_001','','請輸入密碼')
testLogin('測試帳戶不存在','這是一個不存在的名字哦','xxxxxxx','該賬戶名不存在')
testLogin('測試成功登入','qd_test_001','taobao1234','繼續登入前操作')
# test find password
output("測試[找回密碼]連結")
browser.visit(__testUrl)
backPasswordLink = browser.find_link_by_text('取回密碼')
if 1 == len(backPasswordLink):
backPasswordLink.first.click()
ru = re.findall(re.compile(".*(reg/gp.htm).*", re.IGNORECASE), browser.url)
if ru is not None:
checkresult('找回密碼')
else:
output("測試找回密碼連結失敗")
except Exception,x:
print x
if CLOASE_AFTER_TEST:
browser.quit()