2

我有下面的 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 返回未定义。

无论如何在 test.spec.js 文件中将特定的 JSON 数组对象映射到特定的函数?

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

4

2 回答 2

1

如果 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',
  });
});
于 2021-10-04T04:57:23.820 回答
0

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

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

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

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

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

testData.username
testData.password
于 2021-10-04T04:37:56.757 回答