0

I have a function that defines what market we should show to the user depending on their location.

We have different cypress tests for each market. In my cypress tests, I want to invoke the function that defines the market by passing a variable. So essentially:

 const getMarket = (cypressParam: string) => {
  console.log(cypressParam); <--- undefined
  if (window.location.hostname === 'localhost' && cypressParam === "UK") {
    return "UK";
  }
  ...

But how do I do this in my cypress tests? How do I pass a param to the below?

    cy.stub(utils, 'getMarket').withArgs('UK');

The param is undefined

1 Answers1

2

I'm thinking the function must be part of the app, if so one way to communicate your test parameter is via the window object.

const getMarket = () => {
  if (window.location.hostname === 'localhost' && window.cypressParam === "UK") {
    return "UK";
  }

In the test

cy.visit('/', {
  onBeforeLoad: (contentWindow) => {
    contentWindow.cypressParam = 'UK'
  },
})
user16695029
  • 3,365
  • 5
  • 21