简体   繁体   中英

Cypress hoverover GUI effect test

I started using cypress nowadays to learn better and deeper. I am faced with a problem that I can't handle. The problem is some kind of GUI effect working with mouse hover. While hovering my mouse on the element I can't see any information change into DOM. There are just::before and::after words appearing. I think I have to solve that problem with some javascript tricks. unfortunately, I am new to javascript and I don't have any idea if you help me I would be very happy. Thank you! (I want to assert in some way that grey background and plus icon is shown or not)

generically look like that

在此处输入图像描述

after the mouse hovers this grey background and plus icon comes

在此处输入图像描述

you can see elements DOM here

在此处输入图像描述

you can see the changes after the mouse hovers element

在此处输入图像描述

You can use the cypress-real-events plugin. To install use the command:

npm i cypress-real-events

Then inside your cypress/support/e2e.{js,ts} , write:

import "cypress-real-events/support";

And in your code you can directly write:

cy.get("selector").realHover('mouse')
//Add assertions

Note: Since the above plugin uses Chrome DevTools Protocols to simulate native events, hence this will only work with Chromium-based browsers, so no firefox.

Into cypress' test try to use the method cy.wait("time here") following hover command. This is very simple but for visual test this is so useful.

In order to access the :before or :after of an element, we have to do a little Cypress and JavaScript magic. This is answer is mostly from this link.

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');
})

So, using that same example, if we instead wanted to check for say the CSS style of font-size , it would look something like this:

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');
})

This can also be applied to :after , changing out ($els[0], ':before') with ($els[0], ':after') .

Looking at your screenshots, I think you have a gray overlay element covering the original incident element.

The only code I can suggest is from the limited information is

cy.get('#IN-578')
  .trigger('mouseover')
  .should('not.be.visible')

The overlay has opacity 50%, so you can still see the incident details but from Cypress point of view the incident is covered so it will not be "visible".

If you don't have any luck with 'mouseover', try .realHover() from cypress-real-events .


After the above code, you should look for the overlay element in DOM and the + icon in the middle, select that icon and click it to take the action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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