What if your tests could spot a wrong button color faster than your eyes ever could?
Why CSS assertions in Cypress? - Purpose & Use Cases
Imagine you have a website with buttons that should be blue and text that should be bold. You open the site and check each element's style by eye, one by one, every time you make a change.
This manual checking is slow and tiring. You might miss a color change or a font style mistake because your eyes get tired or you forget to check some parts. It's easy to make mistakes and hard to keep track of all style rules.
CSS assertions let you write automatic tests that check if elements have the right colors, fonts, or sizes. These tests run fast and never get tired, so you catch style mistakes immediately without looking manually.
Open browser > Look at button > Guess if color is blue > Repeat for all elements
cy.get('button').should('have.css', 'color', 'rgb(0, 0, 255)')
With CSS assertions, you can trust your website always looks right, even after many changes, without spending hours checking styles yourself.
A designer updates the brand colors. Instead of checking every page by hand, the QA runs CSS assertion tests that quickly find any button or text still using old colors.
Manual style checks are slow and error-prone.
CSS assertions automate style verification reliably.
This saves time and ensures consistent website appearance.
Practice
cy.get('button').should('have.css', 'color', 'rgb(255, 0, 0)') check?Solution
Step 1: Understand the CSS property being checked
The command checks the CSS property 'color', which controls the text color of the element.Step 2: Interpret the value 'rgb(255, 0, 0)'
This RGB value represents the color red.Final Answer:
It checks if the button's text color is red. -> Option BQuick Check:
CSS property 'color' = text color [OK]
- Confusing 'color' with 'background-color'
- Assuming it checks visibility instead of style
- Mixing RGB values with hex codes incorrectly
Solution
Step 1: Identify the correct assertion method
The correct Cypress assertion to check CSS isshould('have.css', property, value).Step 2: Match the syntax with the options
cy.get('p').should('have.css', 'font-size', '16px') usesshould('have.css', 'font-size', '16px'), which is correct syntax.Final Answer:
cy.get('p').should('have.css', 'font-size', '16px') -> Option DQuick Check:
Useshould('have.css')for CSS assertions [OK]
- Using 'should' without 'have.css'
- Using 'assert' which is not a Cypress command
- Confusing 'have.style' with 'have.css'
cy.get('.alert').should('have.css', 'background-color', 'rgb(255, 0, 0)')Solution
Step 1: Understand the expected CSS value
The test expects the background color to be 'rgb(255, 0, 0)', which is red.Step 2: Compare with actual CSS value
The actual background color is 'rgb(0, 128, 0)', which is green, so it does not match.Final Answer:
The test will fail because the background color does not match 'rgb(255, 0, 0)'. -> Option AQuick Check:
CSS value mismatch causes test failure [OK]
- Assuming any color presence passes the test
- Confusing RGB values for different colors
- Expecting Cypress to auto-correct CSS mismatches
cy.get('#header').should('have.css', 'font-weight', 700)Solution
Step 1: Check the type of CSS value in assertion
Cypress expects the CSS value as a string, so 700 should be '700'.Step 2: Verify other parts of the command
The selector '#header' is valid, and 'should' with 'have.css' is correct syntax.Final Answer:
The font-weight value should be a string: '700'. -> Option AQuick Check:
CSS values must be strings in assertions [OK]
- Passing numbers instead of strings for CSS values
- Changing valid selectors unnecessarily
- Confusing assertion methods in Cypress
.submit-btn has a border radius of 5 pixels and a background color that is not transparent. Which Cypress code correctly asserts both styles?Solution
Step 1: Assert border-radius with correct value and type
Useshould('have.css', 'border-radius', '5px')to check border radius as a string with units.Step 2: Assert background-color is not transparent
Since 'transparent' can be represented as 'rgba(0, 0, 0, 0)', use a callback assertion to check the value is not equal to that.Step 3: Review options for correctness
cy.get('.submit-btn').should('have.css', 'border-radius', '5px').and('have.css', 'background-color').should(value => expect(value).not.to.equal('rgba(0, 0, 0, 0)')) uses a callback withshouldandexpectto verify background-color is not transparent, which is correct.Final Answer:
cy.get('.submit-btn').should('have.css', 'border-radius', '5px').and('have.css', 'background-color').should(value => expect(value).not.to.equal('rgba(0, 0, 0, 0)')) -> Option CQuick Check:
Use callback assertions for negative CSS checks [OK]
- Passing numeric values without units for CSS properties
- Trying to chain 'not' directly after 'have.css' without callback
- Checking for exact transparent string instead of rgba value
Ready to prove it?
Pass the test and this pattern moves unknown
