Cypress: cy.type() can only accept a string or number. You passed in: 'undefined'

Created on 29 Jul 2019  路  6Comments  路  Source: cypress-io/cypress

Current behavior:

When I try to get data form a Json file, this message shown.
The question: Can I get text from a Json file?

image

Desired behavior:

I Need to insert this data (that the Json file have) in the text area

Steps to reproduce: (app code and test code)

Test

beforeEach(() => {
        cy.server()
        cy.fixture("licencia.json").then((data) => {
            cy.route('GET', 'licencia.json', data)
        })
it.only('POST - Add License', function () {
        cy.get('[id=operations-License-Post]').children('div').click()
          .get('button').contains('Try it out').click()
        cy.get('textarea').clear()
          .get('textarea').type(this.data)

* Json Example/format*
{ "nombres": "Pulga", "apellido": "Rodriguez", "tipoDocumento": "NOT_DEFINED", "fechaNacimiento": "1986-08-10T17:40:08.484Z", "nroLicencia": "32001001", "cuil": "20320010013", "domicilio": "25 de mayo 1350", "nacionalidad": "Argentino", "sexo": "Masculino", "grupoFactor": "0+", "nroTramite": "251350", "numeroInsumo": "string 1", "fechaOtorgamiento": "2019-07-26T17:40:08.484Z", "fechaRenovacion": "2019-07-26T17:40:08.484Z", "fechaVencimiento": "2019-07-26T17:40:08.484Z", "categoria": "A", "clase": "1", "restricciones": "ninguna", "observaciones": "cinco", "centroEmisor": "Las Talitas", "donante": true, "firmante": "Leito", "estado": "Vigente" }

Versions


win10
Chrome v75

All 6 comments

Please read the https://docs.cypress.io/api/commands/fixture.html#Using-an-alias-to-access-a-fixture and the examples linked there. You are never saving the loaded fixture, so there is no this.data later on. You probably want

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

Or even shorter https://docs.cypress.io/api/commands/fixture.html#Shortcuts

beforeEach(() => {
  cy.fixture("licencia.json").as('data')
  cy.server()
  cy.route('licencia.json', 'fixture:licencia')
})

@bahmutov Thanks for the comment! I made this corrections but still have the other issue that I found in the other tries (a lot of tries)
image
I'm not sure if cy.type and json file is that I need to did this kind of enter text
image

so the JSON from the fixture is automatically parsed and converted into an object, right? You probably want to type it as text. This should do the trick

cy.get('textarea').type(JSON.stringify(this.data))

@bahmutov this is another of the issues that I saw. using cy.get('textarea').type(JSON.stringify(this.data)) is shown again

image

Finally!
Thanks for all the help @bahmutov

First solution was use the correct sintaxis like bahmutov explained and works great:

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

Spec

cy.get('textarea').type(JSON.stringify(this.data))

BUT the second one, is related with this (my) particular case: I need a TXT file instead Json. The .txt don't need any particular syntax, so I can put the script with no issues related with specials characters.

For the .txt:

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

Spec

cy.get('textarea').type(this.data)

image

There's my first suite in cypress. Thank you guys!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carloscheddar picture carloscheddar  路  3Comments

szabyg picture szabyg  路  3Comments

jennifer-shehane picture jennifer-shehane  路  3Comments

igorpavlov picture igorpavlov  路  3Comments

simonhaenisch picture simonhaenisch  路  3Comments