Selenium Testing Basics with Ruby

So recently I was tasked with creating a test suite for testing new instance deployments of our company’s product. We were founded in 1998 yet our product has never had a testing suite… this is beyond me but lets move forward and ensure everyone can easily get one setup for any website, product etc.

If you don’t know ruby don’t worry the language is very readable and selenium testing in general is pretty easy as long as you know basic css/html and can read some documentation.

Lets start with some basic examples:

Does google search engine work?

Sample code:

#!/usr/bin/ruby
require 'selenium-webdriver'
require 'minitest/test'
require 'minitest/autorun'
class GoogleTest < Minitest::Test
    def setup # Method called via autorun
    @url     = 'http://google.com' # The URL we are using available via @url
        @driver = Selenium::WebDriver.for :firefox # WebDriver API for Firefox
    end
     def teardown # Method called on end
    @driver.quit if @driver
     end
     def test_can_search # The actual test function
     # All functions must begin with test_ to autorun
    @driver.navigate.to @url # Navigate in FireFox to @url (google)
         # id="lst-ib" this is the CSS ID for the input of the search
    element  = @driver.find_element(:id, 'lst-ib')
         element.send_keys('abracadabra') # What are we searching for
    sleep(2) # Give some time for ajaxy stuff
            found_results = @driver.find_element(:id, 'resultStats') # Find resultStats id element
    refute_nil(found_results) # Test if found_results is nil|undefined|null|etc
     end
 end

Here I hopefully commented enough to where even if the code isn’t readable you know exactly what’s going on. This base can be used to do any of your basic testing.

Output from above example

[email protected]:~/Projects/Deployment_Selenium$ ruby google.test.rb
Run options: --seed 61686
# Running:
.
Finished in 7.099786s, 0.1408 runs/s, 0.1408 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

We see that there was 1 run and 1 assertion(passed test) so we now know that:

  1. Google is up
  2. Search is working as expected

Again, this is just a very basic example of testing using selenium and in general you would have a more robust test suite testing criteria like:

  1. Are there actual results
  2. Does pagination work as expected
  3. Etc
  4. Etc

I hope to explore some more extensive QA testing in the future and look forward to sharing.

Write a Comment

Your email address will not be published. Required fields are marked *