A CSS assertion checks if a web element has a specific CSS property value during a test. It helps verify the style or appearance of elements.
Click to reveal answer
beginner
How do you check the color of a button using Cypress CSS assertions?
Use cy.get('button').should('have.css', 'color', 'rgb(255, 0, 0)') to assert the button's text color is red in RGB format.
Click to reveal answer
intermediate
Why is it better to use should('have.css', ...) instead of checking styles manually?
Because should('have.css', ...) waits for the element to have the style and retries automatically, making tests more reliable and less flaky.
Click to reveal answer
intermediate
What CSS property value format does Cypress expect in assertions?
Cypress expects computed CSS values, usually in formats like rgb() for colors or exact strings for other properties, matching what the browser computes.
Click to reveal answer
beginner
Example: How to assert that a div has a font size of 16 pixels in Cypress?
Use cy.get('div').should('have.css', 'font-size', '16px') to check the font size is exactly 16 pixels.
Click to reveal answer
Which Cypress command checks if an element has a specific CSS property?
Ashould('have.css', property, value)
Bexpect(element).toHaveStyle(property, value)
CcheckCss(property, value)
DassertCss(property, value)
✗ Incorrect
In Cypress, should('have.css', property, value) is the correct way to assert CSS properties.
What format should color values be in for Cypress CSS assertions?
AHexadecimal (#FF0000)
BRGB format (rgb(255, 0, 0))
CNamed colors (red)
DHSL format (hsl(0, 100%, 50%))
✗ Incorrect
Cypress uses the computed style, which usually returns colors in RGB format.
Why does Cypress retry CSS assertions automatically?
ATo ignore CSS errors
BTo slow down tests
CTo wait for animations or style changes to complete
DTo cache CSS values
✗ Incorrect
Cypress retries assertions to wait for dynamic style changes like animations or transitions.
Which CSS property would you check to verify text is bold?
Afont-weight
Bfont-style
Ctext-decoration
Dfont-size
✗ Incorrect
The font-weight property controls boldness of text.
What happens if the CSS property value does not match in a Cypress assertion?
ATest retries forever
BTest passes anyway
CTest skips the assertion
DTest fails with an assertion error
✗ Incorrect
If the CSS value does not match, Cypress fails the test with a clear error.
Explain how to write a CSS assertion in Cypress to check an element's background color.
Think about selecting the element and checking the computed style property.
You got /4 concepts.
Describe why CSS assertions are important in UI testing with Cypress.
Consider how CSS affects user experience and why testing it matters.
You got /4 concepts.
Practice
(1/5)
1. What does the Cypress command cy.get('button').should('have.css', 'color', 'rgb(255, 0, 0)') check?
easy
A. It checks if the button is clickable.
B. It checks if the button's text color is red.
C. It checks if the button has a red background.
D. It checks if the button is visible on the page.
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 B
Quick Check:
CSS property 'color' = text color [OK]
Hint: Check the CSS property name to know what style is tested [OK]
Common Mistakes:
Confusing 'color' with 'background-color'
Assuming it checks visibility instead of style
Mixing RGB values with hex codes incorrectly
2. Which of the following is the correct Cypress syntax to assert that an element has a font size of 16 pixels?
easy
A. cy.get('p').should('css', 'font-size', '16px')
B. cy.get('p').should('have.style', 'font-size', '16px')
C. cy.get('p').assert('font-size', '16px')
D. cy.get('p').should('have.css', 'font-size', '16px')
Solution
Step 1: Identify the correct assertion method
The correct Cypress assertion to check CSS is should('have.css', property, value).
Step 2: Match the syntax with the options
cy.get('p').should('have.css', 'font-size', '16px') uses should('have.css', 'font-size', '16px'), which is correct syntax.
Final Answer:
cy.get('p').should('have.css', 'font-size', '16px') -> Option D
Quick Check:
Use should('have.css') for CSS assertions [OK]
Hint: Use 'should' with 'have.css' for CSS property checks [OK]
Common Mistakes:
Using 'should' without 'have.css'
Using 'assert' which is not a Cypress command
Confusing 'have.style' with 'have.css'
3. Given the following Cypress code snippet, what will be the test result if the element's background color is actually 'rgb(0, 128, 0)'?
A. The font-weight value should be a string: '700'.
B. The selector '#header' is invalid and should be '.header'.
C. The method 'have.css' should be replaced with 'css'.
D. The assertion should use 'expect' instead of 'should'.
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 A
Quick Check:
CSS values must be strings in assertions [OK]
Hint: Always quote CSS values in Cypress assertions [OK]
Common Mistakes:
Passing numbers instead of strings for CSS values
Changing valid selectors unnecessarily
Confusing assertion methods in Cypress
5. You want to verify that a button with class .submit-btn has a border radius of 5 pixels and a background color that is not transparent. Which Cypress code correctly asserts both styles?
hard
A. cy.get('.submit-btn').should('have.css', 'border-radius', '5px').and('have.css', 'background-color').not('transparent')
B. cy.get('.submit-btn').should('have.css', 'border-radius', '5px').and('have.css', 'background-color', 'rgba(0, 0, 0, 0)')
C. 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)'))
D. cy.get('.submit-btn').should('have.css', 'border-radius', 5).and('have.css', 'background-color', 'transparent')"
Solution
Step 1: Assert border-radius with correct value and type
Use should('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 with should and expect to verify background-color is not transparent, which is correct.