0
0
Cypresstesting~20 mins

expect() for BDD assertions in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BDD Assertions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check if element contains exact text using expect()

You want to verify that a button with id #submitBtn contains the exact text Submit. Which assertion using expect() is correct?

Cypress
cy.get('#submitBtn').then(($btn) => {
  const text = $btn.text();
  // Which expect assertion is correct?
});
Aexpect(text).to.include('Submit')
Bexpect(text).to.contain('submit')
Cexpect(text).to.equal('Submit')
Dexpect(text).to.have.text('Submit')
Attempts:
2 left
💡 Hint

Use to.equal() for exact match of strings.

Predict Output
intermediate
1:30remaining
Output of expect() assertion on array length

What is the result of this Cypress test snippet?

const items = ['apple', 'banana', 'cherry'];
expect(items).to.have.length(3);
Cypress
const items = ['apple', 'banana', 'cherry'];
expect(items).to.have.length(3);
ATest fails with length mismatch error
BTest passes successfully
CSyntaxError due to wrong assertion syntax
DTypeError because items is not a DOM element
Attempts:
2 left
💡 Hint

Arrays have a length property that can be asserted.

🔧 Debug
advanced
2:00remaining
Identify the error in this expect() assertion

What error will this Cypress assertion produce?

cy.get('.alert').then(($el) => {
  expect($el.textContent).to.include('Error');
});
Cypress
cy.get('.alert').then(($el) => {
  expect($el.textContent).to.include('Error');
});
ATest passes successfully
BAssertionError: text does not include 'Error'
CSyntaxError due to missing parentheses
DTypeError: $el.textContent is not a function or property
Attempts:
2 left
💡 Hint

Check how to get text content from a jQuery element.

🧠 Conceptual
advanced
1:30remaining
Understanding chaining with expect() in Cypress

Which statement about chaining assertions with expect() in Cypress is correct?

AYou can chain multiple assertions like <code>expect(value).to.be.a('string').and.to.include('test')</code>
BYou must use separate <code>expect()</code> calls for each assertion
CChaining multiple <code>expect()</code> calls on the same value is not allowed
DChaining works only with <code>should()</code>, not with <code>expect()</code>
Attempts:
2 left
💡 Hint

Chai assertions support chaining with and.

framework
expert
2:30remaining
Best practice for asynchronous expect() assertions in Cypress

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?

AUse <code>cy.get('#status').should('have.text', 'Loaded')</code> instead of <code>expect()</code> inside <code>then()</code>
BAdd a <code>cy.wait(5000)</code> before the <code>expect()</code> call
CUse <code>expect()</code> inside <code>then()</code> without changes; Cypress handles waiting automatically
DWrap the <code>expect()</code> call inside a <code>setTimeout</code> with delay
Attempts:
2 left
💡 Hint

Use Cypress built-in retry-ability for assertions.