ruby-on-railsrspeccapybarasite-prism

Using capybara/rspec find elements/sections by selector with similar id


I want to create Page Objects using SitePrsim. Selector of element on page is in form 'service_id'. How to find all elements/sections in a single page where id is different but selector contains key word 'service_' ?

I have tried:

class ServicesSection < SitePrism::Section
end

class APIs < SitePrism::Page
  element :create_service, '#content > a'
  sections :services, ServicesSection, "#service"

  def create_new
    create_service.click
  end
end

Elements I am looking for looks like this:

<section class="service-widget u-legacy-cookie is-closed"    id="service_2555417736137">

All of these elements are children from:

<div class="self_clear" id="content">

Solution

  • You can use the CSS "begins with" attribute selector to do what you're asking - https://www.w3schools.com/css/css_attribute_selectors.asp

    sections :services, ServicesSection, 'section[id^="service_"]'
    

    although, from your example, just using a class name may work just as well

    sections :services, ServicesSection, 'section.service-widget'