Cypress-cucumber-preprocessor: (Add Documentation for) Passing API return values between given/when/then

Created on 5 Apr 2018  路  6Comments  路  Source: TheBrainFamily/cypress-cucumber-preprocessor

Hi,

I'm using cypress-cucumber-preprocessor to e2e test an API, and am having trouble using cy.request due to it naturally needing to be chained.

In Cypress I would normally do

cy.request('/test').its('status').should(...) etc 

In the example given you use cy.visit, and I assume this stores it in the cy object? What is the best way to do this? Or is this not the correct way of thinking? Currently I am doing this:

/* global cy, then, when, given */

const testEndpoint = '/test'
let response

given('I have access to the test endpoint', () => {
  // expect(true).to.be.equal(true)
})

when('I request the GET /test endpoint', () => {
  response = cy.request(testEndpoint)
})

then(`I get a {int} response code`, (responseCode) => {
  response.its('status').should('eq', responseCode)
})

whilst this works this is obviously not great, and when I want to split out the then into its own shared file etc it will become a nightmare!

Many thanks

Most helpful comment

You can use everything that's in Cypress with this library.

If I was doing this in a single function, I'd use the "Cy.as" syntax and then wait for it. I think you should be able to do this across steps in the same scenario, though.

E.G.

given('I have access to the test endpoint', () => {
  // expect(true).to.be.equal(true)
})

when('I request the GET /test endpoint', () => {
  cy.server()
  cy.route('GET', '/test').as('loginResponse');  // using "AS" here instead of a local variable.
  cy.request(testEndpoint)

})

then(`I get a {int} response code`, (responseCode) => {
    cy.wait('@loginResponse').then((xhr) => {
        expect(xhr.status).to.equal(responseCode);
     })
});


All 6 comments

I guess another question is, is it possible to use the world with this library?

Hello @OllyNuralAND
Thanks for using the package and the feedback :-)

Could you elaborate a bit more about: "when I want to split out the then into its own shared file etc it will become a nightmare!" (Could you maybe provide an example?)
and: " is it possible to use the world with this library?"

Do you mean using "this" keyword between steps?

This is still very new package and we are open to making significant changes/improvements :)

Hi,

I am new to Cypress. I am in need to integrate cucumber with Cypress. Choose to use cucumber plugin hence choose the cucumber -cypress example to work with initially. ButwWhen I tried to select the folder "cypress-cucumber-preprocessor-master" in Cypress, the window simply stuck unable to import without any error message or image. Kindly suggest correct steps to execute the existing feature files in cypress

You can use everything that's in Cypress with this library.

If I was doing this in a single function, I'd use the "Cy.as" syntax and then wait for it. I think you should be able to do this across steps in the same scenario, though.

E.G.

given('I have access to the test endpoint', () => {
  // expect(true).to.be.equal(true)
})

when('I request the GET /test endpoint', () => {
  cy.server()
  cy.route('GET', '/test').as('loginResponse');  // using "AS" here instead of a local variable.
  cy.request(testEndpoint)

})

then(`I get a {int} response code`, (responseCode) => {
    cy.wait('@loginResponse').then((xhr) => {
        expect(xhr.status).to.equal(responseCode);
     })
});


@KokilaGanesan I don't understand what you mean, coud you open a new issue? As it doesn't seem to be related to this one.

@PointmanDev Thanks, that looks great, and with this example I finally understood what @OllyNuralAND meant.. We should probably test and document this, as it does look like a pattern that should be widely used.

@OllyNuralAND We have prepared an example of how to share context between given, when, then steps. You can see it here:
https://github.com/TheBrainFamily/cypress-cucumber-preprocessor/pull/74
Note that we alias the input in When step as addNewItemInput and we use this reference in Then step to make sure it's empty.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EmmanuelDemey picture EmmanuelDemey  路  5Comments

NoNameProvided picture NoNameProvided  路  8Comments

johnknoop picture johnknoop  路  3Comments

PrinceJohn picture PrinceJohn  路  3Comments

roschaefer picture roschaefer  路  5Comments