Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Selenium Design Patterns and Best Practices

You're reading from   Selenium Design Patterns and Best Practices Build a powerful, stable, and automated test suite using Selenium WebDriver

Arrow left icon
Product type Paperback
Published in Sep 2014
Publisher
ISBN-13 9781783982707
Length 270 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Dima Kovalenko Dima Kovalenko
Author Profile Icon Dima Kovalenko
Dima Kovalenko
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Writing the First Test FREE CHAPTER 2. The Spaghetti Pattern 3. Refactoring Tests 4. Data-driven Testing 5. Stabilizing the Tests 6. Testing the Behavior 7. The Page Objects Pattern 8. Growing the Test Suite A. Getting Started with Selenium Index

Comparing Ruby to Selenese

Let's look at the commands we just learned in the IDE and Selenese and how they translate into the Ruby language. In the case of Ruby, we will only look at the key commands and how they translate from Selenese into Ruby. The goal of this exercise is to take away some of the intimidation factor of moving to a programming language for someone who may never have seen software code before.

To start, let's look back at the HTML table that is the Selenese output:

Comparing Ruby to Selenese

The first line in this table is the name of the test, which happens to be search_test.

Comparing Ruby to Selenese

The second item, shown in the preceding screenshot is the open command to the root (/) of the base domain URL. So, the browser will navigate to this exact address http://awful-valentine.com/.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

In Ruby, the open command translates into a very straightforward get method call. The code looks like this:

selenium.get("http://awful-valentine.com/")

Tip

Note that we didn't have to use base URL like we did with Selenese. WebDriver talks directly to the web browser, not through JavaScript; this eliminates XSS limitations, and you can test as many websites as you want in a single test run.

Once the browser navigates to the website we want, it needs to locate the search field and input the search term. In the search_test table, it is in the third line:

Comparing Ruby to Selenese

Since this is a complex multistep action, let's break it down into smaller chunks:

  1. Find the text field element with the help of the find_element method by passing it the HTML ID of the text field (searchInput), and then store the element in the element variable:
    element = selenium.find_element(:id, "searchInput")
  2. Once the text field is located and stored in the element variable, we will type the cheese string into it by using the send_keys method:
    element.send_keys("cheese")
  3. We have now typed the text we wanted into the search bar. We used the element variable to store the reference to the text field, and then applied some typing action on that variable.
  4. We can use method chaining to get the same result in a more condensed version; the search and type text action would look like the following code with method chaining:
    selenium.find_element(:id, "searchInput").send_keys("cheese")

    Note

    Method chaining is a common type of syntax that allows the programmer to invoke multiple method calls without using intermittent variables. Each method call in the chain returns an object that answers to the next method call in the chain. We will go deeper into object-oriented programming in the The Page Objects pattern section of Chapter 7, The Page Objects Pattern.

    The last action our test performs is clicking on the search submit button. In the Selenese table, it is the fourth row of our test:

    Comparing Ruby to Selenese
  5. Using method chaining as before, we will find the submit button and send a click command to it:
    selenium.find_element(:id, "searchsubmit").click
    

The clickAndWait command translates to a simple click method call in Ruby.

Note

Notice that with Selenium WebDriver, the wait for page to load part of the clickAndWait command is implicit. As of Selenium 2, when navigating from page to page, Selenium will automatically wait for the new page to finish loading. This, however, does not apply for any AJAX requests to finish. We will discuss AJAX waits in Chapter 5, Stabilizing the Tests.

You have been reading a chapter from
Selenium Design Patterns and Best Practices
Published in: Sep 2014
Publisher:
ISBN-13: 9781783982707
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image