Verify input field value and button attribute
Preconditions (1)
✅ Expected Result: The input field contains the typed value 'testuser' and the submit button has the correct 'type' attribute set to 'submit'
describe('Value and attribute assertions test', () => { beforeEach(() => { cy.visit('/form'); }); it('should type username and verify value and button attribute', () => { cy.get('#username').type('testuser').should('have.value', 'testuser'); cy.get('#submit-btn').should('have.attr', 'type', 'submit'); }); });
This test visits the form page before each test to ensure a fresh start.
It types 'testuser' into the input field with id 'username' and immediately asserts that the input's value is 'testuser'.
Then it checks that the submit button with id 'submit-btn' has the attribute 'type' set to 'submit'.
Using cy.get() with id selectors ensures precise element targeting. The .should() command is used for clear, readable assertions.
Now add data-driven testing with 3 different usernames and verify the input value and button attribute for each.
cy.get('input').should('have.value', 'hello') check?should('have.value', 'hello') which asserts the value property of the input.disabled?should('have.attr', 'attributeName').should('have.attr', 'disabled').cy.get('input#email')
.should('have.attr', 'placeholder', 'Enter your email')
.and('have.value', '')should('have.attr', 'placeholder', 'Enter your email') checks the placeholder attribute equals 'Enter your email'.and('have.value', '') asserts the input's current value is empty (no text typed).cy.get('input[name="username"]').should('have.value', 'admin')have.value fails, likely 'admin' is placeholder text, not actual value.data-test with value agree. Which Cypress assertion chain is correct?should('be.checked').should('have.attr', 'data-test', 'agree').and() to check both conditions on the same element.Pass the test and this pattern moves unknown