估计你早已厌烦了成百上千遍的网页点击测试,至少之前的我是这样的,那么,让自己的web测试自动化吧,本文使用Cucumber + Watir来完成自动化测试,从最简单例子入手,一步一步重构成一个完整的自动化测试框架。

(一)Cucumber和Watir的关系

Cucumber和Watir可以没有任何关系,Cucumber只是一个BDD框架,而Watir只是一个Web Driver而已,两者的共同点是均属于Ruby世界。

(二)单独使用Cucumber

关于Cucumber的执行过程请参考笔者的另一篇文章:行为驱动开发: Cucumber的目录结构和执行过程 。

由于Cucumber和Watir没有必然联系,因此两者均可单独使用,下面就让我们用Cucumber来写一个非常简单的单元测试。 定义一个需要测试的Calculator类如下:

1 class Calculator

2 def add num1, num2

3 num1+num2

4 end

5 end

用于测试Calculator类的add方法的feature文件如下:

1 Feature: Unit test for Calculator

2

3 Scenario: Add two numbers

4 Given I have a calculator created

5 When I add '3' and '5'

6 Then I should get the result of '8'

对应的step文件为:

1 require File.join(File.dirname(__FILE__), "../calculator")

2 require 'rspec'

3

4 Given /^I have a calculator created$/ do

5 @calculator = Calculator.new

6 end

7

8 When /^I add '([^"]*)' and '([^"]*)'$/ do |num1, num2|

9 @result = @calculator.add(num1.to_i, num2.to_i)

10 end

11

12 Then /^I should get the result of '([^"]*)'$/ do |expected_result|

13 @result.should == expected_result.to_i

14 end

在以上的step文件中,第1,2行分别require了自定义的Calculator类和rspec(用于assertion,参考第13行的“should”), 第5行新建了一个@calculator实例变量,第9行完成两个数相加(3+5),第13行为测试断言。运行cucumber命令,输出结果如下:

1 Feature: Unit test for Calculator

2

3 Scenario: Add two numbers # features/Calculator.feature:4

4 Given I have a calculator created # features/step_definitions/calculator_step.rb:4

5 When I add '3' and '5' # features/step_definitions/calculator_step.rb:8

6 Then I should get the result of '8' # features/step_definitions/calculator_step.rb:12

7

8 1 scenario (1 passed)

9 3 steps (3 passed)

10 0m0.002s

测试成功,没有看到任何Watir的影子。

(三)单独使用Watir

听说有非常fashionable的office lady用Watir来完成日常例行并且繁琐的网页点击工作的(当然不是测试),听说而已,但是Watir的确可以完成诸如此类的网页模拟操作,接下类我们就用Watir来完成google搜索功能,新建watir_google.rb文件并加入以下内容:

1 require 'watir-webdriver'

2 browser = Watir::Browser.new :chrome3 browser.goto "www.google.com/"

4 browser.text_field(:name => "q").set "ThoughtWorks"

5 browser.button(:name => "btnG").click

当执行到第2行时,一个浏览器窗口会自动打开,之后访问google主页(第3行),设置搜索关键词"ThoughtWorks",最后点击搜索按钮,单独运行Watir成功,没有任何Cucumber的影子。

(四)用Cucumber+Watir写自动化测试

由上文可知,Cucumber只是用接近自然语言的方式来描述业务行为,而Watir则只是对人为操作网页的模拟。当使用Cucumber+Watir实现自动化测试时,通过正则表达式匹配将Cucumber的行为描述与Watir的网页操作步骤耦合起来即可。同样以Google搜索为例,搜索关键字后,我们希望获得搜索结果,先用Cucumber完成搜索行为描述:

1 Feature:Google search

2 Scenario: search for keyword

3 Given I am on google home page

4 When I search for 'ThoughtWorks'

5 Then I should be able to view the search result of 'ThoughtWorks'

对应的Watir代码如下:

1 require "rubygems"

2 require "watir-webdriver"

3require 'rspec'

4 Given /^I am on google home page$/ do

5 @browser = Watir::Browser.new :chrome6 @browser.goto("www.google.com")

7 end

8

9 When /^I search for '([^"]*)'$/ do |search_text|

10 @browser.text_field(:name => "q").set(search_text)

11 @browser.button(:name => "btnK").click

12 end

13

14 Then /^I should be able to view the search result of '([^"]*)'$/ do |result_text|

15 @browser.text.should include(result_text)

16 end

