Bird
Raised Fist0
Cypresstesting~20 mins

CSS assertions in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSS Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Check background color of a button
You want to verify that a button with id #submitBtn has a background color of rgb(0, 128, 0). Which Cypress assertion is correct?
Cypress
cy.get('#submitBtn').should('have.css', 'background-color', 'rgb(0, 128, 0)')
Acy.get('#submitBtn').should('have.style', 'background-color', 'rgb(0, 128, 0)')
Bcy.get('#submitBtn').should('have.css', 'background-color', 'rgb(0, 128, 0)')
Ccy.get('#submitBtn').should('have.attr', 'background-color', 'rgb(0, 128, 0)')
D)')0 ,821 ,0(bgr' ,'roloc-dnuorgkcab' ,'ssc.evah'(dluohs.)'ntBtimbus#'(teg.yc
Attempts:
2 left
💡 Hint
Use the correct assertion method to check CSS properties.
❓ assertion
intermediate
2:00remaining
Verify font size of a paragraph
Which Cypress command correctly asserts that a paragraph with class .intro has a font size of 16px?
Cypress
cy.get('.intro').should('have.css', 'font-size', '16px')
Acy.get('.intro').should('have.css', 'font-size', '16px')
Bcy.get('.intro').should('have.attr', 'font-size', '16px')
Ccy.get('.intro').should('contain.css', 'font-size', '16px')
Dcy.get('.intro').should('have.style', 'font-size', '16px')
Attempts:
2 left
💡 Hint
Remember CSS properties are checked with 'have.css' in Cypress.
❓ Predict Output
advanced
2:00remaining
Output of CSS color assertion failure
What will be the test result when running this Cypress code if the element's color is rgb(255, 0, 0) but the assertion expects rgb(0, 0, 255)?
Cypress
cy.get('.title').should('have.css', 'color', 'rgb(0, 0, 255)')
ATest fails with a syntax error in the test code
BTest passes because color values are case-insensitive
CTest fails with an assertion error showing expected 'rgb(0, 0, 255)' but found 'rgb(255, 0, 0)'
DTest passes but logs a warning about color mismatch
Attempts:
2 left
💡 Hint
Think about what happens when expected and actual CSS values differ.
❓ locator
advanced
2:00remaining
Best locator for CSS assertion on a disabled button
You want to assert the opacity of a disabled button. Which locator is best to select the disabled button for CSS assertion?
Acy.get('button:disabled')
Bcy.get('button[disabled="true"]')
Ccy.get('button.disabled')
Dcy.get('button[disabled]')
Attempts:
2 left
💡 Hint
Consider how HTML disabled attribute works and how to select it in CSS.
❓ framework
expert
3:00remaining
Handling dynamic CSS changes in Cypress tests
You have a button that changes its background color from rgb(255, 0, 0) to rgb(0, 255, 0) after a click. Which Cypress code correctly asserts this dynamic CSS change?
Acy.get('#colorBtn').should('have.css', 'background-color', 'rgb(255, 0, 0)').click().should('have.css', 'background-color', 'rgb(0, 255, 0)')
Bcy.get('#colorBtn').click().should('have.css', 'background-color', 'rgb(0, 255, 0)')
Ccy.get('#colorBtn').click(); cy.get('#colorBtn').should('have.css', 'background-color', 'rgb(0, 255, 0)')
Dcy.get('#colorBtn').click().then(() => { cy.get('#colorBtn').should('have.css', 'background-color', 'rgb(0, 255, 0)') })
Attempts:
2 left
💡 Hint
Check the initial state before click and then the changed state after click.

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

  1. Step 1: Understand the CSS property being checked

    The command checks the CSS property 'color', which controls the text color of the element.
  2. Step 2: Interpret the value 'rgb(255, 0, 0)'

    This RGB value represents the color red.
  3. Final Answer:

    It checks if the button's text color is red. -> Option B
  4. 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

  1. Step 1: Identify the correct assertion method

    The correct Cypress assertion to check CSS is should('have.css', property, value).
  2. 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.
  3. Final Answer:

    cy.get('p').should('have.css', 'font-size', '16px') -> Option D
  4. 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)'?
cy.get('.alert').should('have.css', 'background-color', 'rgb(255, 0, 0)')
medium
A. The test will fail because the background color does not match 'rgb(255, 0, 0)'.
B. The test will pass because the element has a background color.
C. The test will throw a syntax error.
D. The test will be skipped automatically.

Solution

  1. Step 1: Understand the expected CSS value

    The test expects the background color to be 'rgb(255, 0, 0)', which is red.
  2. Step 2: Compare with actual CSS value

    The actual background color is 'rgb(0, 128, 0)', which is green, so it does not match.
  3. Final Answer:

    The test will fail because the background color does not match 'rgb(255, 0, 0)'. -> Option A
  4. Quick Check:

    CSS value mismatch causes test failure [OK]
Hint: Match exact CSS values to avoid test failures [OK]
Common Mistakes:
  • Assuming any color presence passes the test
  • Confusing RGB values for different colors
  • Expecting Cypress to auto-correct CSS mismatches
4. Identify the error in this Cypress CSS assertion and choose the fix:
cy.get('#header').should('have.css', 'font-weight', 700)
medium
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

  1. Step 1: Check the type of CSS value in assertion

    Cypress expects the CSS value as a string, so 700 should be '700'.
  2. Step 2: Verify other parts of the command

    The selector '#header' is valid, and 'should' with 'have.css' is correct syntax.
  3. Final Answer:

    The font-weight value should be a string: '700'. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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 C
  5. Quick Check:

    Use callback assertions for negative CSS checks [OK]
Hint: Use callback assertions to check CSS values are not equal [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard