0
votes

Trying to integrate both UI & API test cases

Created two files:

  1. API test --> DO Login and writing token & saving it to fixture file

  2. Spec test -->Do REAL Login by using username and password and once test case pass then in next test first get the response by using cy.request using token which saved by step1. Based on response keys search the data in search bar

API Test File:

it("Login Request API",function(){

        cy.request({
            method:"POST",
            url: POST URL,
            headers:{
             "content-type" : "application/json"
            },
            body : {
             "email": USER NAME,
             "password": PASSWORD
            },
            log: true
        }).then((response) => {
             // Parse JSON the body.
             expect(response.status).to.equal(200)
             let resbody = JSON.parse(JSON.stringify(response.body));
            cy.writeFile("File Path to store response",resbody)
         })
    });

Cypress version: 3.8.3

Once above test is executed then it redirect to first test . Not going to further tests

1
You need to refer to docs.cypress.io/api/cypress-api/custom-commands.html#Usage and create a cy.getToken command for step 1. - Sree.Bh
Ideally it should not be the case! Here, I have mentioned only one example but for all multiple api's i can't do the same. I don't want to create Test data for few features as UI is there only for managing purpose and actions can be done on mobile. So By using cy.request I want to generate a response and using that response wanted to do further operations - Parth Makwana
Why it is routing me on first step once That request is executed - Parth Makwana
Can you add some test screenshots to help me understand what is happening with your current approach. - Sree.Bh
To make it simple, Add given file as spec1 (POST CALL) and in spec2 only do cy.visit - Parth Makwana

1 Answers

0
votes

For Cypress Version 3.8.3: You may need to check the path of the new file you are trying to create. it is by default within the 'fixture' folder.

cy.request({
    method:"POST",
    url: POST URL,
    headers: {
      "content-type" : "application/json"
    },
    body : {
      "email": USER NAME,
      "password": PASSWORD
    },
    log: true
}).then((response) => {
    // Parse JSON the body.
    expect(response.status).to.equal(200);
    cy.writeFile("<path>/api.json", response.body); // <path> is with respect to cypress/fixture folder
});

From my test:

Test screenshot