简体   繁体   中英

How can I use cypress import fixture file

I'm writing in regard to cypress fixture. I'm trying to use fixture.incorrectEmail and fixture.incorrectPassword but it doesn't work. The error is Cannot read property correctEmail of undefined

I want to use it as below.

My Fixture file json:

{
  "incorrectEmail": "karolina.waterrr@gmail.com",
  "incorrectPassword": "1234777",
}

My test file:

import fixture from '../../fixtures/data.json'

describe("checkAuthentication", () => {
    before(() => {
        cy.visit("http://localhost:8080")
    })
    it('it should fail if bad email', () => {
        cy.dataTestId("worker-email").type(fixture.incorrectEmail)
        cy.dataTestId("worker-password").type(fixture.incorrectPassword)
        cy.dataTestId("test-button").click()
        cy.dataTestId("test-wrong-message").should("have.text", " wrong email ")

    })

my config file:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es5",
      "dom"
    ],
    "types": [
      "cypress"
    ],
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
  },
  "include": [
    "**/*.ts"
  ]
}

Thank you for your help,

Import is correct but console.log(fixture) return undefined, honestly I don't know what is wrong

The way to load a fixture on the this context is shown here

describe("checkAuthentication", function () {

  before(function () {
    cy.visit("http://localhost:8080")
  })

  beforeEach(function () {
    // "this" points at the test context object
    cy.fixture('data').then((user) => {
      // "this" is still the test context object
      this.user = user
    })
  })

  it('it should fail if bad email', function () {

    // Note access via "this.user"

    cy.dataTestId("worker-email").type(this.user.incorrectEmail)
    cy.dataTestId("worker-password").type(this.user.incorrectPassword)
    cy.dataTestId("test-button").click()
    cy.dataTestId("test-wrong-message").should("have.text", " wrong email ")
  })
})

But

import fixture from '../../fixtures/data.json'

works just as well, so I suspect you have just mixed up property names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM