Purpose

The gotoHelper utility centralizes navigation behavior for page objects that support multiple ways of reaching the same page.

Current Behavior

It creates a goto() function with these variants:

  • goto() uses the configured default navigation method

  • goto.viaMenu() forces menu-based navigation

  • goto.viaDirectLink() forces direct-link navigation

  • goto.random() intentionally randomizes between menu-based and direct-link navigation

Example Usage

import { createGotoWithVariants } from '../utils/gotoHelper';

this.goto = createGotoWithVariants(
  async () => await this.gotoViaSidebarMenu(),
  async () => await this.gotoViaDirectLink()
);

By default in this repo, plain goto() is now used as the deterministic path and randomized navigation is called explicitly with goto.random().

Important Note

The current testing pattern is:

  • tests/navigation.spec.ts explicitly verifies both navigation methods

  • feature specs can use goto.random() when navigation is only setup and not the main behavior under test

  • a few specs can still keep deterministic setup first when they are meant to demonstrate that pattern clearly

Source Code

// utils/gotoHelper.ts

export type GotoMethod = 'viaMenu' | 'viaDirectLink';

type CreateGotoOptions = {
  defaultMethod?: GotoMethod;
};

export type GotoVariants = {
  (): Promise<void>;
  viaMenu: () => Promise<void>;
  viaDirectLink: () => Promise<void>;
  random: () => Promise<void>;
};

export function createGotoWithVariants(
  viaMenuFn: () => Promise<void>,
  viaDirectLinkFn: () => Promise<void>,
  options: CreateGotoOptions = {}
): GotoVariants {
  const defaultMethod = options.defaultMethod ?? 'viaDirectLink';

  const goto: GotoVariants = Object.assign(
    async () => goto[defaultMethod](),
    {
      viaMenu: viaMenuFn,
      viaDirectLink: viaDirectLinkFn,
      random: async () => {
        const useMenu = Math.random() > 0.5;
        return useMenu ? goto.viaMenu() : goto.viaDirectLink();
      }
    }
  );

  return goto;
}