Search code examples
arraysrubyrspecwatir-webdriver

Rspec and Watir; use array param as for an element index


Running GUI tests using Rspec. Is it possible for me to pass an array of values and have them used as the index position for an element?

Something like:

def pin_specific_idxs(*idx)
    pins = foo.divs(:class => 'some-element', :index => idx).div(:class, 'another-element').button(:class, 'thingy-i-want-to-click')
    pins.each do |pin|
        pin.click
    end
end

So in testing, I would call pin_specific_idxs(0,2,3)

Is that doable or do I have to explicitly call individual index values every time?


Solution

  • You need to do 2 things:

    • Iterate over the passed in indices rather than passing the whole Array as a parameter to :index
    • Locate the specific div rather than divs using the :index (ie element collections do not support the :index locator)

    This would look like:

    def pin_specific_idxs(*idxs)
      idxs.each do |idx|
        foo.div(:class => 'some-element', :index => idx)
           .div(:class, 'another-element')
           .button(:class, 'thingy-i-want-to-click')
           .click
      end
    end