CY请求重试

发布于 2025-02-08 17:42:42 字数 572 浏览 2 评论 0原文

我正在尝试创建一条电子邮件服务,CY请求将获取并返回电子邮件的正文,但是从逻辑上讲,可能没有立即到达电子邮件,所以我认为正确的方法是每3秒获取一次,并且如果没有电子邮件尝试再次获取,因此基本上一遍又一遍地重试同样的操作,以便说每3秒钟2分钟。柏树在API中是否有东西?

it('Should be able to reset password', () => {
         let emailContent: string = '';
         cy.request(
          'GET',
          `https://api.testmail.app/api/json?apikey=${TestMailService.APIKEY}&namespace=${TestMailService.NAMESPACE}&tag=test&livequery=true`,
        ).then((response) => {
            emailContent =  response.body.emails[0].text;
        });
      })

h

I am attempting creating an email service where cy request will fetch and return body of an email, but logically there could be a case where email did not arrived right away so i assume the correct way is to fetch every 3 seconds and if there is no email try to fetch again so basically retrying same operation over and over for lets say 2 minutes every 3 seconds. Is there something that cypress already have in the api ?

it('Should be able to reset password', () => {
         let emailContent: string = '';
         cy.request(
          'GET',
          `https://api.testmail.app/api/json?apikey=${TestMailService.APIKEY}&namespace=${TestMailService.NAMESPACE}&tag=test&livequery=true`,
        ).then((response) => {
            emailContent =  response.body.emails[0].text;
        });
      })

H

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2025-02-15 17:42:43

从文档请求投票

// a regular ol' function folks
function req () {
  cy
    .request(...)
    .then((resp) => {
      // if we got what we wanted

      if (resp.status === 200 && resp.body.ok === true)
        // break out of the recursive loop
        return

      // else recurse
      req()
    })
}

这是一个递归功能,所以我会我在无限呼叫的情况下添加警卫(没有显示电子邮件)。

等待3秒只是cy.wait(3000)

// a regular ol' function folks
function req (attempts = 0) {

  if (attempts === 100) throw 'Too many attempts'

  cy.request(...)
    .then((resp) => {
      // if we got what we wanted

      if (resp.status === 200 && resp.body.ok === true)
        // break out of the recursive loop
        return

      // else recurse
      cy.wait(3000)
      req(++attempts)
    })
}

传递特定电子邮件主题

// TestMailService class

static getLatestEmail(subject: string, attempts = 0): string {

  const NAMESPACE: string = '...';
  const APIKEY: string = '...';

  if (attempts === 40) throw 'No email reached within 2 minutes ';

  return cy.request('GET',         // return the async request
    `api.testmail.app/api/…${APIKEY}&namespace=${NAMESPACE}&tag=dev`)
    .then((response) => {

      if (response.body.emails.length != 0) {
        response.body.emails.forEach((email: any) => {
          if (email.subject === subject) {
            return email.text;       // this modifies what request returns
          }
        });
      }
      if (response.body.emails.length === 0) {
        cy.wait(3000);
        // another return here passes inner results outwards
        return TestMailService.getLatestEmail(subject, ++attempts);   
      }
    })
}

...

TestMailService.getLatestEmail('Registration').then(emailBody => {
  ...
})

From the docs Request Polling

// a regular ol' function folks
function req () {
  cy
    .request(...)
    .then((resp) => {
      // if we got what we wanted

      if (resp.status === 200 && resp.body.ok === true)
        // break out of the recursive loop
        return

      // else recurse
      req()
    })
}

It's a recursive function , so I'd add a guard in case of infinite calling (no email shows up).

Waiting 3 seconds is just a cy.wait(3000)

// a regular ol' function folks
function req (attempts = 0) {

  if (attempts === 100) throw 'Too many attempts'

  cy.request(...)
    .then((resp) => {
      // if we got what we wanted

      if (resp.status === 200 && resp.body.ok === true)
        // break out of the recursive loop
        return

      // else recurse
      cy.wait(3000)
      req(++attempts)
    })
}

Passing a specific email subject

// TestMailService class

static getLatestEmail(subject: string, attempts = 0): string {

  const NAMESPACE: string = '...';
  const APIKEY: string = '...';

  if (attempts === 40) throw 'No email reached within 2 minutes ';

  return cy.request('GET',         // return the async request
    `api.testmail.app/api/…${APIKEY}&namespace=${NAMESPACE}&tag=dev`)
    .then((response) => {

      if (response.body.emails.length != 0) {
        response.body.emails.forEach((email: any) => {
          if (email.subject === subject) {
            return email.text;       // this modifies what request returns
          }
        });
      }
      if (response.body.emails.length === 0) {
        cy.wait(3000);
        // another return here passes inner results outwards
        return TestMailService.getLatestEmail(subject, ++attempts);   
      }
    })
}

...

TestMailService.getLatestEmail('Registration').then(emailBody => {
  ...
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文