nutritious-honey-65632
02/22/2023, 11:54 AMCypress.Commands.add('getSomeDataById', (dataId, userId) => {
cy.request('GET', '/myEndpoint')
.its('body')
.then((body) => {
body.data.forEach((data) => {
if (data.id === dataId && data.userId === userId) {
cy.wrap(data).as('data');
}
});
});
});
then in the next command I have:
Cypress.Commands.add(
'someOtherCustomCommandForCreation', () => {
cy.get('@data').then((data) => {
// For this data do that
});
},
);nutritious-honey-65632
02/22/2023, 12:14 PMCypress.Commands.add(
'someOtherCustomCommandForCreation', (dataId, userId) => {
cy.getSomeDataById(dataId, userId).then((data) => {
// For this data do that
});
},
);high-holiday-75305
02/22/2023, 2:26 PMhigh-holiday-75305
02/22/2023, 2:27 PM.cy.ts files to set / get aliases. It feels similar to a local vs. global variables sort of pattern. Aliases should be used like local variables for subjects in a single Cypress test, not globals passed around to many different places.late-planet-4481
02/22/2023, 3:00 PM{ prevSubject: true } on the second command, then chain them together. But I do realize this is not always convenient. In my case we have multiple commands that make use of an object that we build up as the test progresses, which we alias. (We collect data during the test, then do a lot of assertions on a confirmation page at the end of the test.) A global variable will work too, and maybe some clever architecture will get you there.high-holiday-75305
02/22/2023, 3:07 PMhigh-holiday-75305
02/22/2023, 3:07 PM{ prevSubject: true } on the second command should be your first choice by a long shot.late-planet-4481
02/22/2023, 3:13 PMhigh-holiday-75305
02/22/2023, 3:16 PMcy.testStep command and some {before,after}Each hooks to flush that state to the report file on disk than to have the test author juggle the underlying report data at all. I don't recall offhand though when/if any global variable state in a command file is re-loaded by the Cypress runtime. Gotta be careful not to leak across tests.high-holiday-75305
02/22/2023, 3:17 PMbeforeEach hook so I just reset the state therehigh-holiday-75305
02/22/2023, 3:17 PMlate-planet-4481
02/22/2023, 3:18 PMhigh-holiday-75305
02/22/2023, 3:18 PMlate-planet-4481
02/22/2023, 3:18 PMhigh-holiday-75305
02/22/2023, 3:18 PMhigh-holiday-75305
02/22/2023, 3:19 PMhigh-holiday-75305
02/22/2023, 3:19 PMhigh-holiday-75305
02/22/2023, 3:20 PMlate-planet-4481
02/22/2023, 3:21 PMlate-planet-4481
02/22/2023, 3:21 PMhigh-holiday-75305
02/22/2023, 3:22 PMhigh-holiday-75305
02/22/2023, 3:22 PMlate-planet-4481
02/22/2023, 3:25 PMe2e.js, in a beforeEach() 😄nutritious-honey-65632
02/22/2023, 4:45 PMnutritious-honey-65632
02/22/2023, 4:49 PMcy.wrap(false).as('alertFound'); luckily only on one place. And most of my conditional approach comes from neediness to restore to some previous state. But if something goes wrong then some states are not changed and there are no leftovers