Verify CSS properties of a button on the homepage
Preconditions (2)
✅ Expected Result: The button with id 'submit-btn' has the background color '#007bff', font size '16px', and border radius '4px'
describe('CSS Assertions for Submit Button', () => { beforeEach(() => { cy.visit('https://example.com'); // Replace with actual homepage URL }); it('should have correct CSS properties on submit button', () => { // Locate the button by id cy.get('#submit-btn') // Assert background color .should('have.css', 'background-color', 'rgb(0, 123, 255)') // Assert font size .and('have.css', 'font-size', '16px') // Assert border radius .and('have.css', 'border-radius', '4px'); }); });
This Cypress test suite named CSS Assertions for Submit Button runs on the homepage URL.
The beforeEach hook opens the homepage before each test.
The test locates the button with id submit-btn using cy.get('#submit-btn').
It then asserts the CSS properties using should('have.css', property, value):
rgb(0, 123, 255) because browsers return colors in rgb format.16px.4px.Using chaining with .and() keeps the assertions clean and readable.
This approach follows Cypress best practices for CSS assertions and element selection.
Now add data-driven testing to verify CSS properties for three different buttons with ids 'submit-btn', 'cancel-btn', and 'reset-btn' each having different expected CSS values.
cy.get('button').should('have.css', 'color', 'rgb(255, 0, 0)') check?should('have.css', property, value).should('have.css', 'font-size', '16px'), which is correct syntax.should('have.css') for CSS assertions [OK]cy.get('.alert').should('have.css', 'background-color', 'rgb(255, 0, 0)')cy.get('#header').should('have.css', 'font-weight', 700).submit-btn has a border radius of 5 pixels and a background color that is not transparent. Which Cypress code correctly asserts both styles?should('have.css', 'border-radius', '5px') to check border radius as a string with units.should and expect to verify background-color is not transparent, which is correct.Pass the test and this pattern moves unknown