使用连接到数据库的cypress-postgres遇到问题。服务器有多个数据库和多个架构。尝试了多个插件,但可以找到一种解决方案,允许我在数据库中指定特定的架构。服务器可以通过vpn访问。
将需要对sql数据库执行此操作。任何帮助,更好的方法建议欢迎。
-谢谢!-迈克尔·弗雷泽
IE:数据库示例。
返回的错误
no relation for homePackage.json有以下依赖关系:
name": "tests",
"version": "1.0.0",
"description": "Automated Tests",
"main": "index.js",
"scripts": {
"cypress:open": "cypress open",
"test": "cypress open"
},
"repository": {
"type": "git",
"url":
},
"author": "Michael Frazier
"license": "ISC",
"dependencies": {
"@testing-library/cypress": "^8.0.2",
"@testing-library/vue": "^5.8.2",
"@vue/test-utils": "^1.3.0",
"cypress-commands": "^2.0.1",
"cypress-downloadfile": "^1.2.1",
"cypress-postgres": "^1.1.1",
"cypress-postgresql": "^1.0.8",
"cypress-real-events": "^1.6.0",
"pg": "^8.7.1",
"pg-promise": "^10.5.8",
"yarn": "^1.22.17"
},
"devDependencies": {
"@4tw/cypress-drag-drop": "^2.1.0",
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@cypress/skip-test": "^2.6.1",
"@cypress/webpack-preprocessor": "^5.9.1",
"@testing-library/dom": "^8.11.2",
"@types/chai": "^4.2.21",
"@types/jest": "^27.0.1",
"@types/mocha": "^9.0.0",
"cy-mobile-commands": "^0.3.0",
"cypress": "^9.5.3",
"cypress-file-upload": "^5.0.8",
"cypress-fill-command": "^1.0.2",
"cypress-iframe": "^1.0.1",
"cypress-mailosaur": "^2.6.0",
"cypress-mochawesome-reporter": "^2.4.0",
"cypress-msteams-reporter": "^1.0.3",
"cypress-plugin-tab": "^1.0.5",
"cypress-wait-until": "^1.7.2",
"dayjs": "^1.10.6",
"faker": "^5.5.3",
"mysql": "^2.18.1",
"pdf-parse": "^1.1.1",
"webpack": "^5.51.1"
}
}Cypress.json具有以下格式的DB凭据:
{
"trashAssetsBeforeRuns": true,
"responseTimeout": 30000,
"requestTimeout": 50000,
"defaultCommandTimeout": 4000,
"experimentalSourceRewriting": true,
"experimentalStudio": true,
"projectId": "aycmk8",
"reporter": "cypress-mochawesome-reporter",
"reporterOptions": {
"reportDir": "cypress/report",
"charts": true,
"reportPageTitle": "Melos Report"
},
"db":
{"user": "postgres",
"host": "",
"database": "",
"password": "",
"schemas": "sct",
"port": 5432
}
}cypress\plugins\index.js文件中的插件:
module.exports = async (on, config) => {
on('task',
{dbQuery: (query)=> require('cypress-postgres')(query.query,query.connection)})Spec文件有以下测试:
describe("Database Health Tests", function () {
it('should ping the database to ensure its active ', function () {
cy.task("dbQuery", {"query":"select * from home"})
});发布于 2022-07-31 17:30:51
您的dbQuery缺少连接字符串:
cy.task("dbQuery",
{
query: "select * from home",
connection: "your connection string"
})发布于 2022-10-18 10:30:55
如果您将重点放在npm中对cypress-postgres的描述上,它们提供了2种使用方法:
require('cypress-postgres')加载数据库连接。cy.task中自己设置数据库连接。第一条路
这种方式可能会使您出错,该示例向您显示了不明确的参数:
// In cypress\plugins\index.js
module.exports = on => {
on("task", {
dbQuery:(query)=> require("cypress-postgres")(query.query,query.connection)
});
};我不确定query.connection是从哪里来的。如果您访问该依赖项,您将看到:
// In require('cypress-postgres')
const pgp = require('pg-promise')();
const postgressConfig = require(require('path').resolve('cypress.json'));
module.exports = function(query,userDefineConnection) {
let connection = postgressConfig.db
if (userDefineConnection!=undefined){
connection=userDefineConnection
}
const db = pgp(connection);
return db.any(query)
}因此,您可以将query.connection更改为变量。
通常,cypress\plugins\index.js中的模块接收2-参数on和config。config将引用cypress.json,您可以通过config.env.db获得数据库连接:
{
"env": {
"db": {
"user": "postgres",
"host": "localhost",
"database": "postgres",
"password": "mysecretpassword",
"port":5432
}
}
}现在,cypress\plugins\index.js应该是:
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on("task", {
dbQuery: (query) => require("cypress-postgres")(query.query, config.env.db)
})
}在*.spec.js中:
// ...
it('set "Y" to db', () => {
cy.task("dbQuery", { "query": "select count(*) as num_row from home"
}).then(result => {
expect(result[0].num_row).to.equal('1')
})
})
// ...秒路
这种方法直接在*.spec.js中分配数据库连接:
// ...
it('set "Y" to db', () => {
cy.task("dbQuery", {
"query": "select count(*) as num_row from home",
"connection": {
"user": "postgres",
"host": "localhost",
"database": "postgres",
"password": "mysecretpassword",
"port":5432
}
}).then(result => {
expect(result[0].num_row).to.equal('1')
})
})
// ...https://stackoverflow.com/questions/72148473
复制相似问题