Ruby & Selenium-WebDriver Tutorial Part 2a: Interacting with the driver

December 1, 2013  |  Automation, QA, Ruby, Selenium

In our last post, we introduced how to write a simple test using Selenium-WebDriver and Ruby. In this post, we’re going to explore the some of the methods that we can use to interact with a browser with Selenium-WebDriver.

The first thing we have to understand is that there are two main objects when using Selenium. First, there is the driver object. In our last example, we created the example by doing

require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :firefox

Specifically, we are creating a driver variable (a variable is a container) and putting an instance of the driver for Firefox in it for us to use. You can create other drivers by passing in other browsers such as :chrome, :ie, and others. You can find a full list here: http://selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/Driver.html#for-class_method . More importantly is the remote driver which allows us to execute on a grid but we’ll get to that in a future post.

Now this driver object has many methods that we can call upon it to interact with the browser. For example, we used driver.navigate.to "http://google.com" previously to navigate to Google.com. We can use the same bit of code navigate to any page like so driver.navigate.to "http://www.seleniumtutorials.com".

Other useful methods include:

  • execute_script: This allows us to run JavaScript on the current page. For example, doing driver.execute_script(“alert(‘Hello from SeleniumTutorials.com!’)”) will cause an alert window to pop up.
  • find_element: This is probably the most important method along side with find_elements (with an s). This allows us to locate an element on the page and return it as a Ruby object. This will allow us to interact with it deeper. We will be going into elements in the next post so stay tuned. For example, in our last post, we used element = driver.find_element(:name, 'q')
  • currrent_url: This will give us the current_url the driver is on. This is useful for assertions in your test to verify that you’re on the correct page. To do this, we do driver.current_url which will return us a string back with the current url.
  • title: This will give us the title of the page. This is also useful for asserts and is used in a similar way as current_url.
  • switch_to.frame: This is useful for pages that have iframes. This will allow the driver to switch to the specific iframe that is given. To use this, you would call it as driver.switch_to.frame “frame_id”.

In your Selenium-WebDriver usage, these 5 methods are the main driver methods that you will use. In our next installment, we’ll take a look at element level methods that allow us to interact with elements on a page rather then the driver such as the send_keys method which allowed us to type “Selenium Tutorials”.

To find further methods for the driver, you can always find them in the documentation at http://selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/Driver.html