Continue asking about monkey patches I use :)

While building object-oriented page objects (sic), I've came to the idea of 
adding `Watir::Element#try` which will try to run code against element, but if 
it's not present (`UnknownObjectException` is raised), it will return `nil`. 

Just like `#try` in ActiveSupport, but not catching `NoMethodError`.

I needed this because I had the same page accessible to two user roles. 
Depending on the role, some elements could be present and some not. For example:

```ruby
def user
  {
    :name   => user_div.div(:class => 'name').text,   # shown to all roles
    :status => user_div.div(:class => 'status').text, # shown to admin only
  }
end
```

Apparently, this code will raise `UnknownObjectException` when try to retrieve 
`:status` if currently logged in user role is not admin.

So I made it like:

 ```ruby
def user
  {
    :name   => user_div.div(:class => 'name').text,          # shown to all 
roles
    :status => user_div.div(:class => 'status').try(:text), # shown to admin 
only
  }
end
```

Now, `:status` will fail the test only on verification step (if needed).

I understand that this may be a problem for other projects with different 
codebase, so if you think it's a bad idea, I'll just close the issue.

---
Reply to this email directly or view it on GitHub:
https://github.com/watir/watir-webdriver/issues/184
_______________________________________________
Wtr-development mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-development

Reply via email to