In the previous recipes, we saw parameterization with Java and .NET. Ruby has also been used widely to create Selenium WebDriver tests.
Again, Ruby does not have its own way to parameterize the script. However, we can use the Roo (http://roo.rubyforge.org/) gem in Ruby to read spreadsheets. Roo supports multiple formats, as follows:
.xls
) file.ods
) file.xls
) stored in a Confluence wiki page with Confluence Office ConnectorRoo is a great alternative to the Ruby Excel COM WIN32 API, as it does not need Excel or OpenOffice installed on the machine. It reads both these files natively.
In this recipe, we will parameterize the Selenium WebDriver test created in Ruby bindings using an Excel spreadsheet as a test data source.
You need to install the Roo gem using the following command:
gem install roo
This command will download and install all the dependencies required for Roo on your machine.
Let's create a simple Ruby test for parameterization using the following steps. This test will read test data from the Excel spreadsheet used in the Reading test data from an Excel file using JUnit and Apache POI recipe earlier. Create a Ruby test by importing the following modules:
require 'rubygems' require 'selenium-webdriver' require 'roo'
#Create an instance of WebDriver for Firefox driver = Selenium::WebDriver.for :firefox
#Variables for Printing Test Summary test_executed = 0 test_passed = 0 test_failed = 0
Excel
class from Roo to read a spreadsheet:#Create an instance of a Excel Spreadsheet data = Excel.new("C:\Data.xls") data.default_sheet = data.sheets.first
#Iterate through the Sheet reading Rows line by line data.first_row.upto(data.last_row) do |line| if data.cell(line,1) != "Height" #Ignore the first line for Headers begin test_status = true test_executed = test_executed + 1 puts "Test " + test_executed.to_s() driver.get "http://dl.dropbox.com/u/55228056/bmicalculator.html" height = driver.find_element :name => "heightCMS" height.send_keys data.cell(line,1).to_s() weight = driver.find_element :name => "weightKg" weight.send_keys data.cell(line,2).to_s() calculateButton = driver.find_element :id =>"Calculate" calculateButton.click bmi = driver.find_element :name =>"bmi" bmi_category = driver.find_element :name =>"bmi_category" if bmi.attribute("value").to_s() == data.cell(line,3).to_s() puts "Pass, expected value for BMI <" + data.cell(line,3).to_s() + ">, actual <" + bmi.attribute("value").to_s() + ">" else puts "Fail, expected value for BMI <" + data.cell(line,3).to_s() + ">, actual <" + bmi.attribute("value").to_s() + ">" test_status=false end if bmi_category.attribute("value").to_s() == data.cell(line,4).to_s() puts "Pass, expected value for BMI Category <" + data.cell(line,4).to_s() + ">, actual <" + bmi_category. attribute("value").to_s() + ">" else puts "Fail, expected value for BMI Category <" + data.cell(line,4).to_s() + ">, actual <" + bmi_category. attribute("value").to_s() + ">" test_status=false end if test_status test_passed = test_passed + 1 else test_failed = test_failed + 1 end rescue puts "An error occurred: #{$!}" end end end
puts "--------------------------------------------" puts "Total (" + test_executed.to_s() + ") Tests Executed" puts "Total (" + test_passed.to_s() + ") Tests Passed" puts "Total (" + test_failed.to_s() + ") Tests Failed" driver.quit
When we execute this test, Roo will read the contents of the Excel spreadsheet into the data
object, using the following code:
#Create an instance of a Excel Spreadsheet data = Excel.new("C:\Data.xls") data.default_sheet = data.sheets.first
We can then iterate the data
object from the first row to the last row using the following code:
data.first_row.upto(data.last_row) do |line|
This will copy the content from a row in a variable named line
. The value from a data cell is accessed using the data.cell()
method by passing the line and position of the cell, using the following code:
height = driver.find_element :name => "heightCMS" height.send_keys data.cell(line,1).to_s()
We also added a custom reporting code that will generate a nicely formatted report at the end of the test execution, as shown in the following screenshot:
We can also read the Google Docs spreadsheets from Roo. This can be done using the Google.new()
method by passing the key for the Google Docs spreadsheet, as shown in the following command:
data = Google.new("0Al-3LZqhACsidFh1NmdnYktmTkREVEkzb3B0ZnYybHc")
If you want to use Google spreadsheets, you must either have set the environment variables to GOOGLE_MAIL
and GOOGLE_PASSWORD
or passed the Google-username and password to the Google#new
method.
Many teams maintain Excel spreadsheets on Confluence Wiki. Using Roo, you can also read a spreadsheet stored on Confluence Wiki. For more information, visit the Roo home page at http://roo.rubyforge.org/.
3.143.255.36