0

Best practice for checking for error messages using Page Objects. So I have duplication in regards to my code where I'm checking for various error messages. My question is using Page Objects/page_objects gem, is there a way I can remove this duplication? My code:

def check_error_message
  expected_text = "The highlighted fields must be filled out correctly."
  css = "button-error"
  actual_text = @browser.span(:class, css).text

  actual_text.should == expected_text
  puts "Span class '#{css}' expected text: #{expected_text}"
  puts "Span class '#{css}' actual text: #{actual_text}"
end

def check_password_weak_message
  expected_text = "Password is too weak, please choose a different password."
  css = "formError"
  actual_text = @browser.div(:class, css).text

  actual_text.should == expected_text
  puts "Span class '#{css}' expected text: #{expected_text}"
  puts "Span class '#{css}' actual text: #{actual_text}"
end

def check_dupe_email_message
  expected_text = "This email address is already in use by another ID.me account"
  css = "label-error"
  actual_text = @browser.div(:class, css).text

  actual_text.should == expected_text
  puts "Span class '#{css}' expected text: #{expected_text}"
  puts "Span class '#{css}' actual text: #{actual_text}"
end

Things that change in the methods: expected_text, css, actual_text.

Alexander Karmes
  • 2,438
  • 1
  • 28
  • 34
Farooq
  • 1,925
  • 3
  • 15
  • 36

1 Answers1

0

The page object gem will not help you in this case.

In your case, lets not talk about page object gem. Instead, lets simply use the generic term 'page object pattern' as that is what the page object gem implements.

The PO pattern is intended to create an abstraction layer between your web page under test and your test code.

In your case, where you have statements such as

actual_text = @browser.div(:class, css).text

The PO pattern will place all cryptic statements like this into a single place that is called a 'page object.' When this happens you create methods that are used to interact with the page instead of making the direct (often repeated and non-descriptive) Selenium Web driver calls. This is an over simplification of PO pattern, but I don't intend on explaining that here.

With these test methods you have created, you have a set of very simple and functional tests that are fairly easy to create. So you can create them quickly, but what you are intuitively realizing is that you are repeating yourself, which if you expand this test suite out to even just a few hundred test cases you will run into a major maintenance nightmare.

As such, you need to invest in some more complete automation frameworks to help you. For example you might consider using cucumber, rspec, capybara, or any of the many other test frameworks that help you manage the test data in a separate location and begin adding abstraction and specific areas of responsibility such as classes that handle errors, classes that can load test data (CSVs, XML, etc) to be pumped into your test cases.

Basically, I'm starting to write a how-to article (book) on how to create an automation framework, which is way outside the scope of your question and is getting into the realm of opinion, etc., which is not really compatible with the intentions stackoverflow (A problem/answer site vs. A forum to here have discussion, share opinions, etc.)

I would recommend you invest in this area first. When you run into a question about the PO gem specifically, post a new question, we'll be here! :-)