lundi 5 avril 2021

Cypress access alias with this.* doesn't works

I'm having a little problem understanding Cypress documentation. In the alias section they've added a use case of accessing alias with fixtures using the this.* reference:

beforeEach(() => {
  // alias the users fixtures
  cy.fixture('users.json').as('users')
})

it('utilize users in some way', function () {
  // access the users property
  const user = this.users[0]

  // make sure the header contains the first
  // user's name
  cy.get('header').should('contain', user.name)
})

But when I try to reproduce it, I keep getting the error: Cannot read property 'SOAP_body' of undefined.

I don't understand where is my error. Here is my spec:

/// <reference types="cypress"/>

describe("SOAP API Test", () => {

  beforeEach(() => {
    cy.fixture('SOAP_body.xml').as('SOAP_body')
  });

  it("Test with task", () => {
    const body = this.SOAP_body

    cy.request({

      method: "POST",
      headers: {
        "content-type": "text/xml; charset=utf-8",
        Authorization: "Token myVerySecretToken",
        SOAPAction: "http://tempuri.org/TrackingFull",
      },
      url: `https://endpoint.of/the/application.asmx`,
      body: body,
      failOnStatusCode: false

    }).then((result) => {

      expect(result.status).to.equal(200);
      cy.task("XMLtoJSON", result.body).then((response) => {
        expect(
          response.elements[0].elements[1].elements[0].elements[0].elements[1]
            .elements[0].elements[0].elements[0].elements[0].elements[0].text
        ).to.equal("something");
      });

    });

  });
});

and my task

/// <reference types="cypress" />

/**
 * @type {Cypress.PluginConfig}
 */

module.exports = (on, config) => {

  on('task', {
    XMLtoJSON(XML_body) {
      var convert = require("xml-js");
      let result = convert.xml2js(XML_body, { compact: false, spaces: 2 });
      return result;
    },
  });
};

Using debugger just before the const definition I can see that the variables are undefineddebbuger output

I do know about cy.get(), but I just wanted to learn how to use the this.* pattern.

Aucun commentaire:

Enregistrer un commentaire