如何将Fixture/*.json文件中的特定记录访问到Cypress中特定的测试用例中? [英] How to access specific record from fixture/*.json file into sepcific test case in cypress?

查看:30
本文介绍了如何将Fixture/*.json文件中的特定记录访问到Cypress中特定的测试用例中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

testdata.json

[
    {
        "case_id": 1,
        "case": "Login with valid data as wholesaler",
        "username": "admin",
        "password": "password",
        "result": "pass"
    },
    {
        "case_id": 2,
        "case": "Login with valid data as reseller",
        "username": "myreseller.admin",
        "password": "password",
        "result": "pass"
    },
    {
        "case_id": 3,
        "case": "Login with valid data as subscriber",
        "username": "mytenant.admin",
        "password": "password",
        "result": "pass"
    }
]

login.spec.js

  before(() => {
    cy.fixture('testdata').then((datajson) => {
      testdata = datajson
      return testdata
    })
  })
------it blocks-----
  it.only('TC03 - Login with valid data as subscriber', () => {
    cy.login(testdata.username, testdata.password);                            
    cy.title().should('equal', 'Home');
    cy.logout();
-------more it blocks-------
如何正确使用它,以便第2行返回 {testdata.username=mytenant.admin||testdata.password=password} 具体从第3条记录提取

推荐答案

在Cypress文档中,有两种方法描述了如何处理Fixtures文件夹中的那些JSON文件:https://docs.cypress.io/api/commands/fixture#Examples

1.选项

使用cy.fixture()加载testdata.json:

cy.fixture('testdata.json').as('testdata');

2.选项(我的推荐)

在login.spec.js中,您可以从Fixtures文件夹导入所有相关的json文件:

import testdata from '../fixtures/testdata.json';

然后您可以按如下方式直接使用它们,例如使用第一个用户的用户名和密码:

  it.only('TC03 - Login with valid data as subscriber', () => {
    cy.login(testdata[0].username, testdata[0].password);                            
    cy.title().should('equal', 'Home');
    cy.logout();
  })

这篇关于如何将Fixture/*.json文件中的特定记录访问到Cypress中特定的测试用例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