cypressIO使用cy.wrap()从cy.task()返回字符串值会给出错误“未定义cy”。 [英] CypressIO Returning string value from cy.task() using cy.wrap() gives error "cy is not defined"

查看:462
本文介绍了cypressIO使用cy.wrap()从cy.task()返回字符串值会给出错误“未定义cy”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在赛普拉斯 /plugins/index.js 中,我有查询oracleDB的代码

In cypress /plugins/index.js I have code to query oracleDB

module.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。

This returns the correct extracted URL in Node terminal.

但是在我的赛普拉斯测试中,当我尝试使用extractedUrlText字符串值时,出现错误 cy未定义

But then in my cypress test, when I try to use that extractedUrlText string value i'm getting error 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使用。使用cy.wrap()的task()

I use a similiar approach to use a returned value from support/commands.js Cypress.Commands.add() and it works there, but not from cy.task() using cy.wrap()

推荐答案

我的工作解决方案:

/plugins/index.js 从上述代码扩展而成:

/plugins/index.js extended from above code:

 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)
        })

结果:

这篇关于cypressIO使用cy.wrap()从cy.task()返回字符串值会给出错误“未定义cy”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