Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pablogiroud opened this issue Jul 29, 2019 · 6 comments
Closed

Comments

@pablogiroud
Copy link

pablogiroud commented Jul 29, 2019

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"
    }```
<!-- Issues without reproducible steps WILL BE CLOSED -->

<!-- You can fork https://github.com/cypress-io/cypress-test-tiny repo, set up a failing test, then tell us the repo/branch to try. -->

### Versions

<!-- Cypress, operating system, browser -->
win10
Chrome v75
@pablogiroud pablogiroud changed the title cy.type() can only accept a string or number. You passed in cy.type() can only accept a string or number. You passed in: 'undefined' Jul 29, 2019
@bahmutov
Copy link
Contributor

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')
})

@pablogiroud
Copy link
Author

pablogiroud commented Jul 29, 2019

@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

@bahmutov
Copy link
Contributor

bahmutov commented Jul 29, 2019

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))

@pablogiroud
Copy link
Author

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

image

@bahmutov
Copy link
Contributor

see #4287

@pablogiroud
Copy link
Author

pablogiroud commented Jul 30, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants