Naz*_*hyn 2 mysql mysql2 cypress
运行集成测试时出现错误:
0 passing (17s)
1 failure
1) Registration page
register new users allowed and update status in the database:
TypeError: Net.connect is not a function
at new Connection (webpack:///./node_modules/mysql2/lib/connection.js:50:0)
at ./node_modules/mysql2/index.js.exports.createConnection (webpack:///./node_modules/mysql2/index.js:10:0)
at Context.eval (webpack:///./cypress/integration/registration.spec.js:23:34)
Run Code Online (Sandbox Code Playgroud)
这是我的环境:
MySQL Workbench
MySQL Server 8.0.29
Run Code Online (Sandbox Code Playgroud)
我提出了本地后端,我可以访问数据库。这是我的代码:
const mysql2 = require('mysql2');
describe('Registration page', () => {
beforeEach(() => {
// visit the registration page
cy.visit('http://localhost:3000/registration');
});
it('register new users allowed and update status in the database', () => {
// fill out the registration form
cy.get('input[name="fullName"]').type("Nazar Dmytryshyn")
cy.get('input[type="email"]').type('testuser@example.com');
cy.get('input[name="pwd"]').type('testpassword');
cy.get('input[name="confirmPassword"]').type('testpassword');
// submit the form
cy.get('button[class="btn btn-success"]').click();
// check that the user is redirected to the login page
cy.url().should('include', '/login');
// create a connection to the test database
const connection = mysql2.createConnection({
host: '127.0.0.1:3306',
user: 'root',
password: 'rootpassword',
database: 'local1'
});
// open the connection
connection.connect();
// update the developer status in the database
connection.query(
'UPDATE `main_backendless`.`Developer` SET `developerStatusId` = "1" WHERE (`email` = "testuser@example.com")',
(error, results) => {
if (error) throw error;
expect(results.affectedRows).to.equal(1);
}
);
// close the connection
connection.end();
});
});
Run Code Online (Sandbox Code Playgroud)
我检查了这个数据10次,它是正确的,我可以通过MySQL WorkBench连接到数据库
host: '127.0.0.1:3306',
user: 'root',
password: 'rootpassword',
database: 'main_backendless'
Run Code Online (Sandbox Code Playgroud)
我将感激任何可以实现的想法!
我建议使用cypress-mysql,它为您隐藏了很多实现细节。
如果您尝试滚动自己的任务,则可能会得到未定义的返回值。
安装
npm install cypress-mysql
//or
yarn add cypress-mysql
Run Code Online (Sandbox Code Playgroud)
配置
发行说明已过时,以下是 Cypress 10+ 的配置
// cypress.config.js
const { defineConfig } = require("cypress");
const mysql = require('cypress-mysql');
module.exports = defineConfig({
// ...
e2e: {
setupNodeEvents(on, config) {
mysql.configurePlugin(on);
},
"env": {
"db": {
"host": "localhost",
"user": "user",
"password": "password",
"database": "database"
}
}
})
Run Code Online (Sandbox Code Playgroud)
// cypress/support/e2e.js
const mysql = require('cypress-mysql');
mysql.addCommands();
Run Code Online (Sandbox Code Playgroud)
测试
const sql = 'UPDATE "main_backendless.Developer" SET "developerStatusId" = "1" WHERE ("email" = "testuser@example.com")'
cy.query(sql).then(res => {
expect(res.affectedRows).to.equal(1)
});
Run Code Online (Sandbox Code Playgroud)
如果你想使用任务来调用mySql库,你必须从任务中返回一个Promise。
这是因为 mysql 调用是异步的,Cypress 知道等待它们的唯一方法是获取从代码返回的承诺。
赛普拉斯.config.js
const { defineConfig } = require("cypress")
const mysql2 = require('mysql2')
const connection = mysql2.createConnection({
host: '127.0.0.1:3306',
user: 'root',
password: 'rootpassword',
database: 'local1'
})
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', {
mySql: (sql) => {
return new Promise((resolve, reject) => {
connection.query(sql, (error, results) => {
if (error) {
reject(error)
} else {
resolve(results.affectedRows)
})
})
})
}
})
},
})
Run Code Online (Sandbox Code Playgroud)
it('tests with mysql', () => {
cy.task('mySql', 'sql staement here')
.then(result => {
expect(result).to.equal(1);
})
})
Run Code Online (Sandbox Code Playgroud)
或者,mysql2提供一个可以简化代码的承诺包装器:
const { defineConfig } = require("cypress")
const mysql = require('mysql2/promise') // different import here
const connection = mysql2.createConnection({
host: '127.0.0.1:3306',
user: 'root',
password: 'rootpassword',
database: 'local1'
})
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on('task', {
mySql: async (sql) => { // async here
const result = await connection.execute(sql) // await here
return result;
}
})
},
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1015 次 |
| 最近记录: |