Bird
Raised Fist0
Cypresstesting~10 mins

should() with chainers in Cypress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the element is visible using should().

Cypress
cy.get('button').should([1])
Drag options to blanks, or click blank then click option'
A'have.class'
B'exist'
C'contain.text'
D'be.visible'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exist' instead of 'be.visible' only checks presence, not visibility.
Using 'contain.text' requires additional arguments.
2fill in blank
medium

Complete the code to assert the button has the class 'active' using should() with a chainer.

Cypress
cy.get('button').should([1], 'active')
Drag options to blanks, or click blank then click option'
A'have.class'
B'be.enabled'
C'have.text'
D'contain.value'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'have.text' checks text content, not classes.
Using 'be.enabled' checks if element is enabled, not class.
3fill in blank
hard

Fix the error in the code to check if the input contains the value 'test' using should() with chainers.

Cypress
cy.get('input').should([1], 'test')
Drag options to blanks, or click blank then click option'
A'be.checked'
B'have.value'
C'have.attr'
D'contain.text'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contain.text' does not check input value.
Using 'have.attr' requires specifying attribute name.
4fill in blank
hard

Fill both blanks to check if a checkbox is checked and has the attribute 'type' equal to 'checkbox'.

Cypress
cy.get('input[type="checkbox"]').should([1]).and([2], 'type', 'checkbox')
Drag options to blanks, or click blank then click option'
A'be.checked'
B'have.attr'
C'have.value'
D'be.visible'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'have.value' instead of 'have.attr' for attribute check.
Using 'be.visible' does not check checked state.
5fill in blank
hard

Fill all three blanks to assert that a link contains the text 'Home' and has the attribute 'href' equal to '/'.

Cypress
cy.get('a').should([1], 'Home').and([2], 'href', [3])
Drag options to blanks, or click blank then click option'
A'contain.text'
B'have.attr'
C'/'
D'be.visible'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'be.visible' as first chainer instead of 'contain.text'.
Using incorrect attribute value for href.

Practice

(1/5)
1. What does the should() command do in Cypress testing?
easy
A. It asserts that the selected element meets a specific condition.
B. It clicks on the selected element.
C. It waits for the page to load completely.
D. It navigates to a new URL.

Solution

  1. Step 1: Understand the purpose of should()

    The should() command is used to check if an element meets a condition, like visibility or text content.
  2. Step 2: Differentiate from other commands

    Commands like clicking or navigation are done by other Cypress commands, not should().
  3. Final Answer:

    It asserts that the selected element meets a specific condition. -> Option A
  4. Quick Check:

    should() = assertion [OK]
Hint: Remember: should() checks conditions, it doesn't perform actions [OK]
Common Mistakes:
  • Confusing should() with click() or visit()
  • Thinking should() changes page state
  • Using should() without a condition
2. Which of the following is the correct syntax to check if an element is visible and contains the text 'Submit' using should() with chaining?
easy
A. cy.get('button').should('visible', 'Submit')
B. cy.get('button').should('be.visible', 'contain', 'Submit')
C. cy.get('button').should('contain', 'Submit').should('visible')
D. cy.get('button').should('be.visible').and('contain', 'Submit')

Solution

  1. Step 1: Identify correct chaining syntax

    The correct way is to use should() for the first assertion and and() for chaining additional assertions.
  2. Step 2: Check each option

    cy.get('button').should('be.visible').and('contain', 'Submit') uses should('be.visible') then and('contain', 'Submit'), which is correct syntax. Others misuse parameters or chaining.
  3. Final Answer:

    cy.get('button').should('be.visible').and('contain', 'Submit') -> Option D
  4. Quick Check:

    Use should() then and() for multiple checks [OK]
Hint: Use should() first, then and() to chain assertions [OK]
Common Mistakes:
  • Passing multiple conditions in one should() call incorrectly
  • Using should() multiple times instead of and()
  • Wrong order of chaining
3. What will be the result of the following Cypress code?
cy.get('input').should('have.attr', 'type').and('eq', 'text')
medium
A. The test throws a syntax error.
B. The test passes if the input element has a type attribute equal to 'text'.
C. The test passes if the input element has any type attribute, regardless of value.
D. The test fails if the input element has any type attribute.

Solution

  1. Step 1: Understand the first assertion

    should('have.attr', 'type') checks if the element has a 'type' attribute.
  2. Step 2: Understand the chained assertion

    and('eq', 'text') checks if the attribute's value equals 'text'. Both must be true for the test to pass.
  3. Final Answer:

    The test passes if the input element has a type attribute equal to 'text'. -> Option B
  4. Quick Check:

    have.attr + eq chaining = attribute equals value [OK]
Hint: Chain have.attr with eq to check attribute value [OK]
Common Mistakes:
  • Thinking have.attr only checks presence, not value
  • Confusing eq with contain
  • Assuming syntax error due to chaining
4. Identify the error in this Cypress test code:
cy.get('button').should('be.visible').and('contain', Submit)
medium
A. The selector 'button' is invalid.
B. The chaining with and() is not allowed after should().
C. The text 'Submit' should be a string inside quotes.
D. The should() command cannot check visibility.

Solution

  1. Step 1: Check the parameter passed to contain

    The word Submit is not in quotes, so it is treated as a variable, which causes an error.
  2. Step 2: Verify chaining and selector

    Chaining with and() is valid, and 'button' is a valid selector. should('be.visible') is also valid.
  3. Final Answer:

    The text 'Submit' should be a string inside quotes. -> Option C
  4. Quick Check:

    Text values must be quoted in assertions [OK]
Hint: Always quote text strings in should() and and() assertions [OK]
Common Mistakes:
  • Forgetting quotes around text in contain
  • Thinking and() chaining is invalid
  • Misunderstanding selector syntax
5. You want to verify that a checkbox is checked and also has the attribute 'disabled' set to true. Which Cypress command chain correctly performs both checks?
hard
A. cy.get('input[type=checkbox]').should('be.checked').and('have.attr', 'disabled', 'true')
B. cy.get('input[type=checkbox]').should('have.attr', 'disabled', 'true').should('checked')
C. cy.get('input[type=checkbox]').should('be.checked', 'have.attr', 'disabled', 'true')
D. cy.get('input[type=checkbox]').and('be.checked').and('have.attr', 'disabled', 'true')

Solution

  1. Step 1: Understand chaining with should() and and()

    Use should() for the first assertion and and() to chain the second assertion.
  2. Step 2: Check each option's correctness

    cy.get('input[type=checkbox]').should('be.checked').and('have.attr', 'disabled', 'true') correctly chains should('be.checked') and and('have.attr', 'disabled', 'true'). cy.get('input[type=checkbox]').should('have.attr', 'disabled', 'true').should('checked') uses invalid chainer should('checked') instead of should('be.checked'). cy.get('input[type=checkbox]').should('be.checked', 'have.attr', 'disabled', 'true') incorrectly passes multiple conditions in one should(). cy.get('input[type=checkbox]').and('be.checked').and('have.attr', 'disabled', 'true') uses and() without should() first, which is invalid.
  3. Final Answer:

    cy.get('input[type=checkbox]').should('be.checked').and('have.attr', 'disabled', 'true') -> Option A
  4. Quick Check:

    should() then and() for multiple assertions [OK]
Hint: Start with should(), then chain with and() for multiple checks [OK]
Common Mistakes:
  • Passing multiple conditions in one should() call
  • Using and() without should() first
  • Using multiple should() calls unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard