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

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

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:

  1. let inboxId;
  2. let emailAddress;
  3. describe('sign up', () => {
  4. beforeEach(() => {
  5. cy.viewport(1920, 1080);
  6. })
  7. it('receive sign up link', () => {
  8. cy.visit('/signup');
  9. cy.createInbox().then(inbox => {
  10. // verify a new inbox was created
  11. assert.isDefined(inbox)
  12. // save the inboxId for later checking the emails
  13. inboxId = inbox.id
  14. emailAddress = inbox.emailAddress;
  15. cy.get('#email-input').type(emailAddress);
  16. cy.get('.bg-gray-100 > .p-button').click();
  17. })
  18. })
  19. it('extract the confirmation link and extract the code', () => {
  20. cy.waitForLatestEmail(inboxId).then(email => {
  21. const emailHTML = email.body;
  22. const regexLink = /<a href="([^"]+)">/;
  23. const match = emailHTML.match(regexLink);
  24. const link = match[0];
  25. cy.visit(link);
  26. });
  27. });
  28. })

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,以便正确获取所有链接,如下所示:

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

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

从他们的文档中这里

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

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

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

其次,你也不需要创建收件箱或电子邮件地址并调用他们的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 -->

  1. const dom = new JSDOM(email.body);
  2. 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 -->

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

<!-- end snippet -->

So your code would look like this:

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

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

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

<!-- 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:

确定