本文主要演示如何在Ubuntu上搭建Watir-Webdriver与Cucumber环境,用于自动化测试。html
由于个人工做机是Windows,因此采用虚拟机的方式使用Ubuntu。web
1. 首先安装虚拟机软件,能够使用VMware workstation、VirtualBox等虚拟机软件。chrome
2. 在虚拟机中安装Ubuntu,我使用了Ubuntu 13.10 32位.json
注意:Ubuntu 32位与64位对于咱们的环境搭建没有区别,由于个人虚拟机内存为1G,因此使用了Ubuntu 32位;若是你的虚拟机内存大于2G,能够使用Ubuntu 64位,此时须要注意在电脑的BIOS中开启硬件虚拟化选项。ubuntu
安装完成后,须要设置系统的Terminal选型。api
打开一个GNOME Terminal,而后去到 编辑 --》 配置文件首选项 --》 标题和命令,勾选“已登陆Shell方式运行命令"。具体能够参见: https://rvm.io/integration/gnome-terminal
浏览器
Ubuntu上的Chrome须要从Chrome官网下载安装文件,而后双击运行安装便可。ruby
为了Webdriver正常使用Chrome,须要安装Chrome Webdriver。bash
能够从http://chromedriver.storage.googleapis.com/index.html下载对应版本的chrome driver。websocket
下载完成后,解压文件,并将文件移动到指定位置,并赋予读取执行权限。
例如个人chromedriver放在Downloads目录下,将chromedriver文件放到/usr/bin目录, 并赋予读和执行的权限。
sudo cp Downloads/chromedriver /usr/bin sudo chmod +x /usr/bin/chromedriver
在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
打开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使用环境。
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
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