Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cypress: create object and re use object values in the tests

I am new to Cypress. I would appreciate any tips and help. My test:

  1. Go to the page
  2. parse Response from the server
  3. take any item from that response
  4. and then I want to save that item into an object for later use in the tests.
cy.wait('@pageLoaded').then(({ response }) => {
            expect(response.statusCode).to.equal(200);
            
            let i = randomItem();
            
            //Create an object
            const Item = {
                Id: response.body.items[i].id,
                IpuCode: response.body.items[i].ipuCode,
                Description: response.body.items[i].description,
                PackSize: response.body.items[i].packSize,
                PackType: response.body.items[i].packType,
            };
            
           //Here I'm getting the expected array
            cy.log(Item);
            
        })
        //And here I want to re-use it
        
        cy.addItemToShoppingCart(Item.value(IpuCode), pharmacyId, Item.value(Id), currentDateTime);


So, I have tryed

cy.addItemToShoppingCart(Item.value(IpuCode), pharmacyId, Item.value(Id), currentDateTime);

Th result: Item is not defined

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

OR

 cy.addItemToShoppingCart(Item.IpuCode, pharmacyId, Item.Id, currentDateTime);

The result: Item is not defined

I also, have working way to get values and use it

            cy.wait('@pageLoaded').then(({ response }) => {
                expect(response.statusCode).to.equal(200);
                
                let i = randomItem();
                let ItemGmsCode = response.body.items[i].gmsCode;
                cy.log(ItemGmsCode);
                cy.wrap(ItemGmsCode).as('itemGmsCode');
            })

But I don’t like that every time, I have to wrap the command in a block

            cy.get('@itemGmsCode').then((gmsCode) => {
                //do soemthing
            })

Or do I want the impossible?

>Solution :

The built-in mechanism for saving data within a test is the alias which you would use in your code like this

cy.wait('@pageLoaded').then(({ response }) => {
  ...
  cy.wrap({
    id: response.body.items[i].id,
    ipuCode: response.body.items[i].ipuCode,
    description: response.body.items[i].description,
    packSize: response.body.items[i].packSize,
    packType: response.body.items[i].packType,
  }.as('item')
})

// later in the same test

cy.get('@item').then(item => {
  cy.addItemToShoppingCart(item.ipuCode, pharmacyId, item.id, currentDateTime)
})

In Javascript the convention is to use camel case, which means the first char of an identifier is lowercase.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading