我正在尝试安装cypress-sql-server,但是我使用的是10.8.0版本,它不使用cypress.json来配置环境。我找到的所有安装说明都提到使用cypress.json配置插件。在u/Fody的帮助下,我更接近了,但我仍然遇到了一个错误:
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有什么建议吗?我还能提供更多的信息来帮忙吗?
发布于 2022-09-23 19:51:08
这是cypress-sql-server为Cypress v9提供的安装指令。
插件文件 插件可以在您的cypress/plugins/index.js文件中初始化,如下所示。 const sqlServer =需要量(‘cypress-sql-server’);module.exports = (on,config) => { sqlServer.loadDBPlugin(config.db);on(任务,任务);}
翻译成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文件。 从‘cypress-sql-server’导入sqlServer;sqlServer.loadDBCommands();
Cypress v10+
只需将此代码移动到support/e2e.js
发布于 2022-09-23 18:03:43
cypress.json是一种指定柏树环境变量。而不是使用cypress.json文件的方法,您可以在该链接中使用任何策略。
如果您只想将它们包含在您的cypress.config.js中,它应该如下所示:
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:1234',
env: {
db: {
// your db values here
}
}
}
})https://stackoverflow.com/questions/73831176
复制相似问题