本文主要演示如何在Ubuntu上搭建Watir-Webdriver與Cucumber環境,用于自動化測試。
1. Ubuntu環境
A. 安裝
因為我的工作機是Windows,所以采用虛擬機的方式使用Ubuntu。
1. 首先安裝虛擬機軟件,可以使用VMware workstation、VirtualBox等虛擬機軟件。
2. 在虛擬機中安裝Ubuntu,我使用了Ubuntu 13.10 32位.
注意:Ubuntu 32位與64位對于我們的環境搭建沒有區別,因為我的虛擬機內存為1G,所以使用了Ubuntu 32位;如果你的虛擬機內存大于2G,可以使用Ubuntu 64位,此時需要注意在電腦的BIOS中開啟硬件虛擬化選項。
B. 配置
安裝完成后,需要設置系統的Terminal選型。
打開一個GNOME Terminal,然后去到 編輯 --》 配置文件首選項 --》 標題和命令,勾選“已登錄Shell方式運行命令"。具體可以參見:
https://rvm.io/integration/gnome-terminal
2. Chrome安裝
A. 安裝Chrome
Ubuntu上的Chrome需要從Chrome官網下載安裝文件,然后雙擊運行安裝即可。
B. 安裝Chrome Webdriver
為了Webdriver正常使用Chrome,需要安裝Chrome Webdriver。
可以從 http://chromedriver.storage.googleapis.com/index.html 下載對應版本的chrome driver。
下載完成后,解壓文件,并將文件移動到指定位置,并賦予讀取執行權限。
例如我的chromedriver放在Downloads目錄下,將chromedriver文件放到/usr/bin目錄, 并賦予讀和執行的權限。
sudo
cp
Downloads/chromedriver /usr/
bin
sudo
chmod
+x /usr/bin/chromedriver
3. Ruby安裝
A. RVM安裝
在Linux上推薦使用rvm進行ruby的版本管理和安裝。
(1)在Linux Shell下執行以下命令安裝rvm
\curl -L https:
//
get.rvm.io | bash -s stable --auto-dotfiles
(2)驗證安裝
如果安裝和配置成功,每次打開一個新的Shell會話,rvm都會被加載。可以打開一個新的Terminal,執行以下命令進行測試。
type rvm |
head
-n
1
該命令的輸出應該是
rvm is a
function
如果你的系統是中文,上述命令的輸出也是中文的.
(3) 檢查依賴項
打開一個Terminal,執行命令檢查依賴項.
rvm requirements
(4) 修改bash_profile文件
在安裝rvm的時候,安裝完成后會有以下提示:
Upgrade Notes:
* WARNING: You
'
re using ~/.profile, make sure you load it,
add the following line to ~/.bash_profile
if
it exists
otherwise add it to
~/
.bash_login:
source
~/
.profile
* No new notes to display.
所以需要打開 ~/.bash_profile文件,在文件的最后添加如下行:
if
[ -f
"
$HOME/.profile
"
] ;
then
source
"
$HOME/.profile
"
fi
B. Ruby安裝
打開Terminal,執行以下命令安裝ruby。
rvm
install
1.9
.
3
rvm gemset create autotest
rvm use
1.9
.
3
@autotest --default
解釋:
(1)watir-webdriver網站描述最合適的ruby版本是1.9.3,所以我們安裝的是1.9.3.
(2)創建一個單獨的gemset是為了保證環境的互不干擾,可以將自動化測試所需要的gem都安裝到autotest這個gemset里面。
(3)設置默認的ruby使用環境。
4. Watir-Webdriver安裝
watir-webdriver來負責跟瀏覽器進行交互。
打開一個Terminal,執行以下命令安裝:
gem
install
watir-webdriver --no-ri --no-rdoc
以下是我的屏幕輸出,僅供參考:
andy@ubuntu:~$ gem
install
watir-webdriver --no-ri --no-rdoc
Fetching: rubyzip-
0.9
.
9
.gem (
100
%
)
Fetching: ffi
-
1.8
.
1
.gem (
100
%
)
Building native extensions. This could take a
while
...
Fetching: childprocess
-
0.3
.
9
.gem (
100
%
)
Fetching: websocket
-
1.0
.
7
.gem (
100
%
)
Fetching: selenium
-webdriver-
2.33
.
0
.gem (
100
%
)
Fetching: watir
-webdriver-
0.6
.
4
.gem (
100
%
)
Successfully installed rubyzip
-
0.9
.
9
Successfully installed ffi
-
1.8
.
1
Successfully installed childprocess
-
0.3
.
9
Successfully installed websocket
-
1.0
.
7
Successfully installed selenium
-webdriver-
2.33
.
0
Successfully installed watir
-webdriver-
0.6
.
4
6
gems installed
5. Cucumber安裝
Cucumber是行為驅動開發的一種工具,可以很好的與多種語言集成.自動化測試使用cucumber中的feature文件描述軟件行為, 使用watir-webdriver執行瀏覽器操作.
這里同樣使用gem安裝, 打開一個Terminal,執行以下命令安裝:
gem
install
cucumber --no-ri --no-rdoc
以下是我的屏幕輸出,僅供參考:
andy@ubuntu:~$ gem
install
cucumber --no-ri --no-rdoc
Fetching: builder-
3.2
.
2
.gem (
100
%
)
Fetching:
diff
-lcs-
1.2
.
4
.gem (
100
%
)
Fetching: multi_json
-
1.7
.
6
.gem (
100
%
)
Fetching: gherkin
-
2.12
.
0
.gem (
100
%
)
Building native extensions. This could take a
while
...
Fetching: cucumber
-
1.3
.
2
.gem (
100
%
)
Successfully installed builder
-
3.2
.
2
Successfully installed
diff
-lcs-
1.2
.
4
Successfully installed multi_json
-
1.7
.
6
Successfully installed gherkin
-
2.12
.
0
Successfully installed cucumber
-
1.3
.
2
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

