英文:
Getting an error using Cypress.Commands.overwriteQuery
问题
我在覆盖contains()
方法时遇到了这个错误。
无法读取未定义的属性(读取“hasPreviouslyLinkedCommand”)在Cypress中
这是commands.js
文件:
Cypress.Commands.add("clickOnLinks", (linkText) => {
cy.get("a").contains(linkText).click();
});
// 覆盖 contains()
// 为 contains 方法传递相同的参数
Cypress.Commands.overwriteQuery(
"contains",
(originalFn, subject, filter, text, options = {}) => {
// 确定是否传递了 filter 参数
if (typeof text === "object") {
options = text;
text = filter;
filter = undefined;
}
options.matchCase = false;
return originalFn(subject, filter, text, options);
}
);
这是我的测试文件:
describe("自定义命令", () => {
it("覆盖自定义命令", () => {
cy.visit("https://magento.softwaretestingboard.com/");
// 调用 clickOnLinks 命令单击链接
cy.clickOnLinks("RADIANT TEE").should("exist");
// 执行单击操作后进行验证
cy.get("span[class='base']").should("have.text", "Radiant Tee");
});
});
我正在使用最新版本的Cypress - 12.7.0
我也尝试使用这个,但没有成功 -
Cypress.Commands.overwriteQuery
我漏掉了什么?有人可以帮忙吗?
英文:
I am getting this error while overwriting the contains()
method.
> Cannot read properties of undefined (reading 'hasPreviouslyLinkedCommand') in cypress
Here is the commands.js
file:
Cypress.Commands.add("clickOnLinks", (linkText) => {
cy.get("a").contains(linkText).click();
});
// overwriting contains()
// passing the same parameter for the contains method
Cypress.Commands.overwriteQuery(
"contains",
(originalFn, subject, filter, text, options = {}) => {
// determine if the filter argument was passed
if (typeof text === "object") {
options = text;
text = filter;
filter = undefined;
}
options.matchCase = false;
return originalFn(subject, filter, text, options);
}
);
And this is my test file:
describe("Custom command", () => {
it("overwriting the custom commands", () => {
cy.visit("https://magento.softwaretestingboard.com/");
// calling clickOnLinks command to click on the links
cy.clickOnLinks("RADIANT TEE").should("exist");
// verifying after performing click action
cy.get("span[class='base']").should("have.text", "Radiant Tee");
});
});
I'm using the latest version of cypress - 12.7.0
I have tried using this one as well but no luck -
Cypress.Commands.overwriteQuery
What am I missing? Can anyone help?
答案1
得分: 4
主要问题是originalFn
的this
绑定未设置,可以通过originalFn.bind(this)
来解决。
我从源代码中获取了参数命名和解析,并将选项(即userOptions)默认为{}
,以防它们未传递。
有一个奇怪的地方-prevSubject
不再存在。这可能发生在contains
从命令更改为查询时,但我还没搞清楚它是如何获取prevSubject的。
无论如何,这通过cy.get('a').contains('RADIANT TEE')
测试通过。
我还没有测试其他变体。
英文:
The main problem is the this
binding of originalFn
not being set, can be fixed with originalFn.bind(this)
.
I took the parameter naming and resolution from the source, and defaulted the options (aka userOptions) to {}
in case they are not passed.
There is a weird thing - the prevSubject
is no longer present. That might have happened when contains
changed from a command to a query, but haven't figured out how it get's the prevSubject(s) yet.
In any case, this is passing with cy.get('a').contains('RADIANT TEE')
.
I haven't tested other variations yet.
Cypress.Commands.overwriteQuery(
"contains",
function (contains, filter, text, userOptions = {}) {
// This is parameter resolution from Cypress v12.7.0 source
if (Cypress._.isRegExp(text)) {
// .contains(filter, text)
// Do nothing
} else if (Cypress._.isObject(text)) {
// .contains(text, userOptions)
userOptions = text
text = filter
filter = ''
} else if (Cypress._.isUndefined(text)) {
// .contains(text)
text = filter
filter = ''
}
userOptions.matchCase = false;
let contains0 = contains.bind(this) // this line fixes the error
return contains0(filter, text, userOptions)
}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论