首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将电子邮件服务与Cypress 10集成

如何将电子邮件服务与Cypress 10集成
EN

Stack Overflow用户
提问于 2022-09-07 14:24:29
回答 1查看 78关注 0票数 1

我试图整合电子邮件服务与柏树电子邮件测试。我遵循了这个示例项目https://github.com/bahmutov/cypress-ethereal-email-example,这个项目是为Cypress版本<10编写的,我试图使它在Cypress 10.3.1中工作。遵循以下步骤:

directory

  • Tried

  • 在柏树中创建了电子邮件-Account.js/支持复制柏树/插件/index.js在cypress.config.js中

cypress.config.js含量

代码语言:javascript
复制
const { defineConfig } = require('cypress')
const fs = require('fs');
const { isFileExist } = require('cy-verify-downloads');
const xlsx = require('node-xlsx').default;
const path = require('path');
const exec = require('child_process');
const makeEmailAccount = require('./cypress/support/email-account')
module.exports = defineConfig({
e2e: {
 setupNodeEvents (on, config) {
 const emailAccount =  makeEmailAccount()
 on('task', {
   getUserEmail() {
     const emailAccount =  makeEmailAccount()
     console.log(emailAccount)
     return new Promise((resolve) => {
     // tasks should not resolve with undefined
     console.log(emailAccount)
     return resolve(emailAccount.email)
   })
 },

 getLastEmail() {
   return emailAccount.getLastEmail()
 },
})
})

在尝试测试电子邮件时,我遇到了以下错误:

代码语言:javascript
复制
task getUserEmail
CypressError
cy.task('getUserEmail') failed with the following error:

The task 'getUserEmail' returned undefined. You must return a value, null, or a promise that resolves to a value or null to indicate that the task was handled.

The task handler was:

getUserEmail() {
  return new Promise((resolve) => {
  // tasks should not resolve with undefined
  return resolve(emailAccount.email)
  })
}

Fix this in your setupNodeEvents method here:
/Users/ashoknegi/Sites/renesas-d9/cypress.config.js

有人能帮忙解决这个问题吗?我认为问题在于配置文件中的异步函数调用。我检查了异步任务,发现了这个链接,并试图以同样的方式实现它。https://docs.cypress.io/api/commands/task#Return-a-Promise-from-an-asynchronous-task有没有人将电子邮件服务集成到Cypress 10上?任何帮助都很感激。

EN

回答 1

Stack Overflow用户

发布于 2022-09-07 21:22:58

要将Gleb的v9 /plugins/index.js转换为v10 cypress.config.js,您需要setupNodeEvents()是异步的,并等待makeEmeialAccount()的结果。

香柏( Cypress v9 )

代码语言:javascript
复制
module.exports = async (on) => {
  const emailAccount = await makeEmailAccount()

  on('task', {
    getUserEmail() {
      return emailAccount.email
    },

    getLastEmail() {
      return emailAccount.getLastEmail()
    },
  })
}

香柏( Cypress v10 )

代码语言:javascript
复制
e2e: {
 setupNodeEvents:  async (on, config) => {
   const emailAccount = await makeEmailAccount()

   on('task', {
     getUserEmail() {
       return emailAccount.email
     },

     getLastEmail() {
       return emailAccount.getLastEmail()
     },
   })
 },

从技术上讲,使用Promise.resolve()是一种合法的方法,但由于您没有等待,所以需要使用.then(emailAccount => resolve(emailAccount))模式。

代码语言:javascript
复制
e2e: {
  setupNodeEvents (on, config) {

    on('task', {
      getUserEmail() {
        return new Promise((resolve) => {
          makeEmailAccount().then(emailAccount => {
            resolve(emailAccount.email)
          })
        })
      }
    }
  }
},

然而Promise.resolve()并不像async/await方法那么有用,因为在测试中添加getLastEmail()或调用getUserEmail()任务两次实际上是在每次打电话时发出不同的电子邮件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73637181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档