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

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

ReferenceError : $el is not defined in cypress []

问题

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

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

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

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.

describe("Interacting with dropdowns", function () {
  it("Auto Suggest Dynamic Dropdown: We need to write function", function () {
    cy.visit("https://www.google.com/");
    cy.wait(2000);
    cy.get(".gLFyf").type("Automation Testing");
    cy.wait(5000);
    cy.get(".wM6W7d>span").should("have.length", "12");
    cy.get(".wM6W7d>span").then((result) => {
      for (const item of result) {
        if (item.innerText == "automation testing tutorial") {
          cy.get(`.${item.offsetParent.className}`).click();
        }
      }
      cy.wait(2000);
      cy.contains(`Automation Testing`);
    });
  });
});

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:

确定