Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Make the random decision controllable in your test, then assert the chosen experience with a condition-based check that waits for the page to reach the expected state. Use a separate deterministic case for each important variant, and control other moving parts—such as experiment assignment, data, network responses, browser state, and time—that could make the result unpredictable.

First identify what chooses the experience

Before changing Math.random(), trace the decision that determines what the user sees. A client-side draw, a server-selected variant, a feature flag, and an experiment assignment are different test inputs. Control the input at the point where the experience is actually selected.

  • Client-side random choice: supply a known random value through an application seam, or override the browser random function for the test.
  • Server-side or experiment assignment: configure a fixture or stubbed response to return the intended assignment. Replacing browser Math.random() will not control a choice already made on the server.

For experiments, use a stable way to select or identify the assignment, such as a test-controlled cookie, local-storage value, or response field. Cypress cautions against branching test logic based on DOM content that may still be changing; control the campaign or expose a reliable assignment instead (Cypress guidance on conditional testing).

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Force the case you need instead of waiting for chance

Do not keep drawing random values until the desired branch appears. That makes the test’s runtime and outcome depend on chance. Give each important user-visible variant a deterministic test case, and make that case’s assignment explicit.

Playwright: install a client-side override before page code

Playwright’s page.addInitScript() and browserContext.addInitScript() run setup before application scripts. Its documentation demonstrates replacing Math.random() with a constant (Playwright: Evaluating JavaScript).

test('shows the assigned variant', async ({ page }) => {
  await page.addInitScript(() => {
    Math.random = () => 0.25;
  });

  await page.goto('/experience');
  await expect(page.getByRole('heading', { name: 'Variant A' })).toBeVisible();
});

Adapt the expected value and assertion to the application’s actual selection logic. If values in the interval map to different outcomes, choose a value that intentionally exercises the desired branch. A global override can affect unrelated code that also uses Math.random(), so keep it scoped to the test and avoid relying on it for server-side decisions.

Playwright test-runner code and page code run in separate environments. Pass values into page.evaluate() explicitly rather than assuming variables declared in the test are available in the browser (Playwright: Evaluating JavaScript).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Cypress: control the assignment or stub the deciding input

For a server- or campaign-assigned experience, have the test fixture or controlled endpoint return the intended assignment. When the choice is made in browser code, arrange a test seam for that input and check the resulting page state with Cypress’s retrying assertions. Cypress recommends a reliable assignment source rather than conditional paths based on a mutable DOM (Cypress guidance on conditional testing).

Assert what the user sees, not how long the page has had

After navigation or an interaction, assert the expected visible result with a semantic locator, such as a role and accessible name. Playwright’s web-first assertions wait for the condition to become true; Cypress assertions retry until the expected state appears or the command fails (Playwright Best Practices; Cypress: Introduction to Cypress).

A fixed sleep does not establish that the intended variant has rendered. It can waste time when the page is ready early and still fail when rendering takes longer than expected. Express the condition you need instead of waiting an arbitrary number of milliseconds.

Rank #4
The Web Testing Handbook
  • Used Book in Good Condition

Prefer assertions about user-facing content or behavior over private implementation details such as a function name or CSS class. That keeps the test focused on whether the experience works for a user (Playwright Best Practices).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep other sources of variation under control

A deterministic random input cannot prevent flakiness caused by changing test data, external services, or state carried over from another run. Use a controlled test environment for the dependencies that affect the rendered experience.

  • Data: seed the records a case needs and reset them between tests. Cypress describes seeding and resetting application data as part of effective end-to-end testing (Cypress: Effective E2E testing).
  • Network dependencies: stub or route responses when an external service could change or delay the result. Playwright recommends controlling network activity and data where needed (Playwright Best Practices); Cypress also supports stubbing network requests (Cypress: Effective E2E testing).
  • Browser state: keep cookies, local storage, and session storage from leaking between scenarios. Playwright Test creates an isolated browser context for each test, with a separate browser profile (Playwright: Isolation).
  • Time: if a countdown, schedule, or timer changes the experience, control the clock instead of waiting in real time. Playwright’s clock API can set time or advance timers (Playwright: Clock).

A local test server is useful when tests need to seed records, reset server or database state, or expose test-specific routes. Deployed-environment checks can still provide a smaller smoke-test layer, but they usually offer less control over those dependencies (Cypress: Effective E2E testing).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Separate UI-branch tests from randomization checks

Forcing an outcome verifies that the selected UI branch behaves correctly. It does not verify that production assignment logic chooses variants fairly, at a particular rate, or according to an experiment’s rules. If those properties matter, test the assignment mechanism separately against its requirements. The cited framework guidance shows how to force deterministic scenarios; it does not prescribe a universal statistical validation method (Playwright: Evaluating JavaScript).

Likewise, a passing branch test does not establish that a live campaign or server will assign that experience correctly. Keep the purpose of each test explicit: deterministic browser cases check what users see under a chosen assignment; separate checks cover the assignment contract or other required properties.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical test-design checklist

  1. Trace the actual decision point: browser random draw, server response, feature flag, or experiment assignment.
  2. Choose a test seam at that point and make the desired case explicit.
  3. Create a deterministic scenario for each important user-visible variant.
  4. Assert the resulting content or behavior with a semantic locator and retrying assertion.
  5. Seed or reset test data, control relevant network responses, and isolate browser state.
  6. Fix or advance time if timers affect the experience.
  7. Add a separate check for assignment logic or distribution requirements if those are part of the product’s correctness criteria.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.