简体   繁体   English

赛普拉斯不会自动滚动

[英]Cypress does not automatically scroll

I've tried to use scrollIntoView(), scrollTo() methods, tried to nullify css settings, but all were in way.我尝试使用 scrollIntoView()、scrollTo() 方法,尝试使 css 设置无效,但一切都在进行中。 Current situation: When I run test, it gives me this error: (Timed out retrying: cy.type() failed because the center of this element is hidden from view:) When test runs, it cannot scroll down so it cannot see the locator it needs.当前情况:当我运行测试时,它给了我这个错误:(重试超时:cy.type()失败,因为这个元素的中心被隐藏了:)测试运行时,它不能向下滚动,所以它看不到它需要的定位器。 When I scroll manually in the meantime down enough to physically see the element, cypress finds it, gets it and make other methods.当我同时手动向下滚动到足以实际看到元素时,cypress 找到它,获取它并制作其他方法。

Desirable situation: The test would run the way it could automatically scroll down to the page to see the element it needs so no manual assistance would be needed.理想情况:测试将以自动向下滚动到页面以查看所需元素的方式运行,因此不需要手动帮助。

I write down the code here, but changed personal data.我在这里写下代码,但更改了个人数据。 Should i use here code lines like:我应该在这里使用如下代码行:

// document.getElementsByTagName("html")[0].style.scrollBehavior = "unset";

or:或者:

const disableSmoothScroll = () => {
  cy.document().then((document) => {
    const node = document.createElement("style");
    node.innerHTML = "html { scroll-behavior: unset !important; }";
    document.body.appendChild(node);
  });
};

I tried implementing them both, because i've found them in other similar issues, but they did nothing to me.我尝试同时实现它们,因为我在其他类似问题中发现了它们,但它们对我没有任何作用。

import exampleImport from "../../PageObjects/examples/exampleImportClasses.js";

describe("Example Test", function () {
  it("Details1", function () {
    pp.login();
    pp.NPAsubmit();
    cy.get("#name", { timeout: 30000 }).type("Test");
    cy.get("#Number", { timeout: 30000 }).type("123");
    cy.get("#vatNumber", { timeout: 30000 }).type("123");
    pp.Address_default();
  });
});
class exampleImport {
  login() {
    cy.visit("https://xxx");
    cy.get("#email", { timeout: 30000 })
      .clear()
      .type("example@gmail.com");
    cy.get("#password").clear().type("example");
    cy.get("button[type=submit]").click();
    cy.url().should("include", "example");
  }
  NPAsubmit() {
    cy.get(":nth-child(3) > .nav-link", { timeout: 30000 }).click();
    cy.get(".btn-primary").click();
  }
  Address_default() {
    cy.get("#street").scrollIntoView().should("be.visible");
    cy.get("#street").type("Test");

    cy.get("#city").scrollIntoView().should("be.visible");
    cy.get("#city").type("Test");
    cy.get("#country").select("Uruguay (UY)");
  }
}

export default exampleImport;

Thank you for your answer, but i figured it out what was wrong.谢谢你的回答,但我知道出了什么问题。 I disabled smooth scroll in the wrong page so when it redirected, the disableSmoothScroll command didn't work.我在错误的页面中禁用了平滑滚动,因此当它重定向时,disableSmoothScroll 命令不起作用。 Now cypress automatically moves the page down when testing.现在 cypress 在测试时会自动将页面向下移动。

import exampleImport from "../../PageObjects/examples/exampleImportClasses.js";

describe("Example Test", function () {
  it("Details1", function () {
    const disableSmoothScroll = () => {
      cy.document().then((document) => {
        const node = document.createElement("style");
        node.innerHTML = "html { scroll-behavior: inherit !important; }";
        document.body.appendChild(node);
      });
    };
    const pp = new exampleImport();
    pp.login();
    pp.NPAsubmit();
    disableSmoothScroll();
    cy.get("#name", { timeout: 30000 }).type("Test");
    cy.get("#Number", { timeout: 30000 }).type("123");
    cy.get("#vatNumber", { timeout: 30000 }).type("123");
    pp.Address_default();
  });
});

Considering you are submitting a form mid test it is possible the element isn`t fully loaded.考虑到您正在提交表单中间测试,该元素可能未完全加载。 Also type can useforce , so I suggest the following changes: type 也可以使用force ,所以我建议进行以下更改:

describe("Example Test", function () {
  it("Details1", function () {
    pp.login();
    pp.NPAsubmit();
    cy.get("#name", { timeout: 30000 }).should('be.visible').type("Test");
    cy.get("#Number", { timeout: 30000 }).type("123");
    cy.get("#vatNumber", { timeout: 30000 }).type("123");
    pp.Address_default();
  });
});
class exampleImport {
  login() {
    cy.visit("https://xxx");
    cy.get("#email", { timeout: 30000 })
      .clear()
      .type("example@gmail.com");
    cy.get("#password").clear().type("example");
    cy.get("button[type=submit]").click();
    cy.url().should("include", "example");
  }
  NPAsubmit() {
    cy.get(":nth-child(3) > .nav-link", { timeout: 30000 }).click();
    cy.get(".btn-primary").click().should('not.exist'); //Or some other assertion you can make for the form to have completed the action
  }
  Address_default() {
    cy.get("#street").scrollIntoView().should("be.visible");
    cy.get("#street").type("Test", {force:true});

    cy.get("#city").scrollIntoView().should("be.visible");
    cy.get("#city").type("Test", {force:true});
    cy.get("#country").select("Uruguay (UY)");
  }
}

export default exampleImport;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM