【问题标题】:How to set/define environment variables and api_Server in cypress?如何在 cypress 中设置/定义环境变量和 api_Server?
【发布时间】: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。

有人可以帮忙吗?

谢谢,

【问题讨论】:

标签: javascript testing automated-tests cypress


【解决方案1】:

如果我正确理解您的问题,您需要使用不同的网址运行测试。在 cypress.json 或 env 文件中设置 URL。 您可以在 cypress.json 文件中配置 url,如下所示。不过我没试过,你可以试一试吗?

{
    "baseUrl":  "https://environment-2.me/",
    "api_server1": "https://api1_url_here",
    "api_server2": "https://api2_url_here"
}

在测试调用中传递如下网址;

describe('Test for various Urls', () => {
    it('Should test the base url', () => {
      cy.visit('/')  // this point to baseUrl configured in cypress.json file
      // some tests to continue based on baseUrl..
    })

    it('Should test the api 1 url', () => {
      cy.visit(api_server1)  // this point to api server 1 configured in cypress.json file
      // some tests to continue based on api server1..
    })

  it('Should test the api 2 url', () => {
      cy.visit(api_server2)  // this point to api server 2 configured in cypress.json file
      // some tests to continue based on api server2..
    })
})

【讨论】:

  • 非常感谢您的回复,@soccerway。我采用了与您类似的方法:)
  • 太棒了,继续思考是否有其他方法可以实现这一目标。如果您愿意接受,请投票给我的答案,以便我获得我的互联网积分:)
  • 我投票了 ;) 但没有声望,所以不会显示
【解决方案2】:

此问题已解决。

最好的方法是按照他们的文档 (https://docs.cypress.io/api/plugins/configuration-api.html#Usage) 的建议使用插件。

我在我的问题中保持了相同的结构,在我的测试用例中我使用 cy.request(Cypress.env('api_server'))

这解决了我的问题:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2021-05-10
    • 2018-03-01
    • 2021-03-29
    • 1970-01-01
    • 2011-06-15
    • 2021-02-20
    相关资源
    最近更新 更多