Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing tests/helpers need to know if in record or playback mode #8

Open
v-mwalk opened this issue Dec 1, 2022 · 2 comments
Open
Labels
enhancement New feature or request

Comments

@v-mwalk
Copy link

v-mwalk commented Dec 1, 2022

Recording/Playback mode can be set globally or on a spec-by-spec basis (on a test-by-test basis? IE. A Spec having multiple tests but only some in 'record mode? Not tried yet :-) ).

When a test is executing, sometimes it - or its helper code - needs to know when mode the code is executing in. Ideally, a global property should be exposed that can be used anywhere in the test suite code.

IE.

if (cypressReplay.mode === ReplayMode.Recording) then {
  // we are recording
} else {
  / we are not recording
}

A use case for is is in tests we currently have that require manual data entry in 'Recording' mode, but not (uses canned data) in Playback mode. The helper code that does the data entry needs to know what mode we are running in.

@Sam152
Copy link
Owner

Sam152 commented Dec 1, 2022

Interesting problem.

You're correct, the mode is currently configurable at the spec level (not the test level).

I wonder if the return value of enableCypressReplay could be used for this purpose, the signature could become:

enableCypressReplay(mode: ReplayMode | null = null, config: ReplayConfig = {}): ReplayMode;

Beyond that, a workaround could be simply copying the internal logic, while preserving the option to pass in an argument for controlling the mode:

const modeUsedForMySpec = null; // Optionally configure a mode for the spec.
enableCypressReplay(modeUsedForMySpec);
const resolvedMode = modeUsedForMySpec !== null
    ? modeUsedForMySpec
    : (Cypress.env('REPLAY_RECORD_REQUESTS') ? ReplayMode.Recording : ReplayMode.Replaying);

@v-mwalk
Copy link
Author

v-mwalk commented Dec 1, 2022

Good shout - dunno... Not sure just a return value would help as that would mean the caller would be responsible for passing to any interested 'helper' code or making it available globally for 'helper' code to look up.

I'm gonna play with Cypress hooks to see if that can be leveraged to allow mode setting per-test (A spec may have a few tests and only one may need re-recording - so test-level mode setting quite important).

As the workaround, I just wrapped it and watched what mode was being set (We never do a global record and so setting the mode is only at the test level):-

import enableCypressReplay, { ReplayMode as Mode } from 'cypress-replay';
export class Replay {
  private static isCurrectlyRecording: Mode | undefined = undefined;

  public static isRecording(): boolean {
    return (Replay.isCurrectlyRecording === Mode.Recording);
  }

  public static setRecordMode(mode: Mode) {
    enableCypressReplay(mode);
    Replay.isCurrectlyRecording = mode;
  }
}

export namespace Replay {
  export const ReplayMode = { ...Mode };
  export type ReplayMode = typeof ReplayMode;
}

@Sam152 Sam152 added the enhancement New feature or request label Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants