为了账号安全,请及时绑定邮箱和手机立即绑定

CypressIO 使用 cy.wrap() 从 cy.task() 返回字符串值给出错误

CypressIO 使用 cy.wrap() 从 cy.task() 返回字符串值给出错误

一只斗牛犬 2022-12-09 18:59:53
在赛普拉斯/plugins/index.js我有代码来查询 oracleDBmodule.exports = (on, config) => {  on('task', {    'registration': async (email) => {      const oracledb = require('oracledb');      oracledb.initOracleClient({libDir: './oracleClient'});        let result;        let connection;        connection = await oracledb.getConnection(  {            user          : process.env.ORACLEDB_USER,            password      : process.env.ORACLEDB_PASSWORD,            connectString : "localhost/STORE"            });        result = await connection.execute(            "select ..."            );                console.log(result.rows)        var extractedUrlText = JSON.stringify((await result).rows).extract('/VerifiedRegistrationFormView','\"');        console.log('Extracted URL: \r\n' + extractedUrlText);      return cy.wrap(extractedUrlText);    }  });}这会在 Node 终端中返回正确提取的 URL。但是在我的赛普拉斯测试中,当我尝试使用那个 extractedUrlText 字符串值时,我得到了错误cy is not defined:it('Register a new user', () => { cy.task('registration', emailAddress, { timeout: 10000 }).then(value => cy.log(value)) // cy is not defined    })我使用类似的方法来使用返回值并且support/commands.js Cypress.Commands.add()它在那里工作,但不是来自 cy.task() 使用 cy.wrap()
查看完整描述

1 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

我的工作解决方案:


/plugins/index.js从上面的代码扩展:


 var extractedUrlText = JSON.stringify((await result).rows).extract('/VerifiedRegistrationFormView','\"');

    console.log('Extracted NODE URL: \r\n' + extractedUrlText);


  return extractedUrlText

}

在规范文件中:


let extractedUrl;


        cy.task('registration', emailAddress).then(value => {

            extractedUrl = value;

            cy.log('Extracted CY URL: ' + extractedUrl)

        })

结果:

//img1.sycdn.imooc.com//63931553000172b312730461.jpg

查看完整回答
反对 回复 2022-12-09
  • 1 回答
  • 0 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信