Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I locate a custom tag like <g>?

I'm having to deal with some graphic items in a page that have <g> tags. I can do something like this to find them by dropping into selenium webdriver:

browser.wd.find_elements( :tag_name => "g" )

What would be the equivalent for watir webdriver?

Also, how would I convert those selenium elements into watir elements?

Could I add support for a <g> tag to watir locally somehow?

like image 899
ibodog Avatar asked May 23 '12 03:05

ibodog


2 Answers

Solution 1 - Watir-Webdriver Equivalent:

The equivalent to what you were doing in selenium-webdriver is:

browser.elements( :tag_name => "g" )

So you can do something like this to output the text of each element:

browser.elements( :tag_name => "g" ).each do |x|
  puts g.text
end

Solution 2 - Add Support for G Element:

After requiring watir-webdriver, add the following code:

module Watir
    module Container
        def g(*args)
            G.new(self, extract_selector(args).merge(:tag_name => "g"))
        end

        def gs(*args)
            GCollection.new(self, extract_selector(args).merge(:tag_name => "g"))
        end         
    end

    class G < Element
    end

    class GCollection < ElementCollection
        def element_class
            G
        end
    end
end

Then you can treat 'g' like any other element. For example:

puts browser.g(:index, 0).text
browser.gs.each{ |x| puts x.text }

The G and GCollection classes will support all the standard element methods. You can add additional methods to the class if there are things specific to that element.

Update - Example of Adding Custom Methods:

To get the cursor style, you would add a method to the G class like this:

class G < Element
    def cursor_style()
        assert_exists
        return @element.style("cursor")
    end
end

This will then allow you to get the cursor property like this:

puts browser.g(:index, 0).cursor_style
#=> move

Any custom methods that interact with the element need to start with assert_exists. Then, within the method, you can work with the element using the @element variable.

Note that because the G element inherits from the Element class, you could also have used the built in style method:

puts browser.g(:index, 0).style("cursor")
#=> move
like image 151
Justin Ko Avatar answered Oct 06 '22 12:10

Justin Ko


There is no support for non-standard HTML tags in watir or watir-webdriver. In part because the list of possible tag names we'd have to support is endless.

You could monkeypatch in your own custom tags if you want to. In the long run, if you are going to have to deal with those custom tags a lot, that might be your best solution in terms of having stuff that acts just like other standard HTML elements supported by the Watir api.

You can use a CSS selector on the tag name, but that only works if you stick to element objects, as described in Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css. Which kinda makes things a little less readable and/or useful if you ask me, but might be a quick and easy solution worth the slight cost in that regard.

Or you can use .driver or .wd methods to access webdriver functionality when you need it for something not supported by watir. (but that's also not quite as readable)

like image 25
Chuck van der Linden Avatar answered Oct 06 '22 12:10

Chuck van der Linden