Python測(cè)試開(kāi)源工具splinter安裝與使用教程
Splinter是一個(gè)使用Python測(cè)試Web應(yīng)用程序的開(kāi)源工具,可以自動(dòng)化瀏覽器操作,例如訪問(wèn)URL和與它們的項(xiàng)進(jìn)行交互。例如,我們使用百度引擎搜索內(nèi)容,需要再搜索框內(nèi)輸入關(guān)鍵字,再按百度一下即可以搜索想要的內(nèi)容,使用Splinter可以使用pyhton腳本來(lái)實(shí)現(xiàn)上述過(guò)程。
Splinter安裝Splinter的使用需要依賴python環(huán)境,因此首先需要裝python(python安裝可以直接安裝anaconda集成環(huán)境,網(wǎng)上一搜教程很多~),并且python版本需要是2.7+;以下是Splinter的官網(wǎng)說(shuō)明:
In order to install Splinter, make sure Python is installed. Note: only Python 2.7+ is supported.
Splinter安裝Splinter安裝,官網(wǎng)提供了兩種版本安裝,一般使用穩(wěn)定版本即可:
pip install splinter # pip工具首先得安裝,如果安裝anaconda則會(huì)自動(dòng)安裝pip驅(qū)動(dòng)安裝
要使用splinter訪問(wèn)瀏覽器,還需要安裝對(duì)應(yīng)的瀏覽器驅(qū)動(dòng),這里以chrome為例,由于chrome WebDriver依賴于Selenium2,最終需要安裝兩個(gè):即Selenium2和chromedriver。
1. Selenium2直接通過(guò)pip安裝:
pip install selenium
2. 對(duì)于chromedriver,首先查看瀏覽器版本,在chrome瀏覽器訪問(wèn):chrome://version/ 。
然后訪問(wèn)http://chromedriver.storage.googleapis.com/index.html,找到對(duì)應(yīng)的版本下載即可。
下載解壓后,會(huì)得到一個(gè)chromedriver.exe文件,按照官網(wǎng)的說(shuō)法,需要將其配置環(huán)境變量。簡(jiǎn)單的做法,直接將chromedriver.exe文件放在python安裝的根目錄(即和python.exe放在同一個(gè)目錄===這是因?yàn)閜ython.exe所在的目錄肯定配置了環(huán)境變量)。到這里,環(huán)境配置已經(jīng)OK了,接著就是寫(xiě)python腳本測(cè)試了~
python腳本測(cè)試Splinterfrom splinter import Browserfrom time import sleepbrowser = Browser(’chrome’) # 創(chuàng)建瀏覽器實(shí)例browser.visit(’https://www.baidu.com’)# 訪問(wèn)baidu# 將關(guān)鍵詞填入搜索框 通過(guò)wd這個(gè)名字找到對(duì)應(yīng)的Elementsbrowser.fill(’wd’, ’splinter - python acceptance testing for web applications’) browser.find_by_id(’su’).click() # 通過(guò)id找到點(diǎn)擊按鈕,并點(diǎn)擊if browser.is_text_present(’splinter.readthedocs.io’): # 對(duì)響應(yīng)結(jié)果進(jìn)行處理 print('Yes, the official website was found!')else: print('No, it wasn’t found... We need to improve our SEO techniques')sleep(10)browser.quit() # 關(guān)閉瀏覽器
其中,browser = Browser(’chrome’)的’chrome’參數(shù)是必須的,如果不指定的話,默認(rèn)選用火狐瀏覽器,詳見(jiàn)官網(wǎng)說(shuō)明。
結(jié)果:
到此這篇關(guān)于Python測(cè)試開(kāi)源工具splinter安裝與使用教程的文章就介紹到這了,更多相關(guān)python splinter安裝與使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
