Alastair Montgomery
Alastair Montgomery

Reputation: 1856

Watir, looking for a more elegant solution for HTML element checking

I'm looking for a more elegant solution to check that a range of HTML elements are visible in the browser.

I had the idea of creating a CSV file with element type and IDs, reading it into an array and using that to check the elements are present in the browser.

So the CSV file/array would look something like this,

"select","srch-op-select"
"text_field","srch-filter"
"button","srch-button"
"image","srch-showhide-icon"
"div","srch-showhide"

I then thought I could use case statement to do the checking, something like this,

        myElements.each do |row|
        type = row[0]
        id = row[1]
        case type
          when "button"   : assert(browser.button(:id,id).exists?)
          when "checkbox" : assert(browser.checkbox(:id,id).exists?)
          when "div"      : assert(browser.div(:id,id).exists?)
          when "image"    : assert(browser.image(:id,id).exists?)
          when "label"    : assert(browser.label(:id,id).exists?)
          when "link"     : assert(browser.link(:id,id).exists?)
          when "radio"    : assert(browser.radio(:id,id).exists?)
          when "select"   : assert(browser.select_list(:id,id).exists?)
          when "span"     : assert(browser.span(:id,id).exists?)
          when "table"    : assert(browser.table(:id,id).exists?)
          else $log.debug "---Unsupported element type "+type
        end
    end

Obviously this case statement would be come large and unwieldy if you wanted to cover all supported element types or factor in the different methods of selecting a HTML element.

Can anyone suggest a more elegant and flexible solution?

Upvotes: 1

Views: 1240

Answers (2)

VP.
VP.

Reputation: 5141

Akephalos

Thankfully, we then found Akephalos. Akephalos provides a Capybara driver that allows you to run your cucumber integration tests in the headless browser HtmlUnit. HtmlUnit is a “GUI-Less browser for Java programs”. It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc… just like you do in your “normal” browser. With our fork of Akephalos to resolve a couple of issues that we ran into along the way, we were up and running with very reliable, headless browser tests.

HtmlUnit is written in Java, and Akephalos uses jruby-jars to start up and interact with the HtmlUnit browser. It has fairly good JavaScript support (it was able to deal with everything we were able to throw at it, including jQuery 1.4.2 and 1.4.3, jQuery Mobile, and jQuery live).

edit: extracted from http://robots.thoughtbot.com/post/1658763359/thoughtbot-and-the-holy-grail

Upvotes: 1

Bret Pettichord
Bret Pettichord

Reputation: 455

Replace your case statement with this:

assert(browser.send(type.to_sym, :id, id).exists?)

Upvotes: 2

Related Questions