Cypress testing pseudo CSS class :before

后端 未结 4 1273
我在风中等你
我在风中等你 2021-02-15 09:50

Is there a way in which I can test the content of the pseudo CSS class for :before on my element with Cypress?

I have seen links documenting:

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-15 10:26

    This was my solution to get, convert and compare a hexadecimal's background-color with a rgb returned.

    const RGBToHex = (rgbColor) => {
      // it parse rgb(255, 13, 200) to #fa92D4
      const [red, green, blue] = rgbColor.replace(/[a-z]|\(|\)|\s/g, '').split(',');
      let r = parseInt(red, 10).toString(16);
      let g = parseInt(green, 10).toString(16);
      let b = parseInt(blue, 10).toString(16);
    
      if (r.length === 1) r = `0${r}`;
      if (g.length === 1) g = `0${g}`;
      if (b.length === 1) b = `0${b}`;
    
      return `#${r}${g}${b}`;
    };
    
    
    cy.get('.element').then(($el) => {
      const win = $el[0].ownerDocument.defaultView;
      const before = win.getComputedStyle($el[0], 'before');
      const bgColor = before.getPropertyValue('background-color');
      expect(RGBToHex(bgColor)).to.eq('#HEXA');
    });
    

提交回复
热议问题