首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Cypress 12连接Mysql?

如何使用Cypress 12连接Mysql?
EN

Stack Exchange QA用户
提问于 2023-02-14 04:38:53
回答 1查看 331关注 0票数 -1

我想连接到Mysql在服务器上删除几条记录。我试过使用一些教程和答案。因为他们都是关于旧版本的柏树,我不知道是什么错误。这是密码。我很高兴知道是否有任何简单的方法与较少的代码或简单的配置。谢谢

cypress.config.js

代码语言:javascript
复制
const mysql = require("mysql")

function queryTestDb(query, config) {
  // creates a new mysql connection using credentials from cypress.json env's
  const connection = mysql.createConnection(config.env.db);
  // start connection to db
  connection.connect();
  // exec query + disconnect to db as a Promise
  return new Promise((resolve, reject) => {
    connection.query(query, (error, results) => {
      if (error) reject(error);
      else {
        connection.end();
        // console.log(results)
        return resolve(results);
      }
    });
  });
}

module.exports = defineConfig({

    e2e: {
    
        db: {
          host: "10.100.111.25",
          user: "xxxx",
          password: "xxxxx",
          database: "testDB"
      },
      async setupNodeEvents(on, config) {
      // implement node event listeners here

        const bundler = createBundler({
          plugins: [createEsbuildPlugin(config)],
        });

        on("file:preprocessor", bundler);
        await addCucumberPreprocessorPlugin(on, config);
      
        on("task", {
          queryDb: query => {
            return queryTestDb(query, config);
          }
        });

        return config;
      
      },
    }
})          

Test.spec.js

代码语言:javascript
复制
cy.task("queryDb","'select * from data_table where CREATEDDATE like '2022-12-23'")
        .then(count => {
            expect(count);
            cy.log('DB records count is => '+ count);
          });

Package.json

代码语言:javascript
复制
"devDependencies": {
    "@badeball/cypress-cucumber-preprocessor": "^15.1.2",
    "@bahmutov/cypress-esbuild-preprocessor": "^2.1.5",
    "cypress": "^12.4.0",
    "cypress-cucumber-preprocessor": "^4.3.1",
    "cypress-file-upload": "^5.0.8",
    "cypress-mochawesome-reporter": "^3.2.3",
    "cypress-multi-reporters": "^1.6.2",
    "cypress-xpath": "^2.0.1",
    "mocha": "^10.2.0",
    "mochawesome": "^7.1.3",
    "mochawesome-merge": "^4.2.2",
    "mysql": "github:mysqljs/mysql"
  },
  "dependencies": {
    "cypress-email-results": "^1.8.0",
    "cypress-mysql": "^1.0.0",
    "dayjs": "^1.11.5"
  },
EN

回答 1

Stack Exchange QA用户

发布于 2023-02-15 11:54:07

由于我不得不花很多时间想出一个解决办法,所以我想自己回答一下,以防有人遇到同样的问题。

  1. 首先,我必须重新安装mysql2。如果您已经安装了Mysql,请先卸载它。

npm i mysql2 -D

  1. 配置cypress.config.js const mysql = require("mysql2")

在同一个文件中添加下面的函数

代码语言:javascript
复制
  // creates a new mysql connection using credentials from cypress.json env's
  const connection = mysql.createConnection(config.env.db);
  // start connection to db
  connection.connect();
  // exec query + disconnect to db as a Promise
  return new Promise((resolve, reject) => {
    connection.query(query, (error, results) => {
      if (error) reject(error);
      else {
        connection.end();
        // console.log(results)
        return resolve(results);
      }
    });
  });
}

module.exports = defineConfig({

    "env":{
      "db": {
        "host": "11.11.11.11",
        "user": "username",
        "password": "password",
        "database": "databaseName",
        "port": 08,
        //"key": "cypress/pemFiles/testPemFile.pem",
        //"http": true
      },
    },

    async setupNodeEvents(on, config) {
        on("task", {
            queryDb: query => {
               return queryTestDb(query, config)
            }
        });
    }

});      
  1. 现在,在youtest.spec.js中,我已经将结果对象分配到一个var记录中,如果对象并打印到日志中,则读取零行。
代码语言:javascript
复制
cy.task('queryDb',"select * from tableName where columnname = '2050-11-29'")
        .then(function(returnObject) {
            //expect(result);
            var record = returnObject;
            const results = Object.values(record[0])
            cy.log('DB records count is => '+ results);
}); 

此外,还可以通过将其索引号添加为结果0 cy.log('DB records count is => '+ results[0]);来获得列值。

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

https://sqa.stackexchange.com/questions/51256

复制
相关文章

相似问题

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