【发布时间】:2019-05-15 23:29:26
【问题描述】:
目前,我们正在使用 cypress 来测试我们的应用程序。我们有 2 个环境和 2 个不同的 api_Servers。我想在环境文件中定义它。我不确定如何在同一个文件中定义这两个 url。
例如,
环境 1:
baseUrl - https://environment-1.me/ Api_Serever - https://api-environment-1.me/v1
环境 2:
baseUrl - https://environment-2.me/ Api_Serever - https://api-environment-2.me/v1
很少有测试用例依赖于 baseUrl,1 个测试用例检查 API 依赖于 Api_Serever。
为了解决这个问题,我尝试在此链接 https://docs.cypress.io/api/plugins/configuration-api.html#Usage 之后的插件内的配置文件中设置 baseUrl 和 Api_Serever。
我为 2 个环境创建了两个配置文件,
{
"baseUrl": "https://environment-2.me/",
"env": {
"envname": "environment-1",
"api_server": "https://api-environment-1.me/v1"
}
}
另一个与此类似的文件更改各自的端点。
插件文件已修改为,
// promisified fs module
const fs = require('fs-extra')
const path = require('path')
function getConfigurationByFile (file) {
const pathToConfigFile = path.resolve('..', 'cypress', 'config', `${file}.json`)
return fs.readJson(pathToConfigFile)
}
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// accept a configFile value or use development by default
const file = config.env.configFile || 'environment-2'
return getConfigurationByFile(file)
}
在测试用例中,以我们使用的baseUrl为准,visit('/')
当我们使用命令 cypress run --env configFile=environment-2 从命令行运行特定文件时,所有测试用例都通过了,因为 visit('/') 会自动替换为期望的相应环境API 测试用例。
我不确定应该如何修改 API 测试以调用 API 端点而不是基本 URL。
有人可以帮忙吗?
谢谢,
【问题讨论】:
-
更新:当您在 api 测试用例中访问时,默认情况下 cy.request(Cypress.env('api_server')) 只需要到 api-environment-1.me 和之后就什么都没有了。例如,我想在测试用例内部发出请求时默认测试api-environment-1.me/v1/status,直到 api-environment-1.me 被访问
标签: javascript testing automated-tests cypress