DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Cross-Browser Automation Testing Using Watir

Cross-Browser Automation Testing Using Watir

We take a look at how developers can perform cross-browser automation testing locally using Watir and Ruby. Read on to get started!

Muthuraja `Kumar user avatar by
Muthuraja `Kumar
·
Feb. 06, 19 · Tutorial
Like (2)
Save
Tweet
Share
11.47K Views

Join the DZone community and get the full member experience.

Join For Free

What Is Cross-Browser Parallel Test Automation?

Cross-browser parallel testing is performed to run a single test across multiple browser combinations, simultaneously. This is a very practical and powerful scenario for automation testing. Cross-browser parallel test automation allows you to scale back execution time while not compromising with coverage of your check and leads to faster test results.

What Is Watir?

Watir is an open source Ruby library which helps to achieve cross-browser automation testing. Watir supports Ruby, which is an object-oriented language and typically it’s simpler and faster than other languages. The good thing about Watir is that it supports any web application irrespective of the technology used to develop that application.

Why Watir?

  • It’s a free open source tool belonging to the Ruby family.
  • It supports headless browser execution.
  • It supports the page object design pattern.
  • It supports Cucumber integration.
  • Tests' simplicity and flexibility can be maintained.
  • It supports your web app no matter what technology you used to develop your app.
  • It supports multiple browsers on different platforms.
  • It is lightweight and easy to use.
  • It supports execution in the cloud through cloud-based, cross-browser automation testing tools.

In this article, we will take a look on how to set up test automation environments for Watir with RubyMine IDE and then go ahead with the sample script. I would also like to show how to run the script and see the failure in RubyMine. Let’s dive right in.

Getting Started With Cross Browser Automation Testing Using Watir

Here are the prerequisite required for performing cross-browser automation testing using Watir on Windows:

  • RubyInstaller
  • Watir
  • RubyMine IDE

The RubyInstaller is must because Watir supports Ruby code, so this installation is very important before we proceed with automation.

Setting Up the Automation Environment

This section has detailed steps explaining how to setup the automation environment for performing cross-browser automation testing using Watir through Windows.

Step1: Installing Ruby

Navigate to the official RubyInstaller page here.

Click on the latest version with DEVKIT and download it. Select your OS architecture type, for example I selected a (X64)64-bitt operating system.

After download, right click on installer and run as administrator in Windows machine.

The setup window popups to accept license agreement, you need to accept this and move next.


I would recommend selecting all checkboxes and clicking the install button to proceed further.

The below progress bar indicates the install is in-progress, which takes a few minutes time to install Ruby.

After installation is complete, the following window will popup to install the updated component. This is optional; you can either leave it by closing the window or hit the “Enter” button to update all the components.

Open the command prompt and enter the following command:

  • ruby -version

The Ruby version that was installed is displayed in the command prompt.

To verify once again that the installation was successful and Ruby works fine, enter the following command:

  • irb

irb stands for Interactive Ruby Shell which is a REPL (read-eval-print-loop) for programming in the object-oriented scripting language Ruby.

I will write some simple code that will put in “lambdatest.com” and print it.


Step 2: Installation of Watir

Installation of Watir is very simple. As I mentioned in the above section, to verify Watir you must first install Ruby. To install, put in the below gem command:

  • gem install water

Gems are the package manager for the Ruby programming which provide a standard format of disturbing Ruby libraries.

lambdatest
Step 3: Installation of RubyMine IDE

RubyMine is an IDE (Integrated Development Environment) that helps you to write, debug, and test code for your application. Ruby also support multiple OSs like Windows, MacOS, and Linux.

Note: RubyMine comes up with a free 30-day trial license.

To download the RubyMine, navigate here and click on the following button:

lambdatest

Right-click on the installer and run it as an administrator.

Click Next and continue until the installation is complete and launch RubyMine

Keep the default with the existing plugins and click Next until you see the below window.

Click “Create New Project.”

Enter the project name and select “Ruby SDK,” which will be available if you installed RubyInstaller.

Right-click on the created project and create a new directory called “testsuites.”

Create a subdirectory called “resources” under testsuites and drop it in the “chromedriver.exe” file.

