利用 python 來執行 selenium 自動測試

python 撰寫 selenium 自動化測試流程的相關環境設定;
1. 安裝 python (本範例以 python 2.6.6 進行):

請參考「在 Windows 7 環境安裝 Python 2.6.6」
我的電腦/進階系統設定/環境變數/
path c:\Pthon27

2.  安裝 pip:

pip 是目前最受歡迎的 python 套件管理程式,被視為 easy_install 套件管理的取代品

https://pypi.python.org/pypi

到 https://pip.pypa.io/en/latest/installing/ 下載連結 https://bootstrap.pypa.io/get-pip.py

安裝方式:

(1) 在 command line 下

(2) 執行指令進行安裝:python pip.py install

(3) 我的電腦/進階系統設定/環境變數/  path c:\Pthon27\Script

(4) 在 command line 下執行指令進行安裝:pip install requests

3. 安裝 selenium:

在 command line 下輸入以下指令進行安裝:

pip install -U selenium

 4. 安裝 purl (optional):

purl 是 python 功能強大方便使用的 url builder 模組,在 web automation test 時建立 url 建議安裝…

在 command line 下輸入以下指令進行安裝:

pip install purl

5. selenium 必要元件(RC & WebDriver )

(1) 使用 RC (remote control) :
 RC 需要透過 selenium server 來對瀏覽器進行控制,所以必須下載 selenium server 元件

selenium-server-standalone-2.25.0.jar:

http://selenium.googlecode.com/files/selenium-server-standalone-2.25.0.jar

(2) 使用 WebDriver:

WebDriver 可以視為 API 的集合,不需要 selenium server,但卻要下載瀏覽器相關的 WebDriver。

正因為如此,使用 WebDriver 更加貼近實際瀏覽器的實際動作,可依照需求下載:

IE 32-bit WebDriver:

http://code.google.com/p/selenium/downloads/detail?name=IEDriverServer_Win32_2.25.2.zip

IE 64-bit WebDriver :

http://code.google.com/p/selenium/downloads/detail?name=IEDriverServer_x64_2.25.2.zip

chromedriver_win_23.0.1240.0.zip

http://code.google.com/p/chromedriver/downloads/detail?name=chromedriver_win_23.0.1240.0.zip

7.  測試:

請先確定電腦中有安裝 FireFox、Chrome 或是 IE

(1) RC 測試:

請先在 command line 下,輸入 java –jar selenium-server-standalone-2.25.0.jar 啟動 selenium server

# -*- coding: utf-8 -*-

from selenium import selenium

#sel = selenium('localhost', 4444, '*firefox', 'http://www.google.com.tw/')
#sel = selenium('localhost', 4444, '*iexplore', 'http://www.google.com.tw/')
sel = selenium('localhost', 4444, '*googlechrome', 'http://www.google.com.tw/')

sel.start()
sel.window_focus()
sel.window_maximize()

sel.open('/')
sel.wait_for_page_to_load(10000)

sel.type("//input[@id='lst-ib']", u'艾小克')
sel.click("//input[@name='btnK']")

assert u'瓶水相逢- 艾小克' == sel.get_text("xpath=(//h3[@class='r']/a)[1]"), 'not match'

sel.stop()

 (2) Web Driver:

請先確定 IEDriverServer.exe or chromedriver.exe 與以下 python 程式放置同一目錄即可:

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

import time
import os

#browser = webdriver.Firefox()
#browser = webdriver.Ie('IEDriverServer.exe')
browser = webdriver.Chrome('chromedriver.exe')

browser.get('http://www.google.com.tw/')

elem = browser.find_element_by_xpath("//input[@id='lst-ib']")
elem.send_keys(u'艾小克' + Keys.RETURN)

time.sleep(1)

links = browser.find_elements_by_xpath("//h3[@class='r']/a")

assert links[0].text == u'瓶水相逢- 艾小克', 'not match'

browser.close()

8. selenium IDE ( firefox plugin)
利用 selenium IDE 工具可以幫我們來錄製腳本,這裡我們先用前面出儲存的 HTML 測試腳本來執行看看,有一點很重要! Selenium-RC 無法直接執行 Test Case,它只能載入 Test Suite,因此切記指定的檔案必須要是一個 Test Suite。執行的命令如下:
java -jar selenium-server-standalone-2.45.0.jar -port 4546 -trustAllSSLCertificates -htmlSuite *firefox "http://google.com" TestSuite.html result.html
-htmlSuite 就是指定輸入的腳本格式,TestSuite.html 就是腳本 Test Suite,最後的測試結果會輸出到 result.html 檔案中。windows 及 linux 均可執行,目前由於在測試階段,故先採用 windows 架設好環境來執行。執行後會看到 FireFox 被自動啟動,會開啟兩個 FireFox,一個負責控制腳本執行,而另一個負責執行測試腳本。

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json