![](/img/trans.png)
[英]Why does Cypress test in GUI(cypress open) pass but fails in command line(cypress run) even though I using chrome browser?
[英]Cypress hoverover GUI effect test
您可以使用cypress-real-events插件。 要安装使用命令:
npm i cypress-real-events
然后在你的cypress/support/e2e.{js,ts}
里面,写:
import "cypress-real-events/support";
在您的代码中,您可以直接编写:
cy.get("selector").realHover('mouse')
//Add assertions
注意:由于上述插件使用 Chrome DevTools 协议来模拟原生事件,因此这仅适用于基于 Chromium 的浏览器,因此没有 firefox。
进入赛普拉斯的测试尝试在 hover 命令之后使用方法cy.wait("time here")
。 这非常简单,但对于视觉测试来说非常有用。
为了访问元素的:before
或:after
,我们必须做一点 Cypress 和 JavaScript 魔术。 这个答案主要来自这个链接。
cy.get('.myClass')
.then($els => {
// get Window reference from element
const win = $els[0].ownerDocument.defaultView
// use getComputedStyle to read the pseudo selector
const before = win.getComputedStyle($els[0], ':before')
// read the value of the `content` CSS property
const contentValue = before.getPropertyValue('content')
// The above lines are just how we tell Cypress to get the `:before` value.
// There isn't a ton to understand outside of really diving into how elements and windows work together
expect(content).to.equal('foo');
})
所以,使用同样的例子,如果我们想检查 CSS 样式的font-size
,它看起来像这样:
cy.get('.myClass')
.then($els => {
const win = $els[0].ownerDocument.defaultView
const before = win.getComputedStyle($els[0], ':before')
const fontSize = before.getPropertyValue('font-size')
expect(fontSize).to.equal('20');
})
这也可以应用于:after
,将($els[0], ':before')
更改为($els[0], ':after')
。
查看您的屏幕截图,我认为您有一个覆盖原始事件元素的灰色叠加元素。
我可以建议的唯一代码来自有限的信息是
cy.get('#IN-578')
.trigger('mouseover')
.should('not.be.visible')
叠加层的不透明度为 50%,因此您仍然可以看到事件详细信息,但从赛普拉斯的角度来看,事件已被覆盖,因此不会“可见”。
如果您对“鼠标悬停”没有任何运气,请尝试cypress-real-events中的.realHover()
。
在上面的代码之后,您应该在 DOM 中查找覆盖元素和中间的+
图标,select 那个图标并单击它以执行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.