Verify the page title and button text using BDD expect assertions
Preconditions (2)
✅ Expected Result: The page title matches exactly 'Cypress.io: Kitchen Sink' and the button with id 'query-btn' contains text with the word 'Button'
describe('BDD expect() assertions example', () => { it('checks page title and button text', () => { cy.visit('https://example.cypress.io'); cy.title().should('equal', 'Cypress.io: Kitchen Sink'); cy.get('#query-btn').should('be.visible').then(($btn) => { expect($btn.text()).to.include('Button'); }); }); });
This test opens the Cypress example page using cy.visit(). Then it checks the page title with cy.title() and asserts it equals the expected string using .should('equal', ...). Next, it finds the button by its id #query-btn and ensures it is visible. Using .then(), it accesses the button's text and uses expect() to assert the text includes the word 'Button'. This approach uses Cypress best practices: automatic waiting, clear selectors, and BDD style assertions with expect().
Now add data-driven testing to check multiple buttons with different ids and expected text using expect() assertions
expect() in Cypress tests?expect()expect() is used to compare actual results with expected results in tests.count equals 5 using expect() in Cypress?to.equal() checks equality.expect(count).to.equal(5); is the correct way to assert equality.to.equal() for equality [OK]const title = 'Welcome'; expect(title).to.have.length(7);
to.have.length(7) correctly asserts the string length is 7.expect(user.name).to.equals('Alice');to.equal, not to.equals.to.equals causes a runtime error because it is not defined.to.equal -> Option Bitems contains the string 'apple' using expect(). Which assertion correctly tests this?items.to.include('apple') checks if the array contains that element.Pass the test and this pattern moves unknown