<p>BDD approach is counted as one of the most desired/intended techniques in both software development and automation testing. It finalizes the behavior of an application (i.e., how it will work) on the basis of which it starts to build the application. Similarly, BDD testing helps in writing tests and, then, in writing automation code to verify them. The testing scenarios are written in plain English sentences that are directly mapped/glued with Ruby code, which will be discussed later with an example in this blog.</p> <h3 style="text-align: left;"></h3> <p><strong>What is Web Application Testing in Ruby (WATIR)?</strong></p> <p>WATIR is an open-source bundle of Ruby libraries (gems) that is used for automating web browsers. It can automate all popular browsers like Google Chrome, Mozilla Firefox, Internet Explorer, Safari, and run Headless. WATIR is a Ruby gem (or a Ruby API) which is easily scalable to various platforms like different operating systems, cloud testing, and continuous integration. It is fast and can set up your automation project in no time. Faster scripting and amazing out-of-the-box features makes WATIR the first choice of many Test Automation developers.</p> <p><strong>Pros and Cons of WATIR</strong></p> <p>Let’s understand a few Pros and Cons of the tool:</p> <ol> <p><strong>Pros:</strong></p> <li>Being an open-source tool with a vast community support, the community answers all scripting-related queries and fixes bugs (if any) in no time.</li> <li>WATIR uses Ruby to write scripts (Ruby is one of the languages which supports <a href="https://en.wikipedia.org/wiki/Duck_typing">duck-typing</a>).</li> <li>It performs faster in comparison to its counterparts available in the industry.</li> <li>WATIR has 100s of light-weight ruby gems that makes scripting easier, faster, and maintainable.</li> <li>Easy integration with cloud testing tools like Sauce Labs, BrowserStack, etc., supports automation on all latest versions of mobile and headless browsers.</li> </ol> <ol> <p><strong>Cons:</strong></p> <li>Doesn’t automate mobile-native applications.</li> <li>WATIR only supports Ruby.</li> </ol> <p><strong>Connection between Ruby, Gems, WATIR, Cucumber, and Ruby Version Manager (RVM)</strong></p> <p>Ruby is an interpreted, high-level, general-purpose programming language. It is a module/library that helps in enhancing the already-written ruby code. Programmers in the ‘Ruby world’ keep these ready for their own use and for the rest of the world. Whenever they need a functionality, they simply install that gem and start working on it. For example, “faker” gem - this gem generates real-looking test data such as usernames, email addresses, phone numbers, etc. Hence, next time when you run your tests cases with unreadable, poor test data like “Test123”, “test123@test.com” etc., download this gem to generate some good test data for you, like “Christophe Bartell”, “kirsten.greenholt@corkeryfisher.info”, etc.</p> <p><strong>WATIR</strong> is another gem from Ruby’s amazing arsenal, just like “faker”, “pretty_face”, etc. This gem helps in running test automation scripts across different browser and operating systems.</p> <p><strong>Cucumber</strong> is a Ruby gem that helps in writing test scripts in plain English language or in Domain Specific Language (DSL ) using Gherkin syntax – Given, When, And, and Then.</p> <p><strong>RVM</strong> or Ruby Version Manager helps in managing different Ruby projects within the same machine.</p> <p>A developer can switch between different versions to work on several projects with different version requirements. RVM functions as an installer for various implementations of Ruby, such as Matz's Ruby Interpreter (MRI), JRuby, mruby, MacRuby, IronRuby, Maglev, Rubinius, Ruby Enterprise Edition, Topaz, and GoRuby (an interpreter optimized for code golf).</p> <p>Hence, it can be concluded that Ruby is a scripting language, WATIR is one of Ruby’s gems, Cucumber writes BDD test scenarios and RVM manages gems and project.</p> <p>Let’s better understand this better by the following picture.</p> <p><img width="450" height="798" alt="Ruby, Gems, Cucumber, WATIR, and RVM" src="https://gspann.cdn.prismic.io/gspann/2ad49a4f2a5cacc2ebb4c7bac6aa92e087124714_ruby-gems-cucumber-watir-and-rvm.jpg" /></p> <h3 style="text-align: left;">How to install WATIR on different operating systems?</h3> <p><strong>Windows Installation:</strong></p> <ul> <p>Ruby installation on windows is straight-forward. To download the latest version of the installer click on <a href="https://rubyinstaller.org/downloads/">https://rubyinstaller.org/downloads/</a> and start by double-clicking on it. Ruby installer comes with a development kit that helps in building a new project and maintain ruby gems.</p> </ul> <p><strong>Macintosh Installation:</strong></p> <ul> <p>Generally, Ruby comes pre-installed on MacOS, but it is usually the old version. To update Ruby, you can follow the steps below:</p> <li> Install X code <ul> <li>Launch Terminal</li> <li> Type <br><i style="font-family:'Lucida Console', monospace" size=14px;>xcode-select–install</i> and hit return/enter. <br>It will give you a pop prompting to install it. Accept it and you are done. </li> </ul> </li><br> <li>Install RVM – Ruby Version Manager <br>Launch Terminal and type <br><i style="font-family:'Lucida Console', monospace" size=14px;>curl -L https://get.rvm.io | bash -s stable</i></li><br> <li>Install Ruby <br>Launch Terminal and type <br><i style="font-family:'Lucida Console', monospace" size=14px;>rvm install ruby</i><br><i style="font-family:'Lucida Console', monospace" size=14px;>rvm --default use ruby</i></li> </ul> <p><strong>Linux Installation:</strong></p> <ul> <p>Let’s take Ubuntu as an example and learn installation through its ‘apt’ package manager. Launch terminal and type following commands sequentially:</p> <li> First, update the package <br><i style="font-family:'Lucida Console', monospace" size=14px;>sudo apt update</i> </li><br > <li> Install Ruby <br><i style="font-family:'Lucida Console', monospace" size=14px;>sudo apt install ruby-full</i> </li> <p>Ruby installation is completed, now verify it by typing: <br><i style="font-family:'Lucida Console', monospace" size=14px;>ruby --version</i> </ul> <p><strong>Installing WATIR Gem</strong></p> <ul> <p>Before installing WATIR, let’s install another important gem called ‘Bundler’. This helps in updating gems and installing multiple gems in one go.</p> <p>Launch terminal or command window and type <br><i style="font-family:'Lucida Console', monospace" size=14px;>gem install bundler</i></p> <p>now let’s install WATIR <br><i style="font-family:'Lucida Console', monospace" size=14px;>gem install watir-webdriver</i></p> </ul> <p><strong>WATIR Implementation</strong></p> <p>After understanding the BDD approach, it's important to finalize the behavior and then write tests, that to be verified using WATIR. Let’s say, there are multiple open vacancies that need to be verified. In that case, the test scenario will be to ‘verify that there are more than one opportunities at GSPANN.’</p> <p>Test steps:</p> <ul> <li>Go to <a href="https://www.gspann.com/">GSPANN.com</a></li> <li>Mouse hover on the careers tab</li> <li>Click on ‘Find a position’ tab</li> <li>Assert that total opportunities are more than 1</li> </ul> <p>The execution of these test steps would mean that ruby functions are running behind the scenes (since it's already discussed that test steps are glued/mapped with a Ruby function). At this point, there are 2 possible queries. Both are mentioned below with their resolution:</p> <p><strong>Query 1:</strong> How does the code gets to know that there are English sentences to run?</p> <p><strong>Resolution:</strong></p> <p>Ruby is a scripting language which doesn’t understand English sentences but a code written in its own syntax. To let Ruby understand what is being said or map English sentences with Ruby functions, another gem is used which is called Cucumber. Cucumber comes with its own syntax - Gherkin syntax - which is nothing but certain formatting standards and annotations. So, let us first install Cucumber install by launching the terminal and typing:</p> <ul><i style="font-family:'Lucida Console', monospace" size=14px;>gem install cucumber</i></ul> <p>Let’s see the test case again in Cucumber gherkin syntax. Cucumber wants us to define the tests in the following order:</p> <ul> <li> Define <i style="font-family:'Lucida Console', monospace" size=14px;>Feature</i> i.e., what is your test cases about. This is defined only once </li><br> <li> Define <i style="font-family:'Lucida Console', monospace" size=14px;>Scenario</i> i.e., one out of many scenarios that you can test around this feature. </li><br> <li> Use <i style="font-family:'Lucida Console', monospace" size=14px;>Given, When, And, Then</i> annotations to write test steps. </li><ul> <p><strong>Feature:</strong> Verify available opportunities at GSPANN</p> <p><strong>Scenario:</strong> Verify that there are more than one full-time opportunities at GSPANN</p> <p><strong>Given</strong> I am on GSPANN website</p> <p><strong>When</strong> I mouse hover on CAREERS tab</p> <p><strong>And</strong> I click on Find a position tab</p> <p><strong>Then</strong> I see more than 1 open opportunities</p></ul> <p>This makes the first test case which is called a feature file and it ends with <i style="font-family:'Lucida Console', monospace" size=14px;>feature</i> extension</p> </ul> <p><strong>Query 2:</strong> While running Ruby functions, why is it necessary test in English sentences instead of writing Ruby code straight away?</p> <p><strong>Resolution:</strong> Writing tests in Gherkin syntax helps in understanding what requires to be verified, especially in reports. Also, when our test reports go to a stakeholder or a person, who is not aware of the “Steps to Execute” or only interested whether the functionality is passing or not, they can read it and get confidence in what is passing and what is not.</p> <p>Open-source integrated development environment (IDE) for Ruby: It’s suggested to use sublime text for Ruby since it is light-weight and can be customized with the use of a vast range of available plugins.</p> <p><strong>Let’s get hands-on with the example:</strong></p> <p>Let’s understand the project structure first. The project has following directories and files in the order shown below:</p> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> - DemoAutomation - features - pages - step_definitions -support -env.rb -hooks.rb cucumber.yml Gemfile Gemfile.lock </pre> </code> </div><br> <p>Start with the name of the project as root directory, let’s say ‘DemoAutomation’. There is a <i style="font-family:'Lucida Console', monospace" size=14px;>feature</i> directory that keeps all feature files. Create two more directories in the same directory <i style="font-family:'Lucida Console', monospace" size=14px;>step_definitions.</i></p> <p>Pages directory consists of all the pages that are visited during the entire test flow, e.g., Homepage, Careers page, etc. Step definitions directory has the mapping of English sentences that was discussed above in Ruby code.</p> <p>Parallel to features directory, there is a support directory that contains <i style="font-family:'Lucida Console', monospace" size=14px;>env.rb</i> and <i style="font-family:'Lucida Console', monospace" size=14px;>hooks.rb</i>. These two files execute what needs to run before every test. Gemfile contains a list of gems that is required for a project. While making a bundle installation, ruby looks for gems present in this file and installs them together, plus other dependent gems. Once these gems are installed, <i style="font-family:'Lucida Console', monospace" size=14px;>Gemfile.lock</i> is created automatically so that it is clearly known with which the project is running smooth and the locking prevents an automatic upgrade of any gem.</p> <p><strong>env.rb:</strong></p><ul> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> require 'page-object' require 'page-object/page_factory' World(PageObject::PageFactory)</pre> </code> </div><br> <p>The first two lines adds the <i style="font-family:'Lucida Console', monospace" size=14px;>page-object</i> gem and <i style="font-family:'Lucida Console', monospace" size=14px;>page-factory</i> gems, these two gems help in writing less code and makes it readable with less maintenance.</p> <p><strong>Example:</strong></p><div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> @browser.goto https://www.gspann.com </pre> </code> </div><br> <p>It can be simply written as <i style="font-family:'Lucida Console', monospace" size=14px;>visit_page homepage</i> where <i style="font-family:'Lucida Console', monospace" size=14px;>visit_page</i> is the out-of-the-box function in page factory and the homepage is a ruby class, which has the page object <i style="font-family:'Lucida Console', monospace" size=14px;>page_url</i> defined.</p> <p>The last line is a way of initializing/calling <i style="font-family:'Lucida Console', monospace" size=14px;>PageFactory</i> from <i style="font-family:'Lucida Console', monospace" size=14px;>PageObject.</i></p> </ul> <p><strong>hooks.rb:</strong></p> <ul><div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> require 'watir' Before do @browser = Browser.new :chrome end After do @browser.close end </pre> </code> </div> </ul> <p>It also contains the items that were needed before the test started. <i style="font-family:'Lucida Console', monospace" size=14px;>Watir</i> web driver was needed to launch the browser before test starts, and it required to be closed thereafter.</p> <p>Hence, both the files’ were keeping the prerequisites ready.</p> <p><strong>cucumber.yml</strong></p> <p>It keeps the configuration running. For example, which browser needs to run, which test needs to run, etc.</p> <p>Let's keep it simple for this tutorial and run the tests on chrome:</p> <ul> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> default: BROWSER=chrome </pre> </code> </div> </ul> <p><strong>Note:</strong>To run tests in Chrome, keep Chrome binary in your environment properties.</p> <p>Hence, the feature file is put inside the features folder as <i style="font-family:'Lucida Console', monospace" size=14px;>careers_tab.feature</i>. A feature file must end with ‘.feature’ extension.</p> <p>Every step or sentence has a ruby function defined. Keeping such files in a ruby (.rb extension) file called ‘step definitions’. To store all these at one place, a specific folder is there which is called ‘step_definitions’. Just go to this folder and create one .rb file. Name of this ruby file will be the same as the feature, i.e., <i style="font-family:'Lucida Console', monospace" size=14px;>careers_tab.rb</i></p> <p>Now, let’s run this feature file. Open the terminal, command prompt, go to the project folder and type the following command:</p> <ul><div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> cucumber features/careers_tab.feature </pre> </code> </div></ul> <p>You can see that it gives and shows the steps that are not defined along with showing how the steps can be defined, just like it is shown in the image below.</p> <p><img width="750" height="395" alt="Cucumber Features" src="https://prismic-io.s3.amazonaws.com/gspann%2F9592856e-f09c-4055-8ad2-5e5773b02fbf_cucumber+features.jpg" /></p> <p>Let’s copy and paste all these functions in the steps file <i style="font-family:'Lucida Console', monospace" size=14px;>careers_tab.rb</i>. Now, remove pending lines and call ruby functions instead. Create another .rb file inside step_definitions folder <i style="font-family:'Lucida Console', monospace" size=14px;>career_tab_helper.rb</i>. The first method is created in this case is</p><ul> <p><i style="font-family:'Lucida Console', monospace" size=14px;>go_to_gspann_website</i></p> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> def go_to_gspann_website end </pre> </code> </div><br> <p>After the first ruby function is created, write Ruby code inside this function.</p> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> def go_to_gspann_website visit_page gspann_home_page end </pre> </code> </div><br> <P>Here, visit_page is an out-of-the-box function which comes with another gem called page_object. Let’s install this gem by typing</P> <p><i style="font-family:'Lucida Console', monospace" size=14px;>gem install page_object</i></p> <p>and gspann_home_page is the page class, i.e., another ruby file insides pages folder</p> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> class GspannHomePage include PageObject page_url "http://www.gspann.com" end </pre> </code> </div></ul> <p>Before checking everything that is done, there is a small change. It's important to call a helper class in <i style="font-family:'Lucida Console', monospace" size=14px;>step_definition</i>. For that, go to <i style="font-family:'Lucida Console', monospace" size=14px;>careers_tab.rb</i> and on top mention</p> <ul> <div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> require_relative "career_tab_helper" </pre> </code> </div><br> <p>and, hence, the step definition will look like this</p><div class="col-md-10"> <code> <pre style="padding: 0px 20px; margin: 0px; font-size: 15px; text-align: left; border:1px solid #B5B5B5; background-color: #F4F4F4;"> require_relative "career_tab_helper" Given("I am on GSPANN website") do goto_gspann_homepage end </pre> </code> </div></ul> <p>Run this feature file again and you’ll see that first step does not appear as pending instead it launches the browser and go to GSPANN’s homepage. By now, you must have understood the working of this amazing tool. You can also write ruby functions for other steps also and run the feature file.</p> <p>Thus, as an open-source tool, WATIR is most suitable for browser automation, whether it is on desktop or mobile devices. Vast gems repository can help you in writing less code and are less maintenance. Although WATIR has a limitation that it cannot be used for “Native mobile app automation” and “API testing”, it can be overcome by using Ruby and Cucumber without using WATIR by making some tweaks in the project. Finally, it is fun coding in Ruby. Happy Coding!</p>
gspann logo
    • Services
      • STRATEGY
      • ARCHITECTURE
      • CONTENT MANAGEMENT
      • E-COMMERCE ECOSYSTEM
      • DATA AND ANALYTICS
      • DEVOPS
      • PRODUCTION SUPPORT
      • QUALITY ENGINEERING AND ASSURANCE
      • MOBILITY
    • Platforms
      • Salesforce Commerce Cloud
      • SAP S/4HANA
      • Adobe Experience Cloud
    • Innovations
      • Revolutionary Automated QA Testing and Auditing Tool
      • Configurable Auto-Generating Big Data Ingestion Engine
      • An AIOPS Platform that Leverages AI and ML Capabilities
    • Resources
    • Jobs
      • Find out how GSPANN's culture, family, focus and challenging projects set us apart from other service organizations.
      • Find the current positions that GSPANN has available in technology, marketing, sales and administration.
      • Join GSPANN and accelerate your growth.
      • Learn about and register for upcoming walk-in events
    • About
      • Meet our transformational leadership
      • Check out our latest news stories and press releases