【问题标题】:How to map different JSON objects from the fixture into specific spec test file in the cypress如何将夹具中的不同 JSON 对象映射到 cypress 中的特定规范测试文件
【发布时间】:2021-11-24 13:30:31
【问题描述】:

我有下面的 Input.json 作为夹具,它包含两个不同的测试用例。

Input.json(夹具文件夹)

[
    {
        "searchKeyword":"cypress"
    },
    {
        "username":"QATesting",
        "password":"testprofile"
    }
]

以上数据将验证 Google 的两种不同功能。一个用于验证搜索引擎,另一个用于验证用户登录活动(这仅用于示例用例,可能会模仿我的实际需求)。

我刚刚创建了 cypress runner,我只想使用下面的 runner.js 文件运行规范文件

const cypress = require('cypress')
const fixtures = require('./cypress/fixtures/Test.json')

const promises = fixtures.map(fixture => {
  return cypress.run({
    env: {
      fixture
    },
    spec: './cypress/integration/test.spec.js',
  });
});

我刚刚在下面的“test.spec.js”文件中分别添加了两个不同的It(测试用例)。一项测试将执行搜索功能,另一项测试将检查现有用户登录活动:

describe("How to map two different data set with respective test function",() =>{

    const baseUrl = "https://www.google.com/";

    const testData = Cypress.env('fixture')

    beforeEach("",()=>{

        cy.visit(baseUrl);
    });

    it("Test Case1: Search the keyword", function () {
            cy.xpath("//input[@name='q']").type(testData.searchKeyword);
            cy.xpath("//input[@value='Google Search']").click();
            cy.get("//ul/li[2]").should("be.visible");

    });

    it("Test Case2: login to the gmail account", function(){
        cy.xpath("//a[contains(text(),'Sign in')]").click();
        cy.xpath("//div[contains(text(),'Use another account')]").click();
        cy.xpath("#identifierId").type(testData.username);
        cy.xpath("//*[contains(text(),'Next')]").click();
        cy.xpath("#password").type(testData.password);
        cy.xpath("#submitbtn").click();
    })

});

但是第二个测试失败了,testData.username 返回 undefined。

有没有办法在 test.spec.js 文件中将特定的 JSON 数组对象映射到特定的函数?

不确定如何将第一个数据集索引与第一个 It(测试用例 1)和第二个数据集索引分别映射到第二个测试用例。

【问题讨论】:

    标签: cypress cypress-component-test-runner


    【解决方案1】:

    由于您的灯具数据在一个数组中,并且用户名和密码字段位于索引 1 处,因此为了访问您必须使用的那些:

    testData[1].username
    testData[1].password
    

    如果您不想使用索引值,请将夹具结构更改为:

    {
      "searchKeyword": "cypress",
      "username": "QATesting",
      "password": "testprofile"
    }
    

    并在您的测试中直接使用:

    testData.username
    testData.password
    

    【讨论】:

    • 不通过索引是否可以实现?像有没有其他方法可以将数据映射到规范文件中的特定测试用例函数
    • 是的。但是随后您必须更改您的 fixture.json 并编写一个简单的 JSON 而不是在数组中。更新了我的答案。
    【解决方案2】:

    如果 testData 没有所需的属性,一个快速的方法是跳过,

    describe("How to map two different data set with respective test function",() =>{
    
        const baseUrl = "https://www.google.com/";
    
        const testData = Cypress.env('fixture')
    
        beforeEach("",()=>{
            cy.visit(baseUrl);
        });
    
        it("Test Case1: Search the keyword", function () {
    
          if (!testData.searchKeyword) this.skip          
    
          cy.xpath("//input[@name='q']").type(testData.searchKeyword);
          cy.xpath("//input[@value='Google Search']").click();
          cy.get("//ul/li[2]").should("be.visible");
        });
    
        it("Test Case2: login to the gmail account", function() {
    
          if (!testData.username) this.skip
    
          cy.xpath("//a[contains(text(),'Sign in')]").click();
          cy.xpath("//div[contains(text(),'Use another account')]").click();
          cy.xpath("#identifierId").type(testData.username);
          cy.xpath("//*[contains(text(),'Next')]").click();
          cy.xpath("#password").type(testData.password);
          cy.xpath("#submitbtn").click();
        })
    });
    

    标记

    你也可以进入标签,给testData添加一个标签属性

    [
        {
            "tag": "search",
            "searchKeyword":"cypress"
        },
        {
            "tag": "user",
            "username":"QATesting",
            "password":"testprofile"
        }
    ]
    

    也许使用像cypress-tags 这样的库,然后在运行脚本中

    const cypress = require('cypress')
    const fixtures = require('./cypress/fixtures/Test.json')
    
    const promises = fixtures.map(fixture => {
    
      if (fixture.tag) {
        process.env.CYPRESS_INCLUDE_TAGS = fixture.tag
      }
    
      return cypress.run({
        env: {
          fixture
        },
        spec: './cypress/integration/test.spec.js',
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2019-07-06
      • 1970-01-01
      相关资源
      最近更新 更多