Introduction to Selenium with Ruby

Selenium with Ruby

Selenium is a popular open source functional test automation tool to test web applications. It supports multiple browsers, operating systems. It allows scripting using various programming languages like Python, Java, C#, Ruby, Perl, JavaScript etc. Java, and Python implementation of Selenium is quite popular. Ruby with Rails development of web applications is also quite popular and holds a significant market share. So it is only advisable to build your automation suite using ruby as a language.

 

Ruby

The tagline of the ruby programming language says “It’s a Programmer’s best friend”. Ruby is also known as a tester’s language. The creator of Ruby Yukihiro Matz Matsumoto blended parts of his favorite languages (Perl, Smalltalk, Ada, Lisp and Eiffel) to form this new language. It is a completely free language to use, modify and distribute. Ruby is dynamic, interpreted, objective, general purpose programming language. Everything is an object in ruby.

One can download and setup ruby on their system from this link - https://www.ruby-lang.org/en/downloads/

Implementing Selenium with Ruby

Selenium,  is a gem in Ruby. You can install it using the following command on your command prompt –

C:/> gem install selenium-webdriver

https://rubygems.org/gems/selenium-webdriver/versions/2.53.4

We will have a look at the following script, and see how can we automate using selenium with ruby.

a.      Open the application with url – http://www.5elementslearning.com

b.      Click on the my account link

c.       Type in the username abc@demo.com

d.      Type in the password demo@123

e.      Click on the Log off link

f.        Click on the Continue link.

The above script will be created in using Ruby as follows – 

1.      require "selenium-webdriver"

2.      require "test/unit"

3.      require 'rubygems'

4.       

5.       

6.      class LoginClass < Test::Unit::TestCase

7.       

8.      def setup

9.      Selenium::WebDriver::Chrome.driver_path = File.join(File.absolute_path('', File.dirname("D://Driverss")),"Drivers","chromedriver.exe")

10. @driver = Selenium::WebDriver.for :chrome

11. @driver.get('http://www.5elementslearning.com/demosite”)

12. @driver.manage.window.maximize 

13. end

14.  

15.  

16. def teardown

17. @driver.quit

18. end

19.  

20.  

21. def test_login

  1. @driver.navigate.to "http://5elementslearning.com/demosite"
  2.  assert @driver.title.include?("5ElementsLearning")
  3.  @driver.find_element(:link_text => "My Account").click
  4.  @driver.find_element(:name => "email_address").send_keys("abc@demo.com");
  5.   @driver.find_element(:name => "password").send_keys("demo@123")
  6.    @driver.find_element(:id => "tdb5").click
  7.  @driver.find_element(:link_text => "Log Off").click
  8.  @driver.find_element(:link_text => "Continue").click
  9. end

31. end

The concepts of using Selenium with Ruby remains largely the same, as you may have learnt with any other programming language. The changes will be, because of the change in the language used to implement Selenium. If you have any query, please feel free to reach out.

Hope the article helped.