# Scripting Web Tests

with Watir and Ruby

Workbook / Labs / 3. Writing Scripts

## Lab 3: Writing and Running Watir Scripts

Now that you’ve used the Watir Console to drive Depot you’ll create a script. Actually, you’ll create several. Create each script using Scite, an editor installed with Ruby. First, create a file with a ”.rb” ending and then select “Edit” on the popup menu in Windows Explorer. Scite is a simple with built in support for Ruby.

When you used the watir-console, the commands you typed were automatically stored in a file called console.log. You may wish to copy those commands from that file when creating your script. You also may need to refer to the Watir Cheat Sheet.

The watir-console knew that you were using Watir, so it automatically loaded the watir library. Your script is now a generic Ruby script. So the first thing you will have to do is load the Watir library. Thus:

    require 'watir'

There are two ways to run your scripts:

1. The simplest method is to type F5 while editing the script in Scite. This will save the file and then execute it. The output will be displayed in a side window.
2. A second method is to run a script from the system command line. Type “ruby script-name.rb” at the cmd prompt. (Actually, just “script-name.rb” usually works too.) Don’t forget to save the file first!

Create and execute scripts for each of the following scenarios.

1. Add Book to Cart. Add a book to the cart.
2. Purchase Books. Add two books to the cart and then check out.

Don’t worry about validating the application behavior at this point. We’ll add validation in the next lab.

### Completion

At the completion of this exercise, you should know how to:

- Create executable Watir scripts from console logs.
- Execute scripts from within Scite or from the command line.

You can compare your scripts to a sample solutions for this lab