运行cucumber,一个新的浏览器被打开,显示结果与(三)中相同。

(五)自动化测试的设计模式:Page对象

在上面的例子中,我们在所有的step中均直接对@browser对象进行操作,对于这样简单的例子并无不妥,但是对于动则几十个甚至上百个页面的网站来说便显得过于混乱,既然要面向对象,我们就希望将不同的页面也用对象来封装,于是引入Page对象,既可完成对页面的逻辑封装,又实现了分层重用。此时位于high-level的Cucumber文件无需变动,我们只需要定义一个Page对象来封装Google页面(google-page.rb):

1 require "rubygems"

2 require "watir-webdriver"

3 require "rspec"

4

5 class GooglePage

6 def initialize

7 @browser = Watir::Browser.new :chrome8 @browser.goto("www.google.com")

9 end

10

11 def search str

12 @browser.text_field(:name => "q").set(str)

13 @browser.button(:name => "btnK").click

14 end

15

16 def has_text text

17 @browser.text.should include(text)

18 end

19 end

相应的step文件需要做相应的修改:

1 require File.join(File.dirname(__FILE__), "google-page")

2

3 Given /^I am on google home page$/ do

4 @page = GooglePage.new

5 end

6

7 When /^I search for '([^"]*)'$/ do |search_text|

8 @page.search search_text

9 end

10

11 Then /^I should be able to view the search result of '([^"]*)'$/ do |result_text|

12 @page.has_text result_text

13 end

运行cucumber,一个新的浏览器被打开,显示结果与(三)中相同。

(六)加入角色用户

既然是行为驱动,既然是模拟用户实际操作,那么直接对Page对象进行操作也显得不够了,于是我们引入了角色用户User对象,对于拥有多种用户角色的网站来说特别实用。加入User对象之后,step文件中不再出现对Page对象的直接引用,而是在User对象的行为方法中进行引用,定义User对象如下(user.rb):

1 require File.join(File.dirname(__FILE__), "google-page")

2

3 class User

4 def initialize

5 @browser = Watir::Browser.new :chrome6 end

7

8 def visit_google

9 @page = GooglePage.new(@browser)

10 end

11

12 def search_text text

13 @page.search text

14 end

15

16 def assert_text_exist text

17 @page.has_text text

18 end

feature文件保持不变,在step文件用User代替Page:

1 require File.join(File.dirname(__FILE__), "user")

2

3 Given /^I am on google home page$/ do

4 @user = User.new

5 @user.visit_google

6 end

7

8 When /^I search for '([^"]*)'$/ do |search_text|

9 @user.search_text search_text

10 end

11

12 Then /^I should be able to view the search result of '([^"]*)'$/ do |result_text|

13 @user.assert_text_exist result_text

14 end

运行cucumber,一个新的浏览器被打开,显示结果与(三)中相同。

对于拥有多个用户角色的网站,比如又customer,administrator等,可分别对这些角色定义相应的对象,再在step文件中应用这些角色对象即可。

(七)用ruby的Module来封装不同的行为功能

对于单个用户来说,比如网上购物网站的customer,既要购物操作,又要能修改自己的profile,此时为了对这些不同的逻辑功能进行组织,可引入ruby中的Module来进行封装,即将costomer的不同行为功能模块封装在不同的module中,然后在customer对象中include这些Module。为简单起见,依然用Google搜索来进行演示,此时可将搜索功能加入到Module中,定义搜索module(search-behavior.rb)如下:

1 module SearchBehavior

2

3 def visit_google

4 @page = GooglePage.new(@browser)

5 end

6

7 def search_text text

8 @page.search text

9 end

10

11 def assert_text_exist text

12 @page.has_text text

13 end

14

15 end

在User对象中include该Module:

1 require File.join(File.dirname(__FILE__), "search-behavior")

2 class User

3 include SearchBehavior

4 def initialize

5 @browser = Watir::Browser.new :chrome6 end

对step文件和feature文件均不用修改,运行cucumber,一个新的浏览器被打开,显示结果与(三)中相同。

(八)总结

我们可以在Cucumber对应的step文件中直接访问Watir的API,这样的确也能达到测试目的,但这样的缺点在于缺少设计,于是我们引入Page对象来封装不同的页面,引入用户角色管理不同的用户行为,再引入Module来组织不同的功能模块,最后重构成了一个简单实用的自动化测试框架。

Logo

一站式 AI 云服务平台

更多推荐