ReferenceError: $el在Cypress中未定义[]

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

ReferenceError : $el is not defined in cypress []

问题

  1. ///\<reference types = "cypress"/\>
  2. ///\<reference types = "cypress-xpath"/\>
  3. describe("与下拉菜单交互", function () {
  4. it("自动建议动态下拉菜单:我们需要编写函数", function () {
  5. cy.visit("https://www.google.com/");
  6. cy.wait(2000);
  7. cy.get(".gLFyf").type("自动化测试");
  8. cy.wait(5000);
  9. cy.get(".wM6W7d>span").should("have.length", "12");
  10. cy.get(".wM6W7d>span").each(($el, index, $list) => {
  11. if ($el.text() == "自动化测试教程") {
  12. cy.wrap($el).click();
  13. }
  14. });
  15. cy.get(".gLFyf").should("have.value", "自动化测试");
  16. });
  17. });

在执行时,我遇到了"ReferenceError",无法在Cypress中执行代码,并且出现了"$el未定义"的错误。

英文:
  1. ///\<reference types = "cypress"/\>
  2. ///\<reference types = "cypress-xpath"/\>
  3. describe("Interacting with dropdowns", function () {
  4. it("Auto Suggest Dynamic Dropdown: We need to write function", function () {
  5. cy.visit("https://www.google.com/");
  6. cy.wait(2000);
  7. cy.get(".gLFyf").type("Automation Testing");
  8. cy.wait(5000);
  9. cy.get(".wM6W7d>span").should("have.length", "12");
  10. cy.get(".wM6W7d>span").each(($el, index, $list), function () {
  11. if ($el.text() == "automation testing tutorial") {
  12. cy.wrap($el).click();
  13. }
  14. });
  15. cy.get(".gLFyf").should("have.value", "Automation Testing");
  16. });
  17. });

While execution I am getting referenceerror.Unable to execute the code in cypress and also getting the "ReferenceError" i.e., $el is not defined .

答案1

得分: 1

你可以尝试运行这段代码,并根据你的需要进行更新。

以下是结果的快照:

ReferenceError: $el在Cypress中未定义[]

在这里,借助这个方法的帮助,我们检索了结果并根据匹配条件执行了点击事件。

我相信每个人都不能正确地找到可迭代对象。
有关每个的更多详细信息,请查看文档链接:

https://docs.cypress.io/api/commands/each

希望这有所帮助!
祝愉快编码 ReferenceError: $el在Cypress中未定义[]

英文:

You can try out this code and update it according to your need.

  1. describe("Interacting with dropdowns", function () {
  2. it("Auto Suggest Dynamic Dropdown: We need to write function", function () {
  3. cy.visit("https://www.google.com/");
  4. cy.wait(2000);
  5. cy.get(".gLFyf").type("Automation Testing");
  6. cy.wait(5000);
  7. cy.get(".wM6W7d>span").should("have.length", "12");
  8. cy.get(".wM6W7d>span").then((result) => {
  9. for (const item of result) {
  10. if (item.innerText == "automation testing tutorial") {
  11. cy.get(`.${item.offsetParent.className}`).click();
  12. }
  13. }
  14. cy.wait(2000);
  15. cy.contains(`Automation Testing`);
  16. });
  17. });
  18. });

Here is the snap of the result:

ReferenceError: $el在Cypress中未定义[]

Here with the help of the method, we retrieved the result and executed the click event based on the matching condition.

I believe each is not able to properly find the iterable.
For more detail regarding each, please go through the documentation link :

https://docs.cypress.io/api/commands/each

Hope this helps!
Happy coding ReferenceError: $el在Cypress中未定义[]

huangapple
  • 本文由 发表于 2023年8月10日 14:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873272.html
匿名

发表评论

匿名网友

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

确定