Python測試開源工具splinter安裝與使用教程
Splinter是一個使用Python測試Web應(yīng)用程序的開源工具,可以自動化瀏覽器操作,例如訪問URL和與它們的項進(jìn)行交互。例如,我們使用百度引擎搜索內(nèi)容,需要再搜索框內(nèi)輸入關(guān)鍵字,再按百度一下即可以搜索想要的內(nèi)容,使用Splinter可以使用pyhton腳本來實現(xiàn)上述過程。
Splinter安裝Splinter的使用需要依賴python環(huán)境,因此首先需要裝python(python安裝可以直接安裝anaconda集成環(huán)境,網(wǎng)上一搜教程很多~),并且python版本需要是2.7+;以下是Splinter的官網(wǎng)說明:
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則會自動安裝pip驅(qū)動安裝
要使用splinter訪問瀏覽器,還需要安裝對應(yīng)的瀏覽器驅(qū)動,這里以chrome為例,由于chrome WebDriver依賴于Selenium2,最終需要安裝兩個:即Selenium2和chromedriver。
1. Selenium2直接通過pip安裝:
pip install selenium
2. 對于chromedriver,首先查看瀏覽器版本,在chrome瀏覽器訪問:chrome://version/ 。
然后訪問http://chromedriver.storage.googleapis.com/index.html,找到對應(yīng)的版本下載即可。
下載解壓后,會得到一個chromedriver.exe文件,按照官網(wǎng)的說法,需要將其配置環(huán)境變量。簡單的做法,直接將chromedriver.exe文件放在python安裝的根目錄(即和python.exe放在同一個目錄===這是因為python.exe所在的目錄肯定配置了環(huán)境變量)。到這里,環(huán)境配置已經(jīng)OK了,接著就是寫python腳本測試了~
python腳本測試Splinterfrom splinter import Browserfrom time import sleepbrowser = Browser(’chrome’) # 創(chuàng)建瀏覽器實例browser.visit(’https://www.baidu.com’)# 訪問baidu# 將關(guān)鍵詞填入搜索框 通過wd這個名字找到對應(yīng)的Elementsbrowser.fill(’wd’, ’splinter - python acceptance testing for web applications’) browser.find_by_id(’su’).click() # 通過id找到點擊按鈕,并點擊if browser.is_text_present(’splinter.readthedocs.io’): # 對響應(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)選用火狐瀏覽器,詳見官網(wǎng)說明。
結(jié)果:
到此這篇關(guān)于Python測試開源工具splinter安裝與使用教程的文章就介紹到這了,更多相關(guān)python splinter安裝與使用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
