通过 cypress 将参数传递给一个函数

huangapple go评论67阅读模式
英文:

Passing a parameter to a function via cypress

问题

以下是翻译好的部分:

我有一个函数,根据用户的位置定义应该向他们展示哪个市场。

我们为每个市场编写了不同的 Cypress 测试。在我的 Cypress 测试中,我想通过传递一个变量来调用定义市场的函数。

因此,基本上:

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

但我该如何在我的 Cypress 测试中执行此操作?如何将参数传递给以下内容?

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

参数是未定义的。

英文:

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 parameter to the following?

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

The parameter is undefined

答案1

得分: 3

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'
  },
})
英文:

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'
  },
})

huangapple
  • 本文由 发表于 2023年4月6日 23:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定