You want to verify that a button with id #submitBtn contains the exact text Submit. Which assertion using expect() is correct?
cy.get('#submitBtn').then(($btn) => { const text = $btn.text(); // Which expect assertion is correct? });
Use to.equal() for exact match of strings.
to.equal() checks for exact equality. to.include() and to.contain() check for substring presence. to.have.text() is not a valid Chai assertion.
What is the result of this Cypress test snippet?
const items = ['apple', 'banana', 'cherry']; expect(items).to.have.length(3);
const items = ['apple', 'banana', 'cherry']; expect(items).to.have.length(3);
Arrays have a length property that can be asserted.
The assertion to.have.length(3) correctly checks the array length. No errors occur.
What error will this Cypress assertion produce?
cy.get('.alert').then(($el) => {
expect($el.textContent).to.include('Error');
});cy.get('.alert').then(($el) => { expect($el.textContent).to.include('Error'); });
Check how to get text content from a jQuery element.
$el.textContent does not exist on a jQuery object (use $el.text()). It is undefined, causing a TypeError.
Which statement about chaining assertions with expect() in Cypress is correct?
Chai assertions support chaining with and.
expect() returns an assertion object that supports chaining with and for multiple checks on the same value.
Consider this Cypress test:
cy.get('#status').then(($el) => {
expect($el.text()).to.equal('Loaded');
});What is the best practice to ensure the assertion waits for the element text to become 'Loaded' before checking?
Use Cypress built-in retry-ability for assertions.
should() retries the assertion until it passes or times out, making tests more reliable. Using expect() inside then() runs only once and may fail if the text is not ready.