How to visit a link inside an email using capybara

I am new to cucumber with kappybara. I have a testing application whose flow is: "After submitting the form, an email will be sent to the user who contains a link to another application. To access the application, we have to open the mail and click on the link, which will be redirected to the application. ' I do not have access to the mail id. Is there a way to retrieve this link and continue flowing? Please give a few possible ways to do this.

Best regards, Abhisek Das

+3


source to share


1 answer


In your test, use whatever you need to trigger your application to send email. After sending the email, use a regex to find the URL from the link in the body of the email (note that this will only work for an email containing one link) and then follow the path from that URL with Capybara. to continue your test:

path_regex = /(?:"https?\:\/\/.*?)(\/.*?)(?:")/    

email = ActionMailer::Base.deliveries.last
path = email.body.match(path_regex)[1]
visit(path)

      


Explanation of regular expression

The regular expression (regular expression) itself is demarcated with forward slashes, and this regular expression, in particular, consists of three groups, each of which is separated by pairs of parentheses. The first and third groups start with ?:

to indicate that they are not capturing groups, and the second is a capturing group (no ?:

). I will explain the meaning of this difference below.

The first group,, (?:"https?\:\/\/.*?)

is:

  • non-capturing group ?:

  • which matches one double quote, "

    • we are matching the quote as we expect the url to be in the href="..."

      link tag attribute
  • followed by the line http

  • followed by a lowercase s, s?

    • question mark makes the previous match, in this case s

      optional
  • followed by a colon and two forward slashes, \:\/\/

    • note the backslashes which are used to remove characters that otherwise have special meaning in the regex
  • and then a wildcard .*?

    that will match any character any number of times until the next regex match is reached
    • period or wildcard matches any character
    • the asterisk *

      repeats the previous match up to an unlimited number of times, depending on the subsequent match that follows
    • the question mark makes this a lazy match, which means that the wildcard will match as few characters as possible, but it will satisfy the next match in the regex


The second group (\/.*?)

is the capture group, which:

  • matches a single forward slash, \/

    • this will match the first forward slash after the host portion of the url (like a http://www.example.com/

      forward slash at the end ), since the forward slashes in http://

      already match the first group
  • followed by another lazy pattern, .*?

Third group (?:")

:

  • another non-exciting group, ?:

  • which matches one double quote, "

So our second group will match the portion of the URL that starts with a forward slash after the host and goes to the double quote at the end of ours href="..."

, but not including it.

When we call a method match

using our regular expression, it returns an instance MatchData

that behaves like an array. The at-indexed item 0

is a string containing the entire matched string (of all the groups in the regex), while the items in subsequent indices only contain the portions of the string that match the regex capture groups (just our second group, in this case). So, to get the appropriate match for our second group - which is the path we want to visit using Capybara - we'll grab the item in the index 1

.

+5


source







All Articles