我正在尝试设置 cypress-sql-server,但我使用的是 10.8.0 版本,该版本不使用 cypress.json 来配置环境。我发现的所有设置说明都涉及使用 cypress.json 来配置插件。我遇到了一个错误:
tasksqlServer:execute, SELECT 'Bob'
CypressError
cy.task('sqlServer:execute') failed with the following error:
The 'task' event has not been registered in the setupNodeEvents method. You must register it before using cy.task()
Fix this in your setupNodeEvents method here:
D:\git\mcare.automation\client\cypress\cypress.config.jsLearn more
node_modules/cypress-sql-server/src/commands/db.js:7:1
5 | }
6 |
> 7 | cy.task('sqlServer:execute', query).then(response => {
| ^
8 | let result = [];
9 |
cypress.config.js
const { defineConfig } = require("cypress");
const sqlServer = require("cypress-sql-server");
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// allows db data to be accessed in tests
config.db = {
"userName": "user",
"password": "pass",
"server": "myserver",
"options": {
"database": "mydb",
"encrypt": true,
"rowCollectionOnRequestCompletion": true
}
}
// code from /plugins/index.js
const tasks = sqlServer.loadDBPlugin(config.db);
on('task', tasks);
return config
// implement node event listeners here
},
},
});
testSQL.spec.js
describe('Testing SQL queries', () => {
it("It should return Bob", () => {
cy.sqlServer("SELECT 'Bob'").should('eq', 'Bob');
});
})
我的版本:
\cypress> npx cypress --version
Cypress package version: 10.8.0
Cypress binary version: 10.8.0
Electron version: 19.0.8
Bundled Node version:
16.14.2
``
这是
cypress-sql-server
当前为Cypress v9提供的安装说明
插件文件
插件可以在 cypress/plugins/index.js 文件中初始化,如下所示。
const sqlServer = require('cypress-sql-server'); module.exports = (on, config) => { tasks = sqlServer.loadDBPlugin(config.db); on('task', tasks); }
将其翻译为 Cypress v10+
const { defineConfig } = require('cypress')
const sqlServer = require('cypress-sql-server');
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// allows db data to be accessed in tests
config.db = {
"userName": "user",
"password": "pass",
"server": "myserver",
"options": {
"database": "mydb",
"encrypt": true,
"rowCollectionOnRequestCompletion": true
}
}
// code from /plugins/index.js
const tasks = sqlServer.loadDBPlugin(config.db);
on('task', tasks);
return config
},
},
})
其他变体也有效,例如将
"db": {...}
部分放在 "e2e: {...}"
部分下方,但 不将 放在 "env": {...}
部分中。
Cypress v9
说明命令文件
该扩展提供了多组命令。您可以导入您需要的。
示例文件。support/index.js
import sqlServer from 'cypress-sql-server'; sqlServer.loadDBCommands();
对于赛普拉斯 v10+
只需将此代码移至
support/e2e.js
cypress.json
是指定 Cypress 环境变量的一种方法。 您可以使用该链接中的任何策略,而不是使用 cypress.json
文件。
如果您只是想将它们包含在您的
cypress.config.js
中,它看起来像这样:
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:1234',
env: {
db: {
// your db values here
}
}
}
})