Consider this Cypress test code that uploads a file using cy.selectFile. What will be the test result?
cy.get('input[type=file]').selectFile('cypress/fixtures/sample.txt') .then(input => { expect(input[0].files[0].name).to.equal('sample.txt') })
Check how cy.selectFile accepts a string path to a fixture file.
The selectFile command accepts a string path to a file in the fixtures folder. The test checks the uploaded file's name and it matches 'sample.txt', so the assertion passes.
You want to check that the uploaded file size is exactly 1024 bytes after using cy.selectFile. Which assertion is correct?
Remember the file list is an array-like object accessed by index.
The file list is accessed as input[0].files[0]. The size property holds the file size in bytes. Option C uses correct syntax and property access.
You want to select the file input element on a form for uploading a file using cy.selectFile. Which locator is best practice?
Use the most specific and stable selector possible.
Using an ID selector like #fileInput is more specific and less likely to break than generic selectors like input[type=file] or classes that may not target the input element.
Given this test code, why does the upload fail with 'No file selected'?
cy.get('input[type=file]').selectFile({ contents: 'Hello' })Check the type expected for the contents property in selectFile.
The contents property must be a binary type like Buffer or Uint8Array. Passing a plain string causes the file to be empty, leading to 'No file selected' error.
cy.selectFile?You want to simulate a drag-and-drop file upload in Cypress using cy.selectFile. Which option correctly triggers the drag-and-drop event?
Look for the action option in selectFile to simulate drag-drop.
Option D uses selectFile with { action: 'drag-drop' } which simulates dragging and dropping the file onto the element. Other options either miss this or use unsupported commands.