in src/helpers/generator.ts [63:114]
export function generateMergedIR(prevSteps: Steps, pwInput: Steps): Steps {
const nextSteps: Steps = pwInput.map(step => ({
...step,
actions: step.actions.filter(
({ action: { name }, frame: { pageAlias } }) => !isOpenOrCloseAction(name, pageAlias)
),
}));
const prevLength = prevSteps.reduce(getActionCount, 0);
const nextLength = nextSteps.reduce(getActionCount, 0);
/**
* when recorder is started/reset
*/
if (prevLength === 0 || nextLength === 0) {
return nextSteps;
}
const mergedSteps: Steps = [];
let pwActionCount = 0;
for (const step of prevSteps) {
const actions: ActionInContext[] = [];
for (const action of step.actions) {
if (action.action.name === 'assert') {
/**
* Keep adding all the assertions added by user as PW
* does not have any assertion built in
* We treat the UI as the source of truth
*/
actions.push(action);
} else {
/**
* If actions are not assert commands, then we need to
* check if the actions are modified on the UI and add
* them to the final list
*
* Any modified state in the UI is the final state
*/
const item = action?.modified ? action : nextSteps[0].actions[pwActionCount];
actions.push(item);
pwActionCount++;
}
}
mergedSteps.push({ actions });
}
/**
* Append any new Playwright actions to the final step
*/
const lastStep = mergedSteps[mergedSteps.length - 1];
nextSteps[0].actions
.filter((_, index) => index >= pwActionCount)
.map(action => lastStep.actions.push(action));
return mergedSteps;
}