首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cypress中设置/定义环境变量和api_Server?

如何在cypress中设置/定义环境变量和api_Server?
EN

Stack Overflow用户
提问于 2019-05-16 07:29:27
回答 1查看 6.3K关注 0票数 1

目前,我们正在使用cypress测试我们的应用程序。我们有两个具有两个不同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_Serever。

为了解决这个问题,我尝试在配置文件中设置baseUrl和Api_Serever,在这个链接https://docs.cypress.io/api/plugins/configuration-api.html#Usage之后的插件中。

我为两个环境创建了两个配置文件,

代码语言:javascript
复制
{
    "baseUrl": "https://environment-2.me/",
    "env": {
      "envname": "environment-1",
      "api_server": "https://api-environment-1.me/v1"
    }
}

另一个与此类似的文件将更改相应的端点。

插件文件已修改为,

代码语言:javascript
复制
// 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为准(‘/’)

当我们使用命令cypress run --env configFile=environment-2从命令行运行一个特定的文件时,所有测试用例都通过了,因为访问(‘/’)自动替换为API测试用例之外的相应环境。

我不确定应该如何修改API测试以调用API端点而不是基URL。

有人能帮帮忙吗?

谢谢,indhu。

EN

回答 1

Stack Overflow用户

发布于 2019-05-16 09:36:18

如果我正确理解了您的问题,您需要使用不同的urls运行测试。在cypress.json或环境文件中设置的Urls。您可以在cypress.json文件中配置urls,如下所示。我还没试过,你能试一试吗?

代码语言:javascript
复制
{
    "baseUrl":  "https://environment-2.me/",
    "api_server1": "https://api1_url_here",
    "api_server2": "https://api2_url_here"
}

在测试调用内部传递urls,如下所示;

代码语言:javascript
复制
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..
    })
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56158890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档