0
0
Cypresstesting~20 mins

Custom plugin development in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cypress Plugin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a Cypress custom command plugin
What will be the output in the Cypress test runner console after running this custom command?
Cypress
Cypress.Commands.add('logMessage', (msg) => {
  cy.log(`Message: ${msg}`)
  return msg.length
})

describe('Test logMessage command', () => {
  it('logs and returns length', () => {
    cy.logMessage('hello').then(length => {
      cy.log(`Length is ${length}`)
    })
  })
})
ALogs 'Length is 5' only, no message logged
BLogs 'Message: hello' and then logs 'Length is 5'
CThrows an error because custom command returns a number directly
DLogs 'Message: hello' only, no length logged
Attempts:
2 left
💡 Hint
Remember that Cypress commands are asynchronous and return chainable objects.
assertion
intermediate
1:30remaining
Correct assertion for a plugin-modified DOM element
A Cypress plugin modifies a button's text to 'Clicked' after a click. Which assertion correctly verifies this change?
Cypress
cy.get('button#submit').click()
// Which assertion verifies the text change?
Acy.get('button#submit').should('have.value', 'Clicked')
Bcy.get('button#submit').should('contain', 'Clicked')
Ccy.get('button#submit').should('have.text', 'Clicked')
Dcy.get('button#submit').should('have.attr', 'text', 'Clicked')
Attempts:
2 left
💡 Hint
Check the difference between text content and attributes.
🔧 Debug
advanced
2:00remaining
Identify the cause of plugin command failure
A custom Cypress plugin command fails with 'TypeError: cy.wrap is not a function'. What is the most likely cause?
AThe plugin tries to use cy.wrap inside the plugins/index.js file where cy is not available
BThe custom command is defined outside the Cypress.Commands.add block
CThe plugin file does not import Cypress correctly, so cy is undefined
DThe test file forgot to import the plugin command
Attempts:
2 left
💡 Hint
Remember where Cypress commands can be used vs plugin code.
🧠 Conceptual
advanced
1:30remaining
Purpose of Cypress plugin file
What is the main purpose of the Cypress plugins file (cypress/plugins/index.js)?
ATo write test cases and assertions
BTo define custom Cypress commands that run in the browser
CTo style the Cypress test runner UI
DTo modify or extend the Node process that runs Cypress tests, like handling tasks or environment variables
Attempts:
2 left
💡 Hint
Think about what runs in Node vs browser in Cypress.
framework
expert
2:30remaining
Correct way to register a custom task in Cypress plugin
Which code snippet correctly registers a custom task named 'readFile' in Cypress plugins file to read a file asynchronously?
Cypress
const fs = require('fs').promises;

module.exports = (on, config) => {
  // Register tasks here
}
A
on('task', {
  readFile: async (path) => {
    return await fs.readFile(path, 'utf8')
  }
})
B
on('task', {
  readFile: (path) => {
    fs.readFile(path, 'utf8')
  }
})
C
on('task', {
  readFile(path) {
    fs.readFileSync(path, 'utf8')
  }
})
D
on('task', {
  readFile(path) {
    return fs.readFile(path, 'utf8')
  }
})
Attempts:
2 left
💡 Hint
Remember tasks can return promises and should be async if needed.