bigpotato
bigpotato

Reputation: 27507

Rails + Capybara: How do I loop through all links with certain text?

I have a bunch of anchor tags with text Delete Image. How do I loop through all of them and click them one by one? They all use AJAX. This doesn't work:

When(/^I delete all the section images$/) do
  page.all(:link, "Delete Image").each do |link|
    click_link(link)
    page.driver.browser.switch_to.alert.accept
  end
  sleep 1
end

Upvotes: 0

Views: 669

Answers (1)

bigpotato
bigpotato

Reputation: 27507

When(/^I delete all the section images$/) do
  all(:link, "Delete Image").each do |link|
    link.click
    page.driver.browser.switch_to.alert.accept
  end
  sleep 1
end

the link parameter passed into the loop is an instance of a capybara class that can respond to #click. click_link("text") only works on matching the text.

Upvotes: 3

Related Questions