Search code examples
rubyrspecsite-prism

Defining elements and locators with interpolation in site-prism?


do you happen to know if there is a way to define both elements and locators using string or symbol interpolation while using the site-prism gem?

I'm trying to do something like this:

0.upto(@adults) do  {
element :"adult#{index}", "#passenger-first-name-#{index}"
element :"adult#{index}", "#passenger-last-name-#{index}"
index+=1
}

But I'm getting the following syntax error at executing:

syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' (SyntaxError) element :"adult#{index}" , "#passenger-first-name-#{index}"

I was reading here that symbols DO allow interpolation: http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/

Maybe I am missing something? Thanks a lot!


Solution

  • I haven't tried symbol interpolation, but the string interpolation should work. But, you many not need to do that... usually, this sort of thing can be solved by more closely modelling your website using sections. Instead of dynamically creating elements, you could use the elements or sections methods that will create arrays of elements. Here's what I'd do based the example you've given:

    I'm guessing from your example that your website lists passengers... if each passenger is displayed in a separate div then you could consider something like:

    #section that models a single passenger
    class PassengerDetails < SitePrism::Section
      element :first_name, "input[id^=passenger-first-name]"
      element :last_name, "input[^=passenger-last-name]"
    end
    
    #page that contains a list of passengers
    class FlightManifest < SitePrism::Page
      sections :passengers, PassengerDetails, ".passenger-details"
      #... where ".passenger-details" is a style on the divs that contains a passenger's details
    end
    

    Given the above, you could refer to the list of passengers as an array:

    Then /^the flight manifest contains the correct first and last names for each passenger$/ do
      @flight_manifest.passengers.each_with_index do |passenger, i|
        passenger.first_name.text.should == @expected_passengers[i].first_name
        passenger.last_name.text.should == @expected_passengers[i].last_name
      end
    end
    

    I don't know if that helps, or if it's possible with your website, but it might point you in the right direction...