02 November 2012

Everything is an object

One thing I really love about the Ruby/Cucumber framework, is that everything is an object. I'm allowed to do something like this:

divlength = @browser.div(:id=>"divValue").wait_until_present(5).length

I can chain actions/methods to each other real easily... again like
@browser.div(:id=>"search-results-container").wait_until_present(5).click

This is really cool when trying to click a "a" tag in a div like the html might be:
<div id="div1" class="containerdiv" name="links">
   <a href="http://www.someplace.com">click here</a>
</div>

Since the a tag doesn't have a id or class, you can still reference it by chaining it like so (using Watir syntax):
browser.div(:id=>"div1").a.click

It's logical and makes sense. It's also pretty easy.

It's letting me treat the div(:id=>"div1") as an object that I'm applying a method handler of "a" to, which is saying "hey look for the a tag" and then that itself is treated as an object and lets me apply the "click" method to it saying "ok now click that."  You could throw a wait_until_present method in front of the click, to give some time for the tag to load on the page, etc. 

But it gives a great example of everything being an object.

No comments:

Post a Comment