Determination of the connection with the cucumber / Capybara

I want to check if a link exists on a specific href on the page. I currently see "/ some-link-here", but this seems to be failing. How can I make sure that the link exists without having to click + should I be on the "/ some-link-here" page?

+3
source share
3 answers

You need to add a custom step.

Then /^"([^\"]*)" should link to "([^\"]*)"(?: within "([^\"]*)")$/ do |link_text, 
page_name, container|
  with_scope(container) do
    URI.parse(page.find_link(link_text)['href']).path.should == path_to(page_name)
  end
end

You can use a step like Then "User Login" should link to "the user_login page", user_login is the name of your route

+5
source

I used jatin's answer, but have a separate step:

When /^(.*) within ([^:]+)$/ do |step, parent|
  with_scope(parent) { When step }
end

Then /^"([^\"]*)" should link to "([^\"]*)"$/ do |link_text, page_name|
  URI.parse(page.find_link(link_text)['href']).path.should == path_to(page_name)
end

Then I have this in my test:

step '"my foods" should link to "food_histories" within ".tabs"'

And this is on my roads:

# note: lots not shown
def path_to(page_name)
  case page_name
  when /^food_histories$/
    food_histories_path
  end
end
0
source

This is what I did myself, is pretty simple, but it means that you are hard-coded your URL, which, frankly, is not perfect, as it makes your test very fragile. Especially if you use a third-party URL!

But if you use the URL that you manage and enjoy supporting this test, look for it.

Then /^the link is "(.*?)"$/ do |arg1|
   page.should have_xpath("//a[@href='" + arg1 + "'][@target='_blank']")
end
0
source

Source: https://habr.com/ru/post/1779809/


All Articles