silvertonbook
silvertonbook

Reputation: 1

What's the best way for Cypress test to communicate with the React app?

I have four separate Cypress tests with ID test1, test2 and test3, and another one without any ID. I want my app to call three different apis based on the test id. What's the best way for Cypress to pass information to the app?

To run test without an id, call http://www.myapi.com/data
To run test with id test1, call http://www.myapi.com/data?id=test1
To run test with id test2, call http://www.myapi.com/data?id=test2
To run test with id test3, call http://www.myapi.com/data?id=test3

Upvotes: 0

Views: 277

Answers (1)

TesterDick
TesterDick

Reputation: 10580

I'm presuming you are using http://www.myapi.com/data?id=test1 in a cy.visit() command.

You can use string interpolation to change the URL parameter, roughly

['test1', 'test2', 'test3`].forEach(param => {

  it(`Tests with ${param}`, () => {
    cy.visit(`http://www.myapi.com/data?id=${param}`)
    ...
  })
})

or in cy.request()

['test1', 'test2', 'test3`].forEach(param => {

  const url = `http://www.myapi.com/data?id=${param}`
  cy.request(url).then(response => {
    ...
  })
})

Upvotes: 1

Related Questions