<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>page-objects-2 &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/page-objects-2/</link>
	<description>Feed of posts on WordPress.com tagged "page-objects-2"</description>
	<pubDate>Thu, 20 Jun 2013 02:40:33 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Rspec, page objects and user flows]]></title>
<link>http://watirmelon.com/2012/06/22/rspec-page-objects-and-user-flows/</link>
<pubDate>Fri, 22 Jun 2012 12:44:47 +0000</pubDate>
<dc:creator>Alister Scott</dc:creator>
<guid>http://watirmelon.com/2012/06/22/rspec-page-objects-and-user-flows/</guid>
<description><![CDATA[I love it when people challenge my views. Recently, Chris McMahon from WMF challenged my view: is Cu]]></description>
<content:encoded><![CDATA[<p>I love it when people challenge my views.</p>
<p>Recently, <a href="http://www.mediawiki.org/wiki/User:Cmcmahon">Chris McMahon</a> from <a href="http://wikimediafoundation.org/wiki/Home">WMF</a> challenged my view: is Cucumber best for end-to-end testing when RSpec will do? Especially in an environment where non-technical people aren&#8217;t involved in writing the specifications.</p>
<p>Admittedly, Cucumber does create some overhead (those pesky step definitions with pesky regular expressions (that I love)), but it does give you plain English (or <a href="https://github.com/cucumber/cucumber/wiki/Spoken-languages">LOLCAT</a>) written, human readable, executable specifications. But the overhead gives you something more. It gives you a way to create reusable chunks of code that you can call wherever you want when want to test something (or set something up). For example: <em>Given I am logged into Wikipedia. </em>But what do you do if you&#8217;re using RSpec?</p>
<p><em><a href="http://watirmelon.files.wordpress.com/2012/06/cucumber-vs-rspec.jpg"><img class="alignnone size-full wp-image-1357" title="Cucumber vs RSpec" src="http://watirmelon.files.wordpress.com/2012/06/cucumber-vs-rspec.jpg?w=584&#038;h=477" alt="" width="584" height="477" /></a></em></p>
<p>What is needed is something to sit in between RSpec code and page objects that provide common functionality to reduce repetition of code, such as logging in, in each RSpec specification.</p>
<p>I&#8217;ve been trying to come up with a good term to describe these and the best I can come up with is &#8216;<strong>user flows</strong>&#8216;, as essentially they are a flow that a user follows throughout the system, spanning multiple pages.</p>
<p><strong>Adding user flows to the Wikimedia custom page object project</strong></p>
<p>I thought I would experiment with user flows in the <a href="https://github.com/alisterscott/wmf-custom-page-object">Wikimedia custom page object project</a> that <a title="Roll your own page objects" href="http://watirmelon.com/2012/06/04/roll-your-own-page-objects/">I recently created</a>. First I replicated my Cucumber features as RSpec specs, which was easy enough, but started to notice a lot of duplication of code.</p>
<p>For example: this was repeated at the beginning of most specs:</p>
<pre class="brush: ruby; title: ; notranslate" title="">
visit Wikipedia::LoginPage do &#124;page&#124;
  page.login_with USERNAME, PASSWORD
  page.should be_logged_in
end
</pre>
<p>Whilst it isn&#8217;t a huge amount of repetition (thanks to the useful <em>login_with</em> method on the LoginPage), it&#8217;s still not ideal. Enter ruby modules.</p>
<p><strong>Using modules to store pages and flows</strong></p>
<p>As far as I know, modules perform two main functions in ruby: first as a namespace for classes (such as the Wikipedia::LogonPage class), and secondly as a way to group methods that don&#8217;t belong in a class, which are often &#8216;mixed into&#8217; other classes. Perfect! A spot to store our flows.</p>
<p>So, since I already had both a Wikipedia and Commons module, I could simply add module methods to these modules to represent our user flows.</p>
<pre class="brush: ruby; highlight: [7,8,9,10,11,12]; title: ; notranslate" title="">
module Wikipedia

  extend PageHelper
  extend RSpec::Expectations
  extend RSpec::Matchers

  def self.ensure_logged_in
    visit Wikipedia::LoginPage do &#124;page&#124;
      page.login_with USERNAME, PASSWORD
      page.should be_logged_in
    end
  end
end
</pre>
<p><strong>Wiring these up to RSpec</strong></p>
<p>I needed to do a little wiring to ensure these user flows can be easily used in RSpec. In my <em>spec_helper.rb</em> file (which is the equivalent to <em>cucumber.rb</em> in Cucumberland), I added the following to ensure that my browser object I created in RSpec is available to use in the flows:</p>
<pre class="brush: ruby; title: ; notranslate" title="">
RSpec.configure do &#124;config&#124;
  config.include PageHelper
  config.before(:each) do
    @browser = browser
    Commons.browser = @browser
    Wikipedia.browser = @browser
  end
  config.after(:suite) { browser.close }
end
</pre>
<p>and that was all that was needed to start using my user flows in my RSpec specifications.</p>
<p>A completed RSpec specification in my WMF suite looks something like this:</p>
<pre class="brush: ruby; title: ; notranslate" title="">
describe 'Editing a Wikipedia Page' do

  context 'Logged on user' do

    it 'should be able to edit own user page' do
      Wikipedia::ensure_logged_in
      content, edit_message = Wikipedia::edit_user_page_by_adding_new_content
      visit Wikipedia::UserPage do &#124;page&#124;
        page.page_content.should include content
        page.view_history
        page.history_content.should include edit_message
        page.history_content.should include Wikipedia::USERNAME
      end

    end

  end

end
</pre>
<p>where <em>Wikipedia::ensure_logged_in</em> and <em>Wikipedia::edit_user_page_by_adding_new_content</em> are two user flows that I defined in the Wikipedia module.</p>
<p><strong>Summary</strong></p>
<p>I found using page objects directly in RSpec lacking, so I created a concept of user flows in modules that can be easily used from RSpec to reduce repetition and increase readability of specs. If I were to use RSpec for end to end tests, I would find this incredibly useful as a replacement for what Cucumber steps provide.</p>
<p><a href="http://watirmelon.files.wordpress.com/2012/06/rspec-user-flows.jpg"><img class="alignnone size-full wp-image-1358" title="Rspec user flows" src="http://watirmelon.files.wordpress.com/2012/06/rspec-user-flows.jpg?w=584&#038;h=472" alt="" width="584" height="472" /></a></p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Roll your own page objects]]></title>
<link>http://watirmelon.com/2012/06/04/roll-your-own-page-objects/</link>
<pubDate>Mon, 04 Jun 2012 11:50:14 +0000</pubDate>
<dc:creator>Alister Scott</dc:creator>
<guid>http://watirmelon.com/2012/06/04/roll-your-own-page-objects/</guid>
<description><![CDATA[There seems to be a lot of focus being put into page object ruby gems at the moment. Cheezy has done]]></description>
<content:encoded><![CDATA[<p>There seems to be a lot of focus being put into page object ruby gems at the moment. <a href="http://www.cheezyworld.com/">Cheezy</a> has done a fantastic job of the aptly named <a href="https://github.com/cheezy/page-object">page-object</a> that supports <a href="http://watirwebdriver.com">Watir-Webdriver</a> and Selenium-Webdriver, and then there&#8217;s the more recent <a href="https://github.com/natritmeyer/site_prism">site_prism</a> (also fantastic) by Nat Ritmeyer that works with <a href="https://github.com/jnicklas/capybara">Capybara</a>. Before these two came along, I even wrote my own; the now retired <a title="Introducing the Watir Page Helper gem" href="http://watirmelon.com/2011/05/05/introducing-the-watir-page-helper-gem/">watir-page-helper gem</a>.</p>
<p>The premise of these gems is they make it super easy to create page objects for your ruby automated testing projects. But today I want to discuss another crazy idea with you: do you even need a gem at all to do page objects?</p>
<p><strong>Background</strong></p>
<p>I recently <a href="https://github.com/alisterscott/wmf-custom-page-object">refactored</a> <a href="https://github.com/chrismcmahon/Page-Object-WMF-spike">some automated tests</a> that <a href="http://chrismcmahonsblog.blogspot.com.au/">Chris McMahon</a> wrote as a potential framework for Wikimedia Foundation (creators of Wikipedia). Chris&#8217;s code used Cheezy&#8217;s excellent page-object gem so I happily went about my refactoring his code using that gem. Suddenly&#8230; I found instead of helping me it started to hinder me. I kept having to refer to page-object user guide I got from Cheezy in Austin to work out how things work. Namely:</p>
<ol>
<li><strong>How to define elements:</strong> as they are different from watir-webdriver (eg. <em>list_item</em> vs <em>li</em>, <em>cell</em> vs <em>td</em> etc.)</li>
<li><strong>How to identify elements:</strong> as they are limited to certain supported attributes by element type, unlike watir-webdriver which supports every attribute for all elements</li>
<li><strong>What methods each element provides and what each does:</strong> as different elements create different methods with different behaviours, so calling a link element <em>clicks</em> it, whilst a span element <em>returns its text</em>.</li>
</ol>
<p>The main problem I personally found was that Page-object has essentially created its own DSL to describe elements in page objects, and this DSL is subtly and not so subtly different from the Watir-Webdriver API, so the API I know and love doesn&#8217;t work in a lot of places.</p>
<p>An example. There&#8217;s a common menu bar on all the Wikimedia sites that displays the logged in user as a link (to the user&#8217;s page).</p>
<p><a href="http://watirmelon.files.wordpress.com/2012/06/screen-shot-2012-06-04-at-3-55-49-pm.png"><img class="alignnone size-full wp-image-1309" title="Wikimedia Menu Bar" src="http://watirmelon.files.wordpress.com/2012/06/screen-shot-2012-06-04-at-3-55-49-pm.png?w=456&#038;h=27" alt="" width="456" height="27" /></a></p>
<p>The link is only recognizable by its <em>title</em> attribute, and whilst this is supported by watir-webdriver (it supports <em>any</em> attribute), it is not supported by page-object. The source html looks like:</p>
<pre class="brush: xml; light: true; title: ; notranslate" title="">
&#60;a class=&#34;new&#34; accesskey=&#34;.&#34; title=&#34;Your user page [ctrl-.]&#34; href=&#34;/wiki/User:Alister.scott&#34;&#62;Alister.scott&#60;/a&#62;
</pre>
<p>What I would have liked to do was:</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
  link :logged_in_as, :title =&#62; 'Your user page [ctrl-.]'
</pre>
<p>But, instead, I had to do this (which isn&#8217;t very <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>):</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
  def logged_in_as
    @browser.link(:title =&#62; 'Your user page [ctrl-.]').text
  end
</pre>
<p>I believe essentially what has happened is the page-object, in its neutrality between selenium-webdriver and watir-webdriver, has created its own DSL that is somewhat of a halfway point between the two. This is probably fine for most people starting out, it&#8217;s just an API to learn, but for someone like me who has extensive experience with the watir-webdriver API (and loves the power of it), I find it limiting. This is particularly evident when I write a majority of my code using the watir-webdriver API under IRB.</p>
<p>So, I had to take a re-think. Why not roll my own page objects for Wikimedia Foundation?</p>
<p><strong>Roll your own page objects</strong></p>
<p>I recently had a discussion with a colleague/good friend about page objects which went along the lines of &#8220;I don&#8217;t understand those page object gems because you end up writing a custom page object pattern for each project anyway, as every project/application you work on is different in its own way&#8221;. It was one of those <a href="http://en.wikipedia.org/wiki/Eureka_effect">aha</a> moments for me.</p>
<p>What I needed was to roll my own Wikimedia page objects.</p>
<p>Taking it back to basics, essentially there are three functions I see a page object pattern provides:</p>
<ol>
<li>Ability to easily instantiate pages in a consistent manner;</li>
<li>Ability to concisely describe elements on a page, keep it DRY by avoiding repetition of element identifiers (using the underlying driver&#8217;s API); and</li>
<li>Ability to provide higher level methods that use the elements to perform user oriented functions.</li>
</ol>
<p>You can probably notice the helper methods &#8211; the magic &#8211; that gems like page-object and site_prism provide are missing from my list. This is on purpose, and is because, after lots of thought, I actually don&#8217;t find these useful, as they encourage specifications/steps to be too lower level. I would rather a high level method on the page (eg. <em>login</em>) than exposing my <em>username</em> and <em>password</em> fields and a <em>login</em> button.</p>
<p><strong>A Wikimedia Page Model</strong></p>
<p><a href="http://watirmelon.files.wordpress.com/2012/06/img_0018.jpg"><img class="alignnone size-full wp-image-1310" title="Wikimedia Page Model" src="http://watirmelon.files.wordpress.com/2012/06/img_0018.jpg?w=584&#038;h=438" alt="" width="584" height="438" /></a></p>
<p>Taking those things into consideration: this is the page model I came up with for Wikimedia.</p>
<p><strong>Generic Base Page Class</strong></p>
<p>The generic base page class is what everything else extends. It contains the instantiation code common to all pages, and the class methods needed to define elements and methods (more on these later).</p>
<p><strong>Wikimedia Base Page Class</strong></p>
<p>This page class contains elements and methods that are common to all Wikimedia pages. The &#8216;logged in user&#8217; example above is a good example of something that is the same on every Wikimedia page, whether you&#8217;re on Wikipedia, or Wikimedia Commons etc.</p>
<p><strong>Commons &#38; Wikipedia Base Page Classes</strong></p>
<p>These two classes are placeholders for elements and methods are common to a particular site. At the moment with my limited examples, these don&#8217;t contain content.</p>
<p><strong>Commons &#38; Wikipedia Page Classes</strong></p>
<p>These are the actual pages that are representations of pages in Wikimedia. These are in separate modules so they are in different namespaces (you can have a Wikipedia::LogonPage and a Commons::LogonPage).</p>
<p>Some example pages:</p>
<pre class="brush: ruby; title: ; notranslate" title="">
class Wikipedia::LogoutPage &#60; Wikipedia::BasePage
  page_url &#34;#{Wikipedia::BASE_URL}/w/index.php?title=Special:UserLogout&#34;
  expected_title &#34;Log out - #{Wikipedia::TITLE}&#34;

  element(:main_content_div) { &#124;b&#124; b.div(id: 'mw-content-text' ) }
  value(:main_content) { &#124;p&#124; p.main_content_div.text }
end
</pre>
<p>Here we can see we define a <em>page_url</em> and <em>expected_title</em>, which are used to instantiate the page.</p>
<p>Next we define an <em>element</em> passing in a block of watir-webdriver code for it, and a <em>value</em> by referencing the element we defined before it. Since these element and value methods execute blocks against self, and the class delegates missing methods to our browser, we can refer to either the <em>browser</em> (shown as b) or the <em>page</em> <em>class</em> (shown as p) in our blocks.</p>
<pre class="brush: ruby; title: ; notranslate" title="">
class Commons::LoginPage &#60; Commons::BasePage
  page_url &#34;#{Commons::BASE_URL}/w/index.php?title=Special:UserLogin&#34;
  expected_title &#34;Log in / create account - #{Commons::TITLE}&#34;

  login_elements
  value(:logged_in?) { &#124;p&#124; p.logged_in.exists? }

  def login_with username, password
    username_field.set username
    password_field.set password
    login_button.click
  end
end
</pre>
<p>In this example, we again define the <em>page_url</em> and <em>expected_title</em>, but we have stored the <em>login_elements</em> with the <em>WikimediaBasePage</em> (as they are the same across all the sites) so we include them by specifying <em>login_elements</em>. We have also defined a <em>login_with</em> method that performs actions on our elements.</p>
<p>There are three available methods to define page elements, values and actions, and these all follow the same format of specifying the method name, and passing in a block of watir-webdriver code.</p>
<p><strong>Calling the page objects from Cucumber step definitions</strong></p>
<p>I chose to use Cucumber for the Wikimedia Foundation framework over Chris&#8217;s choice of RSpec as I find it easier to specify end-to-end tests in this way. I find the Cucumber step definitions encourage reuse of steps typically used to set up a test (that are often duplicated in RSpec).</p>
<p>I try to stick to calling the exposed methods, values or actions instead of the elements themselves from my Cucumber steps to ensure I am writing them at a high level. An example step using the page above looks like:</p>
<pre class="brush: ruby; title: ; notranslate" title="">
Given /^I am logged into Commons$/ do
  visit Commons::LoginPage do &#124;page&#124;
    page.login_with Commons::USERNAME, Commons::PASSWORD
    page.should be_logged_in
  end
end
</pre>
<p>The <em>visit</em> and <em>on</em> methods are defined in a <em>Pages</em> module that is mixed into the Cucumber World so these available on all step definitions. As named, the <em>visit</em> method instantiates and visits the page, whereas the <em>on</em> just instantiates it.</p>
<pre class="brush: ruby; title: ; notranslate" title="">
module Pages
  def visit page_class, &#38;block
    on page_class, true, &#38;block
  end

  def on page_class, visit=false, &#38;block
    page = page_class.new @browser, visit
    block.call page if block
    page
  end
end
</pre>
<p><strong>Summary</strong></p>
<p>That&#8217;s all there really is the rolling your own page objects. I found this excercise useful as it gives me maximum flexibility and allows me to clearly define pages how I want to define them. I appreciate all the great work that Cheezy and Nat have done on their page object gems, if anything these contain great inspirations on how to roll your own custom page objects most suited to your environment and applications.</p>
<p>You can check out my full code <a href="https://github.com/alisterscott/wmf-custom-page-object">here on Github</a>.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Page objects returning page objects]]></title>
<link>http://watirmelon.com/2012/05/29/page-objects-returning-page-objects/</link>
<pubDate>Tue, 29 May 2012 05:53:33 +0000</pubDate>
<dc:creator>Alister Scott</dc:creator>
<guid>http://watirmelon.com/2012/05/29/page-objects-returning-page-objects/</guid>
<description><![CDATA[It seems to be common for page object automated testing frameworks to encourage the chaining of page]]></description>
<content:encoded><![CDATA[<p>It seems to be common for page object automated testing frameworks to encourage the chaining of page objects through page objects returning other page objects. The selenium team recommends this on their <a href="http://code.google.com/p/selenium/wiki/PageObjects">page objects page</a> (emphasis added):</p>
<blockquote>
<h2>Summary</h2>
<ul>
<li>The public methods represent the services that the page offers</li>
<li>Try not to expose the internals of the page</li>
<li>Generally don&#8217;t make assertions</li>
<li><strong>Methods return other <a href="http://code.google.com/p/selenium/wiki/PageObjects">PageObjects</a></strong></li>
<li>Need not represent an entire page</li>
<li>Different results for the same action are modelled as different methods</li>
</ul>
</blockquote>
<p>I don&#8217;t agree with this however. I find returning other page objects creates more tightly coupled pages with less flexibility, and duplication of code, for example, as the selenium page shows, there are two methods required: &#8216;<em>loginAs</em>&#8216; and &#8216;<em>loginAsExpectingError</em>&#8216;, each doing roughly the same thing but each returning a different page.</p>
<p>I prefer to not return pages, but rather instantiate each page from the calling code, usually my Cucumber of SpecFlow step definitions. This means you can call any page in any order, which is often the case in a web app &#8211; for example, a search page may go straight to the puppies page when searching for &#8216;puppies&#8217; as shown below, and this could be simply represented by a single method: search_for.</p>
<p><a href="http://watirmelon.files.wordpress.com/2012/05/img_0016.jpg"><img class="alignnone size-full wp-image-1304" title="Page Flow" src="http://watirmelon.files.wordpress.com/2012/05/img_0016.jpg?w=584&#038;h=438" alt="" width="584" height="438" /></a></p>
<p><strong>Better ways to instantiate pages in calling code</strong></p>
<p>The most straightforward way to instantiate pages in calling code (for example, Cucumber steps) is to create new objects each time you want to interact with a page.</p>
<p>In ruby this would be like:</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
@search_page = SearchPage.new @browser
@search_page.search_for 'kittens'
</pre>
<p>or in C#:</p>
<pre class="brush: csharp; light: true; title: ; notranslate" title="">
SearchPage = new SearchPageModel(Driver);
SearchPage.SearchFor(&#34;kittens&#34;);
</pre>
<p>A nicer way in ruby is to use blocks and <a title="Removing local page references from Cucumber steps" href="http://watirmelon.com/2011/06/07/removing-local-page-references-from-cucumber-steps/">automatically instantiate pages as needed</a> (this is supported in the <a href="https://github.com/cheezy/page-object">page-object ruby gem</a>):</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
on SearchPage do &#124;page&#124;
  page.search_for 'kittens'
end
</pre>
<p>Whilst I don&#8217;t think there&#8217;s anything this nice in C#, I found using the &#8216;using&#8217; statement makes it look a little similar:</p>
<pre class="brush: csharp; light: true; title: ; notranslate" title="">
using (var page = new SearchPageModel(Driver))
{
  page.SearchFor(&#34;kittens&#34;);
}
</pre>
<p><strong>Summary</strong></p>
<p>I prefer to keep my page objects loosely coupled for maximum flexibility and reuse, and I find using blocks allows me to easily instantiate pages as needed.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Five page object anti-patterns]]></title>
<link>http://watirmelon.com/2012/04/01/five-page-object-anti-patterns/</link>
<pubDate>Sun, 01 Apr 2012 10:49:49 +0000</pubDate>
<dc:creator>Alister Scott</dc:creator>
<guid>http://watirmelon.com/2012/04/01/five-page-object-anti-patterns/</guid>
<description><![CDATA[I&#8217;ve observed some page object anti-patterns which commonly arise when starting out designing]]></description>
<content:encoded><![CDATA[<p>I&#8217;ve observed some page object anti-patterns which commonly arise when starting out designing end-to-end automated tests. Chris McMahon recently asked for some feedback on his initial <a href="https://github.com/chrismcmahon/Page-Object-WMF-spike">test spike for Wikipedia</a>, and some of these anti-patterns were present.</p>
<p><strong>Anti-pattern one: frequently opening and closing browsers</strong></p>
<p>I often see both RSpec and Cucumber tests frequently opening and closing browsers. This slows down test execution times, and should be avoided unless absolutely necessary.</p>
<p>You can clear cookies between tests if you&#8217;re worried about state.</p>
<p>To open and close the browser only once in <strong>Cucumber</strong>, specify this in your <em>env.rb</em> file:</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
browser = Watir::Browser.new

Before do
  @browser = browser
end

at_exit do
  browser.close
end
</pre>
<p>To open and close the browser only once in <strong>RSpec:<br />
</strong></p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
browser = Watir::Browser.new

RSpec.configure do &#124;config&#124;
  config.before(:each) { @browser = browser }
  config.after(:suite) { browser.close }
end
</pre>
<p><strong>Anti-pattern two: hard coding URLs on page classes</strong></p>
<p><strong></strong>Chances are you&#8217;ll at some point run your automated tests in different environments, even if it&#8217;s just to verify that production has been updated correctly. If you&#8217;ve hard coded URLs in page classes, this can be problematic.</p>
<p>Fortunately it&#8217;s easy to avoid, by creating a module that contains base URLs which can be accessed by page classes. These base URLs can be stored in YAML files which can be switched for different environments.</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
module Wikipedia
  BASE_URL = 'https://en.wikipedia.org'
end

class BogusPage
  include PageObject
  page_url &#34;#{Wikipedia::BASE_URL}/wiki/Bogus_page&#34;
end
</pre>
<p><strong>Anti-pattern three: pages stored as instance variables in steps or rspec specs</strong></p>
<p>I don&#8217;t like seeing pages stored as instance variables (those starting with an @) in Cucumber steps or RSpec specs, as it introduces state and thus more room for error.</p>
<p>If you&#8217;re using the page-object gem, there are two methods available to access pages directly without using instance variables: <strong>visit_page</strong> and <strong>on_page</strong> (also <strong>visit</strong> or <strong>on</strong> from 0.6.4+). Both of these can be used as blocks, so you can perform multiple actions within these methods.</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
visit LoginPage do &#124;page&#124;
  page.login_with('foo', 'badpass')
  page.text.should include &#34;Login error&#34;
  page.text.should include &#34;Secure your account&#34;
end
</pre>
<p><strong>Anti-pattern four: checking the entire page contains some text somewhere</strong></p>
<p>I often see people checking that the entire web page contains some expected text. Even if the text was at the very bottom of the page hidden in the footer the test would probably pass.</p>
<p>You should check the text is where it should be, using a container that it should belong to. Ideally a span or a div may exist that contains the exact text, but even if it&#8217;s in a slightly larger container it is still better than asserting it exists <em>somewhere</em> on the page.</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
class BogusPage
  include PageObject
  cell :main_text, :class =&#62; 'mbox-text'
end

visit_page BogusPage do &#124;page&#124;
  page.main_text.should include 'Wikipedia does not have an article with this exact name'
  page.main_text.should include 'Other reasons this message may be displayed'
end
</pre>
<p><strong>Anti-pattern five: using RSpec for end-to-end tests</strong></p>
<p>This one is contentious, and I am sure I&#8217;ll get lots of opinions to the contrary, but I believe that RSpec is best suited to unit/integration tests, and Cucumber is suited to end-to-end tests.</p>
<p>I find I create duplication when trying to do end-to-end tests in RSpec, which is where Cucumber step definitions come in. Trying to do unit tests in Cucumber seems like too much overhead, and in my opinion is more suited to RSpec.</p>
]]></content:encoded>
</item>
<item>
<title><![CDATA[Watir-page-helper has been end-of-lifed]]></title>
<link>http://watirmelon.com/2012/03/29/watir-page-helper-has-been-end-of-lifed/</link>
<pubDate>Thu, 29 Mar 2012 03:15:07 +0000</pubDate>
<dc:creator>Alister Scott</dc:creator>
<guid>http://watirmelon.com/2012/03/29/watir-page-helper-has-been-end-of-lifed/</guid>
<description><![CDATA[Intro I am happy to announce that the watir-page-helper gem has been end-of-lifed: meaning no furthe]]></description>
<content:encoded><![CDATA[<p><strong>Intro</strong></p>
<p>I am happy to announce that the <a href="http://rubygems.org/gems/watir-page-helper">watir-page-helper</a> gem has been <a href="http://en.wikipedia.org/wiki/End-of-life_%28product%29">end-of-lifed</a>: meaning no further development will happen on it and it will remain as it stands. I am thoroughly supportive of <a href="http://www.cheezyworld.com/">Cheezy&#8217;s</a> <a href="http://rubygems.org/gems/page-object">page-object</a> gem and recommend you move onto this when you have the first opportunity to do so.</p>
<p><strong>Background</strong></p>
<p>When I initially released this gem, Cheezy had yet to release his page-object gem. I borrowed a lot of his concepts and packaged them into a gem specifically for watir-webdriver. A few weeks later, Cheezy released his page-object gem that not only supports watir-webdriver, but also selenium-webdriver. He has iterated faster than I have and his gem has come along way to support additional features such as defining page-routes and default data. I have supplied a couple of tiny pull requests to him and there is nothing now that watir-page-helper can offer that page-object can&#8217;t, so I am EOL&#8217;ing watir-page-helper.</p>
<p><strong>So how do I change to use page-object instead of watir-page-helper?</strong></p>
<p>I converted an existing suite of tests that use watir-page-helper to run against Etsy to use the page-object gem (<a href="https://github.com/alisterscott/etsy-page-object-demo">link to tests</a>). It was a fairly straightforward task, and here are the fundamental differences:</p>
<ul>
<li>Accessing an actual element object is done via an _element suffix: for example, defining a link :home, provides a home_element method. Watir-page-helper provided a home_link method, and I prefer Cheezy&#8217;s approach (the home_link method is actually supported by page-object but you should move away from it).</li>
<li>Watir-page-helper supported p,li,ol as methods, but these are defined as paragraph, list_item, and ordered_list in page-object.</li>
</ul>
<p><strong>Using Cheezy&#8217;s Page Factory</strong></p>
<p>One of the neat features of the page-object gem is the <a href="https://github.com/cheezy/page-object/wiki/Creating-and-using-page-objects">page factory</a>. This is a way to conveniently access pages from Cucumber steps and the like.</p>
<p>There are a couple of things you need to do:</p>
<p>In env.rb, you should mix in the PageObject::PageFactory module</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
require 'page-object/page_factory'
World(PageObject::PageFactory)
</pre>
<p>You then have access to helper methods to visit and set the page you want to use (originally there were just <em>visit_page</em>, and <em>on_page</em>, but I added <em>on</em> and <em>visit</em> &#8211; I think they read nicer):</p>
<pre class="brush: ruby; light: true; title: ; notranslate" title="">
visit_page RegistrationPage
on_page HomePage
visit HomePage
on HomePage
</pre>
<p><strong>Summary</strong></p>
<p>Good things come to an end and get replaced by even better things. Here&#8217;s to your future success using the page-object gem.</p>
]]></content:encoded>
</item>

</channel>
</rss>
