I want to update the time in my fixture to the current time everytime before running tests and feed that fixture file with the updated time as a response in the cy.intercept(). But everytime cy.intercept() loads data from the original file (Not with the updated time) and then writes the fixture file with the updated time. How do I make sure that the fixture file is updated first and then intercept call?
describe(‘NBA pregame EventTile verification’, () => {
beforeEach(‘Enforce game start time’, function () {
// Function to read and update schedule file, returning a promise
const updateSchedule = (filePath, updateCallback) => {
return cy.fixture(filePath).then((nbaData) => {
updateCallback(nbaData);
return cy.writeFile(filePath, nbaData);
});
};
// Update QB schedule and set up interception
const updateQBAndIntercept = () => {
return updateSchedule('Odds/QB/NBA1Schedule.json', (nbaData) => {
const nbaGame = nbaData.NBA[0];
const currentTimestamp = Date.now();
const adjustedStartTime = currentTimestamp + (2 * 60 * 60 * 1000); // 2 hours ahead
nbaGame.startTime = adjustedStartTime;
}).then(() => {
return cy.intercept('GET', '**/quarterback/lighterGames/leagues/**', {
fixture: 'Odds/QB/NBA1Schedule.json'
}).as('nbaPregameqb');
});
};
// Update DK schedule and set up interception
const updateDKAndIntercept = () => {
return updateSchedule('Odds/DK/NBA1Schedule.json', (nbaData) => {
const nbaGame = nbaData.NBA[0];
const currentDate = new Date();
const adjustedStartDate = new Date(currentDate.getTime() + (2 * 60 * 60 * 1000)); // 2 hours ahead
nbaGame.startDate = adjustedStartDate.toISOString();
}).then(() => {
return cy.intercept('GET', '**/sports-oddsapp-offers.xreapps.net/dk/events/**', {
fixture: 'Odds/DK/NBA1Schedule.json'
}).as('nbaPregamedk');
});
};
cy.intercept('GET', '**/quarterback/lighterGame/league/**', {
fixture: 'Odds/QB/NBA1Details.json'
}).as('nbaPregameDetailqb');
cy.intercept('GET', '**/sports-oddsapp-offers.xreapps.net/dk/props,lines/**', {
fixture: 'Odds/DK/NBA1Details.json'
}).as('nbaPregameDetaildk');
// Launch the Odds App and wait for intercepts to be set up
const launchAppAndWaitForIntercepts = () => {
cy.launchOddsApp();
return cy.wait(['@nbaPregameqb', '@nbaPregamedk']);
};
// Chain the promises to ensure sequential execution
updateQBAndIntercept()
.then(updateDKAndIntercept)
.then(launchAppAndWaitForIntercepts);
})
context(‘Odds Explorer Page’, () => {
it.only('Should have NBA pregame Tile', () => {
I want to update the time in my fixture to the current time everytime before running tests and feed that fixture file with the updated time as a response in the cy.intercept(). But everytime cy.intercept() loads data from the original file (Not with the updated time) and then writes the fixture file with the updated timecy.writeFile. How do I make sure that the fixture file is updated first and then intercept call?
Kakali Mahapatra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.