本文引用自repeat - 試玩 Selenium 2.0 b3
好不容易有時間再來研究一下 Selenium ,版號竟然已經飛奔到 2.8 (而且還已經預告即將推出 2.9 是哪招…)
原本想要試試用 Python 開,但是玩了半天不知道怎麼樣在 Windows 底下裝 pip (抓頭),後來還是用 Java 開,反正都不熟。(炸)
要用 Java 來開 Selenium 2.8 的話,前置作業比起 2.0 b3 多很多步驟,大概記一下:
- 先準備好 Java 和 Eclipse 。之前剛好在接 logcat 時就有裝了,直接拿來用。
- 下載 Maven 3.0.3 解壓縮,我放到
C:\Program Files\Maven\apache-maven-3.0.3,依照教學設定完以後確認mvn --version會動。 - 安裝 m2eclipse ,不過我不曉得什麼時候裝好了, Eclipse 告訴我不用再裝了,就又直接拿來用…
- 建一個資料夾(我是叫
Sel2-test並且放在桌面),裡面放一個pom.xml檔案。pom.xml的內容我用官方教學裡的內容,只把版號改成2.8。 - 開啟 command line ,用裝好的 Maven 到剛建的資料夾裡下載 Selenium:
mvn clean install - 打開 Eclipse, 在 File → Import 裡找到 Maven → Existing Maven Projects , Root directory 輸入剛剛那個資料夾, Eclipse 就會抓到並匯入專案了。
- 在專案裡新增一個叫 src 的 source folder, 在 src 裡再新增一個叫
Selenium2Example.java的 class ,一樣複製貼上官方教學裡提供的測試碼。 - 看到 firefox 會動的話就可以收工了。
HtmlUnitDriver 照著改沒什麼問題。 InternetExplorerDriver 和 ChromeDriver 比較麻煩一點: IE 會遇到安全性問題, Chrome 則是會哀號說找不到 ChromeDriver 。解法是參考 Getting Started with Selenium 2 and WebDriver 裡提到的方法, IE 我把「啟用受保護模式」全部關掉, ChromeDriver 我則是下載以後放到 Sel2-test\bin 裡,然後在測試碼前面加上 System.setProperty("webdriver.chrome.driver", "C:/blah/Sel2-test/bin/chromedriver.exe"); 。 Windows 的路徑可以用 / 或 \\ ,但是不能直接用 \ , Eclipse 會哀號說你沒有 escape …
以目前大約一個星期更新一次程式的速度看來,文件應該會跟不上程式的更新速度 :(

日常 (6)

===安裝python===
1. 到 http://www.python.org/download/
Python 2.7.2 Windows Installer (windows 32bit os)
http://www.python.org/ftp/python/2.7.2/python-2.7.2.msi
2. Install python-2.7.2.msi, 使用預設值安裝
3. 設定Windows環境變數以便透過 Windows 命令列來使用Python語言。
在 Windows 2000, XP, 2003 這些作業系統裡,點選 "控制台" -> "系統" -> "進階" -> "環境變數"。
在"系統變數(S)"欄中,選取 'PATH' 變數名稱後,再點選 '編輯(I)' 按鈕。
請先確定你安裝 Python 語言程式的路徑是 C:\Python27,接著在彈出的視窗中將 ;C:\Python27 (注意要以分號隔開)這字串添在原本的字串之後。
重開機後才會生效。
或者也可以不管上面那些,直接切換目錄到 C:\Python27 執行 python.exe 也可以。
4. Execute python.exe, it will come up like this:
C:\Python27>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
5.
>>> print 'hello, world'
>>> print '你好世界' 中文碼也通
6. ctrl-z + enter to exit
參考http://code.google.com/p/zhpy/wiki/DownloadInstall
===安裝PIP===
1. go to http://pypi.python.org/pypi/setuptools#files
download setuptools-0.6c11.win32-py2.7.exe
and install it.
2. go to http://pypi.python.org/pypi/pip#downloads
download pip-1.0.2.tar.gz
then uncompress it.
3. go to directiry pip-1.0.2
命令列安裝:c:\Python27\python.exe setup.py install
4. 再加一次環境變數 ;C:\Python27\Scripts,此後執行pip可以不用指定pip的路徑,重開機後才會生效
===Selenium Webdriver===
1. pip install selenium
我安裝完只有IE driver 32/64bit
而且firefox好像不用webdriver也能直接呼叫
===執行python===
http://seleniumhq.org/docs/03_webdriver.html#getting-started-with-selenium-webdriver
test case存為xxx.py
直接執行xxx.py即可成功運作
===設定Chromedriver===
for winxp
1. go to http://code.google.com/p/chromium/downloads/list
download chromedriver_win_16.0.902.0.zip--ChromeDriver server for windows
2. unzip and copy the path of chromedriver.exe to 環境變數
或者也可以把chromedriver.exe放到xxx.py目錄裡
3. 在xxx.py內加入driver = webdriver.Chrome()