3
votes

I have a link like this:

< a href= "mailto:<%= @email %>?subject=Answer to complaint&body=<%= @salutation %>,">Answer< /a>

How can I test it using Cucumber/Capybara? I mean something more than just

And I should see "Answer"

4
What would be sufficient, in your opinion? That the link's href contained a mailto to the correct email?Jesse Wolgamott
Actually I am not really sure what should I test. I just think that 'And I should see' is not enough. What do you think?Piotr Brudny
In my opinion, testing that the href contained "mailto:#{email}" is good.Jesse Wolgamott

4 Answers

2
votes

It sounds like you're concerned that the @email and @salutation values in the mailto: links are correct.

You can do something like this

page.should have_xpath("//a[contains(@href,email)]"))
0
votes

I ended up with Cucumber step:

Then /^I should have a mailto link with:$/ do |table| 
  mailto_link = '//a[@href="mailto:' + table.rows_hash['recipient'] + '?subject=' + table.rows_hash['subject'] + '&body=' + table.rows_hash['body'] + ' "]'
  page.should have_xpath(mailto_link)
end
0
votes

An addition to Piotr Brudny with URL encode and contains so order of parameters won't matter (I removed the body from there, but you can probably figure it out if you need it.

Then(/^I should have a mailto link with:$/) do |table|
  mailto_link = ("//a[contains(@href, \"mailto:#{table.rows_hash['recipient']}\")"\
  " and contains(@href, \""+ {
    subject: table.rows_hash['subject']
  }.to_query + '")]').gsub('+', '%20')
  page.should(have_xpath(mailto_link))
end
0
votes

Similar to the accepted answer, but slightly updated example. You can put it like this:

expect(page).to have_xpath("//a[contains(@href,'mailto:[email protected]')]")