【问题标题】:Cypress fixtures structure json and type赛普拉斯夹具结构 json 和类型
【发布时间】:2020-03-06 13:47:44
【问题描述】:

我有一个带有两个输入的表单

context('Include contains from json file', function () {

    beforeEach(() => {
        cy.server()
        cy.fixture("example.json")
            .as('data')
            .then((data) => {
                cy.route('GET', 'example.json', data)
            })
    })

    it('Výběr klienta', function () {

        cy.visit('/info')
        cy.get('[data-cy=username]').type(JSON.stringify(this.data)
        cy.get('[data-cy=surname]').type(JSON.stringify(this.data)
    })
})

如何为来自外部文件 .json 的两个或多个输入键入包含

我的 .json 文件

{
  "name": "Jane"
  "surname": "Doe"


}

【问题讨论】:

  • 您可能想使用this.data.name 而不是JSON.stringify(this.data)

标签: json cypress fixtures


【解决方案1】:
  1. .json 文件应位于 fixtures 文件夹中,因为默认情况下 cypress 会自动在此文件夹中搜索测试数据,除非另外指定路径。

  2. 你必须在 beforeEach() cy.fixture('example.json').as('data')987654322@的测试中加载fixtures文件

  3. 那么您的代码将是:

    cy.get('[data-cy=username]').type(data.name)
    cy.get('[data-cy=surname]').type(data.surname)
    

【讨论】:

    【解决方案2】:

    在具有多个数据而不是存根 API 的单个夹具 JSON 中完美地为我工作

    before describe('Test Single Input Field Form', function() declare :

    const testData = require("../../fixtures/multipleInputFields.json")

    然后

    `testData.forEach((data) => {  
    const message = data.message 
    it('Test Case', function(){      
    cy.log("data is:" + data) 
    cy.get('#user-message').type(message).should('have.value', message) 
    cy.get('#get-input > button').click()  
    cy.wait(200) 
    cy.get('span#display').should('have.text', message)      
    }) 
    });`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      相关资源
      最近更新 更多