Module: Capybara::Select2

Defined in:
lib/decidim/dev/test/rspec_support/capybara_select2.rb

Instance Method Summary collapse

Instance Method Details

#select2(value, xpath:, search:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/decidim/dev/test/rspec_support/capybara_select2.rb', line 7

def select2(value, xpath:, search:)
  expect(page).to have_xpath(xpath)
  select2_container = find(:xpath, xpath)

  expect(select2_container).to have_selector(".select2-selection")
  select2_container.find(".select2-selection").click

  if search
    body = find(:xpath, "//body")
    expect(body).to have_selector(".select2-search input.select2-search__field")
    body.find(".select2-search input.select2-search__field").set(value)

    page.execute_script(%|$("input.select2-search__field:visible").keyup();|)
    drop_container = ".select2-results"
  else
    drop_container = ".select2-dropdown"
  end

  expect(page).to have_no_content("Searching...")

  body = find(:xpath, "//body")
  expect(body).to have_selector("#{drop_container} li.select2-results__option", text: value)
  body.find("#{drop_container} li.select2-results__option", text: value).click
end