1

I'm pretty new to Watir.

I was looking for a way to detect the type of object received by a subroutine, in order to apply the correct method. The final result should be something like:

sub clickOnElement (elementId, elementRef)
element= browser.(elementId.to_sym, elementRef)
case TypeOfElement (element)
   :link            element.click
   :radio           element.set
   :checkbox        element.set
   :list            ....
end
end

My question is about the actual implementation of TypeOfElement().

Any suggestion or pointer is appreciated: thank you in advance

Sergio

sbos61
  • 554
  • 2
  • 9
  • Just out of curiosity, why do you need to do this? Usually you know what type of element you are working with so can directly call the applicable methods. – Justin Ko Jun 21 '12 at 20:14

1 Answers1

3

You can do the case statement based on the class of the element variable (after converting it to its subtype). This way you do not have to implement your own TypeOfElement method.

Something like:

e = browser.element
case e.to_subtype
    when Watir::CheckBox
        e.set
    when Watir::Anchor #Link
        e.click
    else
        raise( e.class.to_s + ' not handled' )
end

Note:

  • browser.element returns the first element, which will be the HTML tag. Therefore, in the above case statement will raise an exception. I assume e will be something more specific.
  • At the start of the case statement it is just 'e.to_subtype' instead of 'e.to_subtype.class' (as explained in How to catch Errno::ECONNRESET class in "case when"?).
Community
  • 1
  • 1
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Thank you Justin, that would be ideal. Unfortunately, when I run my code like this: e= $browser.element case e when Watir::Link .... when Watir::Checkbox .... – sbos61 Jun 15 '12 at 21:11
  • Try the updated code. The original code was incorrectly using Watir instead of Watir-Webdriver. – Justin Ko Jun 16 '12 at 02:38
  • Yes it works. It took me some time to verify it in my environment. Thank you!! – sbos61 Jun 19 '12 at 18:00
  • I have two observations: 1- you have to reselect the object with correct class to apply the right action. 2- It DOES NOT work with buttons. It throws me an exception, saying `.to_subtype` is not applicable. I'm still investigating on this. Suggestion are welcome. – sbos61 Jun 21 '12 at 19:30
  • For (1) I guess it will depend what methods you are performing on 'e'. You could move the `.to_subtype` to the first line rather than using it in the case statement. For (2) it seems to work fine with buttons - the subtype should be Watir::Button. – Justin Ko Jun 21 '12 at 20:00
  • Thank you Justin. I'm using the 'bit.ly/watir-webdriver-demo'to do the test. The behaviour with buttons is rather odd. For one, I can find the button with name using (:name=>"submit") but if I look for an element with same criteria, I got an exception. I will try to go around that, but Watir-WD does not loook very reliable. – sbos61 Jun 22 '12 at 20:08
  • I would not say that Watir-Webdriver is unreliable. Based on the comments in the code, :name was deliberately excluded as a specifier for elements. If you want, you can change the code in lib/watir-webdriver/locators/element_locator.rb to include :name in the WD_FINDERS array. For the reasoning that it is excluded you would have to ask one of the watir developers. – Justin Ko Jun 22 '12 at 20:41
  • Well, that's not the kind of thing I expect: if a selector can be applied to a more specific object, why not to do to a more general one?? – sbos61 Jun 22 '12 at 21:30
  • I do not believe that the name attribute is supposed to be applicable to all element types. It even appears to be depreciated in [XHTML](http://www.w3.org/TR/xhtml1/#h-4.10). I still think you should re-consider what you are trying to do. I have only had rare occasions where the use of the `browser.element` made sense. – Justin Ko Jun 22 '12 at 22:51