Saving data to a variable and using it in assertion on other page using Cypress

Mario Frohlich
3 min readApr 3, 2023

--

Note: there are probably better ways to achieve this, but this is my take on the problem I’ve faced

So I had the following scenario: create an assertion that verifies that the user can only see the company (school) he’s employed in.

At first, this presented a problem since the data used to assert this is found on two different pages:

  • The Employees (Djelatnici) — holds the information about the school the selected person is working in,
  • The Schools (Škole) — holds the information about all the schools the user has access to, and the user can only see the schools he’s working in

After thinking for a while, I remembered I could try using variables to solve the present issue. I could try to store both values into a variable and then compare those variables to check whether they are the same/similar.

The first step was to go to the Employees (Djelatnici) section and record the value of the Schools (Škole) the targeted person is employed in.

Using the following code I navigated to the Employees (Djelatnici) section and I used the search feature to search for the targeted employee.

/* Navigate to 'Employees (Djelatnici)' */ (1)
cy.get('.ant-menu').find('.ant-menu-item').eq(3).click()

/* Enter the desired value in the search box*/ (2)
cy.get('.search-wrap').find('.search-inputs-box').then (poljaPretrage => {
cy.wrap(poljaPretrage).find('input[name="tekst"]').click().type('školski')
})

/* Verify that the obtained result truly is the one I'm looking for */ (3)
cy.get('.ant-table-body', {timeout: 50000}).find('td').filter(':visible')
.then(items => { expect(items).to.contain('Školski')
})
First, we navigate to the Employees (Djelatnici) (1), and then the search bar (2) is used to look for the desired person. The last step is to check whether the desired results are visible in the app (3)

Once I was sure only the targeted employee is visible, I click on the result to open the details page from which I will take the desired value and save it to the variable (wags)

/* Click on the result obtained */ (3)
cy.get('.ant-table-body').contains(/školski/i).click()

/* Get the value and store it to the 'wags' variable */
cy.wrap(djelatnikŠkola).find('.info-box-row-right').then(message => {
let wags = message;
cy.wrap(wags).as('wags')
Once it was confirmed that the targeted person is visible, we click on it to open its details. From the details, I store the value (marked red) to a variable.

The next step is to navigate to the Schools (Škole) page and to store the visible value to another variable (wags2). This will be used in the last step to compare the values stored in both variables.

We navigate to the Schools (Škole) page and to store the visible value (marked red) to a variable (wags2)
/* Navigate to the Schools (Škole) page */
cy.get('.ant-menu').find('.ant-menu-item').eq(1).click()

/* Get the value and store it to a variable (wags2) */
cy.get('.ant-table-body', {timeout: 50000}).find('.ant-table-cell')
.eq(2).then(message2 => {
let školeVidljivaŠkola = message2.text()
cy.wrap(školeVidljivaŠkola).as('wags2')
})

The last thing left to do is to compare the values of both variables

/* I initiated both variables so that their values can be compared */
cy.get('@wags2').then(školeVidljivaŠkola => {
cy.get('@wags').then(djelatnikDetaljŠkola => {
cy.expect(djelatnikDetaljŠkola).to.contain(školeVidljivaŠkola)
})

})

Since one value contains the other, as seen above, the goal was achieved — values from different parts of the webpage were successfully asserted. Furthermore, these two resources gave me the initial inspiration to try this:

--

--

Mario Frohlich

It's a me, Mario! I'm a manual software tester that is expanding into automation. I also like beer