To run browser-based automation, ChromeDriver is required, which is a separate executable that WebDriver uses to control Chrome. To download ChromeDriver, visit here.

Writing Code for Sample Test

Writing code in RubyMine is very simple.

Right-click on testsuites and select “TestUnit Test Template.”

Enter the file name, “my_script.rb,” in filename and click OK.

In the RubyMine window, write the following code.

Code (Explanations Given in Comments):

require 'watir'
require 'selenium-webdriver'
require 'test/unit'
require 'test/unit/ui/console/testrunner'

class Sampletest < Test::Unit::TestCase

  "" "
    LambdaTest Watir automation sample example
    Configuration
    ----------
    Download Chrome driver from http://chromedriver.chromium.org/downloads and drop into your resource folder

    Result
    -------
    Execute Watir Automation Tests on LambdaTest website
    " ""

  def setup
    "" "
        Setup local driver
        Params
        ----------
        platform : Windows 10
        browserName : Supported platform - (chrome in your local box)

        Result
        -------
        " ""
    #Initializing chrome driver
    Selenium::WebDriver::Chrome.driver_path = "resources/chromedriver.exe"
    @browser = Watir::Browser.new
  end

  def test_verifyLambdapage()
      #Navigate to lambdatest.com website
      @browser.goto 'https://lambdatest.com'
      #Maximize the browser window
      @browser.window.maximize
      #Initializing element with Link text
      ele_starttestingbutton = @browser.link(:text => "START TESTING")
      #Perform click operation
      ele_starttestingbutton.click
      #printing page title
      puts("Actual Page title is: "+@browser.title)
      #Verifying actual and expected title of the page. This examples I intentionally fail the test by adding spell error
      assert_equal("Signup - LambdaTest App | Free Cross Browser Testing To0ol", @browser.title)
  end


  def teardown
    #Quit the browser
    @browser.quit
  end
end

Right-click on the script and select “Run” in the IDE to see test results and the output:

The above test script intentionally fails to verify results in terms of comparing actual vs. expected page titles.

The below snapshot explains whey the test failed and the difference is highlighted.

Using Local WebDriver to Perform Cross-Browser Automation Testing Using Watir

Code Explanation:

def setup
  "" "
      Setup local driver
      Params
      ----------
      platform : Windows 10
      browserName : Supported platform - (chrome in your local box)
 
      Result
      -------
      " ""
  #Initializing chrome driver
  Selenium::WebDriver::Chrome.driver_path = "resources/chromedriver.exe"
  @browser = Watir::Browser.new
end


def teardown
  #Quit the browser
  @browser.quit
end

In the TestUnit template, the IDE automatically creates def setup and teardown. These methods actually run before and after the test.

Setup: This method is a precondition to run the test. You may setup something like initialize browser or any test data setup.

Teardown: This method is used after your test runs. You may close/quit the browser or delete the data in database.

Selenium::WebDriver::Chrome.driver_path = "resources/chromedriver.exe"
    @browser = Watir::Browser.new

The above code is initializing ChromeDriver and creates a new instance and assign an object reference to the variable browser.

The next line of code is typical Watir script (Note: The method name starts with “test_” which is mandatory for Unit Test Runner to actually realize this is test method).

  • Navigate to browser.
  • Maximize the Window.
  • Click on “Start Testing” button in lambdatest.com.
  • Prints Current Page Title.
  • Finally, verify whether you get the expected vs. actual title.
def test_verifyLambdapage()
    #Navigate to lambdatest.com website
    @browser.goto 'https://lambdatest.com'
    #Maximize the browser window
    @browser.window.maximize
    #Initializing element with Link text
    ele_starttestingbutton = @browser.link(:text => "START TESTING")
    #Perform click operation
    ele_starttestingbutton.click
    #printing page title
    puts("Actual Page title is: "+@browser.title)
    #Verifying actual and expected title of the page. This examples I intentionally fail the test by adding spell error
    assert_equal("Signup - LambdaTest App | Free Cross Browser Testing To0ol", @browser.title)
end


unit test

Published at DZone with permission of Muthuraja `Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Front-End Troubleshooting Using OpenTelemetry
  • Key Elements of Site Reliability Engineering (SRE)
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: