首页 > 解决方案 > Cypress:如何在悬停时定位元素?

问题描述

我有一个“反应”应用程序,#controlsBar当 mouseover 时应该可见#video-container,但以下代码失败

    cy.get("#controlsBar").should("be.hidden");
    cy.get("#video-container")
      .trigger("mouseover")
      .then((elem) => {

        cy.get(elem).children().eq(3).should("be.visible"); //this is #controlsBar
        //cy.get("#controlsBar").should("be.visible"); //this failed also 
        cy.get("#controlsBar").invoke("show");
      });

标签: cypress

解决方案


我建议您使用cypress-real-events包。

  1. 要安装使用npm install cypress-real-events.

  2. 然后在cypress/support/index.js文件下写入import "cypress-real-events/support";

  3. 然后在你的测试中你可以写:

cy.get("#video-container").realHover()

推荐阅读