Watir - Page Objects



Page Object in Watir helps us to reuse the code in the form of classes. Using the page object feature, we can automate our app without having to duplicate any code and also makes the code manageable.

When testing, we can create page object for each of the page we are going to test. Then, we are going to access the methods and properties using the page object.

The reasons behind using page object −

  • In case any changes are done to the page at alter changes, re-writing the code is not needed.

  • To avoid code redundancy.

We are going to use RSpec to make use of page-object in Watir. Incase if you are not familiar with RSpec, here is a full tutorial available for RSpec for you learning.

The page that we are going to perform test on is given here −

textbox.html

<html>
   <head>
      <title>Testing UI using Watir</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         function wsentered() {
            console.log("inside wsentered");
            var firstname = document.getElementById("firstname");
            
            if (firstname.value != "") {
               document.getElementById("displayfirstname").innerHTML = 
                  "The name entered is : " + firstname.value;
               
               document.getElementById("displayfirstname").style.display = "";
            }
         }
      </script>
      
      <div id = "divfirstname">
         Enter First Name : 
         <input type = "text" id = "firstname" name = "firstname" onchange = "wsentered()" />
      </div>
      <br/>
      <br/>
      <div style = "display:none;" id = "displayfirstname"></div>
   </body>
</html>

Output

Working with Textboxes Run Output

We will now create page object for the above page as shown below −

pageobjecttest.rb

class InitializeBrowser
   def initialize(browser)
      @browser = browser
   end
end

class TestPage lt; InitializeBrowser
   def textbox
      @textbox = TestTextbox.new(@browser)
   end
   def close
      @browser.screenshot.save 'usingpageobject.png'
      @browser.close
   end
end # TestPage

class TestTextbox < InitializeBrowser
   URL = "http://localhost/uitesting/textbox.html"

   def open
      @browser.goto URL
      self
   end
   def enterdata_as(name)
      name_field.set name
      name_field.fire_event('onchange')
   end

private
   def name_field
      @browser.text_field(:id > "firstname")
   end
end # TestTextbox

There are three classes defined - InitializeBrowser, TestPage and TestTextbox −

  • InitializeBrowser − This will initialize the browser opened and share the browser object with TestPage and TestTextbox classes.

  • TestPage − This class will have object reference to TestTextbox and contains the method to capture screenshot and close the browser.

  • TestTextbox − This class will have methods to open the page url, give reference to textfield, set the data and fire onchange event.

Once you execute the code shown above, you can see the output as shown below −

Using ID Element

Advertisements