英文:
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'
},
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论