python - 撰寫 selenium 自動化測試流程 (一)

  • 52956
  • 0

python - 撰寫 selenium 自動化測試流程 (一)

最近因為多項產品專案進行,都需要透過 python 撰寫 selenium 自動化測試進行 Regression Test、Acceptance  Test 及 Functional Test。

艾小克整理一下 python 撰寫 selenium 自動化測試流程的相關環境設定;

 

1. 安裝 python (本範例以 python 2.6.6 進行):

請參考「在 Windows 7 環境安裝 Python 2.6.6

 

2.  安裝 pip:

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

 

pip-1.2.1.tar.gz 下載連結:

http://pypi.python.org/packages/source/p/pip/pip-1.2.1.tar.gz

 

安裝方式:

(1) 解壓縮 pip-1.2.1.tar.gz

(2) 在 command line 下,進入 pip-1.2.1 目錄

(3) 執行指令進行安裝:python setup.py install

 

3. 安裝 selenium:

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

C:\Python26\Scripts\pip.exe install -U selenium

Image

 

4. 安裝 purl (optional):

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

 

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

c:\Python26\Scripts\pip.exe install purl

 

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

(1) 使用 RC (remote control) :

image

 

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 更加貼近實際瀏覽器的實際動作,可依照需求下載:

image

 

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

以下範例:進入 google 首頁, 輸入「艾小克」關鍵字,核對搜尋結果第一筆連接名稱是否為「瓶水相逢- 艾小克」

# -*- 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 程式放置同一目錄即可:

以下範例:進入 google 首頁, 輸入「艾小克」關鍵字,核對搜尋結果第一筆連接名稱是否為「瓶水相逢- 艾小克」

# -*- 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 套件安裝 selenium IDE…

Selenium IDE d可以將你測試的動作錄製起來,並且轉換成相關的程式碼:C#、Java、Python2、Ruby ,甚至分為 RC 及 WebDriver 不同程式碼

image