简体   繁体   English

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

[英]CypressIO Returning string value from cy.task() using cy.wrap() gives error "cy is not defined"

In cypress /plugins/index.js I have code to query oracleDB在 cypress /plugins/index.js我有查询 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);
    }
  });
}

This returns the correct extracted URL in Node terminal.这将在 Node 终端中返回正确提取的 URL。

But then in my cypress test, when I try to use that extractedUrlText string value i'm getting error cy is not defined :但是在我的柏树测试中,当我尝试使用那个 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

    })

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()我使用类似的方法使用来自support/commands.js Cypress.Commands.add()的返回值,它在那里工作,但不是来自 cy.task() 使用 cy.wrap()

在此处输入图像描述

My working solution:我的工作解决方案:

/plugins/index.js extended from above code: /plugins/index.js从上面的代码扩展:

 var extractedUrlText = JSON.stringify((await result).rows).extract('/VerifiedRegistrationFormView','\"');
    console.log('Extracted NODE URL: \r\n' + extractedUrlText);

  return extractedUrlText
}

In spec file:在规范文件中:

let extractedUrl;

        cy.task('registration', emailAddress).then(value => {
            extractedUrl = value;
            cy.log('Extracted CY URL: ' + extractedUrl)
        })

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM