Example Test Case

转载 2012年03月21日 10:10:49

Google Test Search

This document walks through a very simple test case: Google Search. To begin, open it in a text editor. If you do not have that file, download it from here. To open it with SciTE, simply right-click the file in Windows Explorer, and select Edit from the popup menu.

The format of this example will be to show the Ruby test case scripting code in a box as you would see it in a text editor, with an explanation after it.

Getting Started

Open Internet Explorer, and try out the test case manually on your own:

  1. go to the Google home page
  2. enter pickaxe in the search text field
  3. click the Google Search button

Expected Result

A Google page with results should be shown. Programming Ruby should be high on the list.

Once you have tried the test case out manually, it's time to automate the test case using Watir. Return to the Google home page and view the page source: right mouse click > View Source. Now you can follow along and see how to automate this test with Watir based on the HTML tags in the Google search web application.

Section 1: Comments

Test cases should be commented, just as program code should be commented. In Ruby, any text on a single line that follows a # is a comment, and is ignored by the Ruby interpreter at run time.

What you see in the text editor:

# Demo test for the Watir controller.
# Simple Google test written by Jonathan Kohl 10/10/04.
# Purpose: to demonstrate the following Watir functionality:
#   * entering text into a text field,
#   * clicking a button,
#   * checking to see if a page contains text.
# Test will search Google for the "pickaxe" Ruby book.

Styles differ with comments, but they can really help with test case maintenance. In this case, the author has provided their name, a date and a purpose of the test case. This is here to improve readability, and to help others who may be using the test to understand what it does.

Section 2: Includes

To use Watir, or any other library in our test case, requires us to tell the program where to find the library.

What you see in the text editor:

# the Watir controller
require "watir"

When we run our test script, the Watir library is loaded so that our test cases can use it.

Section 3: Declare Variables

If we are going to use something in our script more than once, or something that could change, we can declare it as a variable and reuse it throughout the script. Some objects we can use for testing tend to change, such as URLs for applications we are testing. In this script, we assign the test URL as a variable. If it changes, we only have to change it in one place.

What you see in the text editor:

# set a variable
test_site = "http://www.google.com"

The test case author has chosen to assign the URL to a variable called test_site. It may not be much of an issue in this test case, but using variables for test URLs is often a good practice.

Section 4: Open an Internet Explorer Browser

To begin driving Internet Explorer, we need to tell Watir to open an instance for testing.

What you see in the text editor:

# open the IE browser
ie = Watir::IE.new

To explain what this is doing, we can start from the right of the = operator. We send a message new to the IE (Internet Explorer) class that is inside Watir module, and assign it to a variable called ie. The ie variable is a local variable (like test_site). This means it can be accessed from our script, but not from other Ruby functions or methods.

Section 5: Interacting With Google

Now we are ready to start automating the steps we ran manually using Watir.

Beginning the test case

What you see in the text editor:

# print some comments
puts "Beginning of test: Google search."

puts is a reserved word in the Ruby language that tells the Ruby Interpreter to print whatever comes after it contained in quotes to the screen. We could just as easily write this information to a file. These puts statements are in this test case to make it more self-explanatory. You can print to the screen as a "friendly message" (e.g. telling the user something is loading while they are waiting for results, or printing a result as "The answer is 5" instead of just "5") or as "flagging" and that is to print to the screen for debugging purposes. Printing what we are doing when we automate the test case is useful for debugging when developing test cases, and for quickly repeating failures for bug reports when the test case doesn't pass.

Step 1: Go to the Google site

This test case follows a pattern of printing out what we intend Watir to do on a web application, followed by the Watir scripting code to carry out that action. This is a style of test case development that is useful for tracking down test case failures quickly.

What you see in the text editor:

puts " Step 1: go to the test site: " + test_site
ie.goto test_site

The first line uses a puts statement to print out the test case step we are attempting to the screen. The second line uses the Watir method goto to direct the test case to the test site: http://www.google.com (stored in the variable test_site). When we print out the variable, we concatenate it to the string (the part in quotes) by using the + sign.

Step 2: Enter Search Term pickaxe in the search field

We need to enter the term to search in the text field on the Google home page.

What you see in the web browser:

What you see in the text editor:

