如何在使用Cypress的Mailslurp确认电子邮件中提取链接?

huangapple go评论116阅读模式
英文:

How to extract a link from a confirmation email in Mailslurp using cypress?

问题

但是常量 'const link' 返回了一个空值。

我原本期望收到链接以继续注册流程。

英文:

I was trying to extract a link to proceed with user registration using Cypress and Mailslurp. For that, I wrote the following code:

let inboxId;
let emailAddress;

describe('sign up', () => {
    beforeEach(() => {
        
        cy.viewport(1920, 1080);
    })

    it('receive sign up link', () => {
        cy.visit('/signup');
        cy.createInbox().then(inbox => {
            // verify a new inbox was created
            assert.isDefined(inbox)
      
            // save the inboxId for later checking the emails
            inboxId = inbox.id
            emailAddress = inbox.emailAddress;

            cy.get('#email-input').type(emailAddress);
            cy.get('.bg-gray-100 > .p-button').click();
        })
    })

    it('extract the confirmation link and extract the code', () => {
        cy.waitForLatestEmail(inboxId).then(email => {
            const emailHTML = email.body;
            const regexLink = /<a href="([^"]+)">/;
            const match = emailHTML.match(regexLink);
            const link = match[0];
            cy.visit(link);
        });
    });

})

But the constant 'const link' returns a null value

I was expecting to receive the link to continue the register link

答案1

得分: 2

你可以使用类似于jsdom的库来解析HTML,以便正确获取所有链接,如下所示:

const dom = new JSDOM(email.body);
const link = dom.window.document.querySelector('a');

然而,你也可以通过正则表达式或DOM库来避免自己查找链接。我在我的公司使用Mailosaur来提取电子邮件中的链接,这样你就不必担心这个问题。

从他们的文档中这里

it('获取密码重置邮件', () => {
  cy.mailosaurGetMessage(serverId, {
    sentTo: testEmail
  }).then(email => {
    expect(email.subject).to.equal('重置密码');
    passwordResetLink = email.text.links[0].href;
  })
})

因此,你的代码将如下所示:

it('提取确认链接并提取代码', () => {
  cy.mailosaurGetMessage(serverId, {
    sentTo: emailAddress
  }).then(email => {
    const link = email.text.links[0].href;
    cy.visit(link);
  });
});

其次,你也不需要创建收件箱或电子邮件地址并调用他们的API,你可以简单地拼接字符串来创建地址。希望这可以帮助你。

英文:

You could use a library like jsdom and parse the html so you correctly get back all links like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const dom = new JSDOM(email.body);
const link = dom.window.document.querySelector(&#39;a&#39;);

<!-- end snippet -->

However, you could also avoid having to find links yourself with regex or a dom library.
I use Mailosaur at my company for this, which extracts links from email for you so you don't have to worry about this.

From their documentation here:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 it(&#39;Gets a Password Reset email&#39;, () =&gt; {
      cy.mailosaurGetMessage(serverId, {
        sentTo: testEmail
      }).then(email =&gt; {
        expect(email.subject).to.equal(&#39;Reset your password&#39;);
        passwordResetLink = email.text.links[0].href;
      })
    })

<!-- end snippet -->

So your code would look like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 it(&#39;extract the confirmation link and extract the code&#39;, () =&gt; {
            cy.mailosaurGetMessage(serverId, {
            sentTo: emailAddress
          }).then(email =&gt; {
                const link = email.text.links[0].href;
                cy.visit(link);
            });
        });

<!-- end snippet -->

Secondly, you also don't need to create an inbox or an email address and call their API, you can just make up addresses as simple as concatenating a string.
I hope that helps you.

答案2

得分: 0

考虑到错误发生的位置,这很可能是由于正则表达式不匹配造成的。你的正则表达式匹配例如&lt;a href=&quot;www.example.com&quot;&gt;,但不匹配&lt;a href=&quot;www.example.com&quot; target=&quot;_blank&quot;&gt;,或者链接具有任何id、类或其他属性。

尝试将你的正则表达式更改为const regexLink = /&lt;a href=&quot;([^&quot;]+)&quot;.*&gt;/;。你还需要将你的链接更改为const link = match[1];,因为match[0]是整个匹配的字符串,而不是第一个捕获组。

英文:

Considering the place where the error occurs this is most likely caused by the regex not matching. Your regex matches for example &lt;a href=&quot;www.example.com&quot;&gt; but not &lt;a href=&quot;www.example.com&quot; target=&quot;_blank&quot;&gt; or if the link has any id, classes or other properties.

Try changing your regex to const regexLink = /&lt;a href=&quot;([^&quot;]+)&quot;.*&gt;/;. You'll also need to change your link to const link = match[1]; since match[0] is the entire string matched, not the first capturing group.

huangapple
  • 本文由 发表于 2023年7月17日 20:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定