question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(Add Documentation for) Passing API return values between given/when/then

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
PointmanDevcommented, Jul 2, 2018

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


1reaction
twilczekcommented, Jul 17, 2018

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

(Add Documentation for) Passing API return values between ...
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 ...
Read more >
How to write REST API Test in Cucumber BDD ... - Tools QA
The test will start by generating a Token for Authorization; Get List of available books in the library; Add a book from the...
Read more >
Parameterize BDD Tests | TestComplete Documentation
This applies to all numerical values, whether they are integer or floating-point. Let's check what values TestComplete passes to the generated functions.
Read more >
How to perform API testing with REST Assured | TechBeacon
Getting started: Configuration · First test: Understanding the syntax · Validating technical response data · Parameterizing tests · Accessing secured APIs · Passing...
Read more >
Welcome to Pytest-BDD's documentation! — Pytest-BDD 6.1.1 ...
Welcome to Pytest-BDD's documentation! BDD library for the pytest runner. Install pytest-bdd; Example; Scenario decorator; Step aliases; Step arguments ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found