puts " Step 2: enter 'pickaxe' in the search text field."
ie.text_field(:name, "q").set "pickaxe" # "q" is the name of the search field

The first line prints the step we are on to the screen. The second line enters the text pickaxe in the text field named q. The comment telling the user that q is the name of the text field is optional.

This is the tag in the HTML source with the name attribute we used:

<input maxlength=2048 name=q size=55 title="Google Search" value="">

The text field has a name attribute q, so we use that to tell Watir what object to interact with.

Step 3: Click the Google Search button

We need to click the search button to activate the Google Search functionality.

What you see in the web browser:

What you see in the text editor:

puts " Step 3: click the 'Google Search' button."
ie.button(:name, "btnG").click # "btnG" is the name of the Search button

The first line prints the step we are on to the screen. The second line clicks the Google Search button.

This is the tag in the HTML source with the name attribute we used:

<input name=btnG type=submit value="Google Search">

Section 6: Evaluating the Results

The Expected Result

This test case prints out what the Expected Result should be prior to the test case using Watir to evaluate the results.

What you see in the text editor:

puts " Expected Result:"
puts "  A Google page with results should be shown. 'Programming Ruby' should be high on the list."

These two statements simply print the expected result of the test to the screen.

Verify Results

Using Watir and a little Ruby, we can evaluate the results to verify whether the test case passed or failed.

puts " Actual Result:"
if ie.text.include? "Programming Ruby"
  puts "  Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
  puts "  Test Failed! Could not find: 'Programming Ruby'."

There is a lot more in this section, but we can break it down.

  • The first line prints out the Actual Result heading to the screen.
  • The second line gets the text of the page and then calls the include? method to determine whether Programming Ruby appears on the first page.
  • Using an if statement, we evaluate whether the include? method was true or false.
    • If include? returns true (or actually anything but false), the text Programming Ruby appears. The test case passes and we print the Test Passed. message.
    • Else, if include? returns false, the text Programming Ruby does not appear. The test case fails and we print the Test Failed! message.
  • We close the conditional if section with an end statement.

junit test case和junit test suite有什么区别

  • u014401141
  • u014401141
  • 2016年06月29日 17:25
  • 754


junit是java中书写unit test的framework,目前一些流行的unit test工具大都都是在junit上扩展而来的。Eclipse中配置junit在要使用JUNIT的project...
  • kevinwu629
  • kevinwu629
  • 2011年03月25日 00:05
  • 7111


测试一菜鸟 Test_Alan.W! 人生需要不断的学习!学习生涯却要记录,记录为学习,也想在时间长河留下一丝足迹! 黑盒测试 testcase设计方法:                   ...
  • Test_Alan
  • Test_Alan
  • 2016年12月12日 21:16
  • 516

《Test-Driven development By Example》阅读(一)

SQ3R阅读法:   一、Survey(阅读前的调查)       1、书名:《Test-Driven development By Example》,中文译名《测试驱动开发实例》。    ...
  • makunyuan123
  • makunyuan123
  • 2013年07月05日 10:51
  • 887


  • jianghuihong2012
  • jianghuihong2012
  • 2014年09月11日 22:43
  • 2014

test case 开源管理工具testlink安装介绍

1.testlink简介 testlink 1.7rc3 main features1.Web based User interface (Mozilla, Firefox, IE compatibl...
  • lzz313
  • lzz313
  • 2007年07月18日 15:04
  • 3884

OCP 1Z0 053 24

24.Which of the following information will be gathered by the SQL Test Case Builder for the problems...
  • jgmydsai
  • jgmydsai
  • 2014年06月30日 18:08
  • 981


24.Which of the following information will be gathered by the SQL Test Case Builder for the problems...
  • rlhua
  • rlhua
  • 2013年11月13日 09:29
  • 10637


#coding=utf8 ''' 测试用例是单元测试最基本的构建块。 测试用例在每个场景中都被建立,用于检查场景的正确性。 在unittest中,测试用例通过unittest的TestCase类来描...
  • henni_719
  • henni_719
  • 2017年02月27日 09:10
  • 1066

hdu 1075 What Are You Talking About (字典树·文字翻译)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Description Ignatius is so lucky that he...
  • theArcticOcean
  • theArcticOcean
  • 2015年08月12日 08:52
  • 728
您举报文章:Example Test Case