2

I'm trying to test a web text editor that saves and opens files from the local system. I'm using Cypress v11.2.0 with Chrome v107.

The file operations use the File System Access API through the browser-fs-access library.

I'm unable to test any of the file operations like save, for example. When the underline system function showSaveFilePicker is called, the test throws an error.

This is the test code:

  it("'new' creates and links to an empty file", () => {
    cy.visit("/");
    cy.get("#new").click();
  });

Here is the error image:

cypress error

The operation is working fine in the app.

My question is: how should someone test a button that evokes showSaveFilePicker using Cypress?

Fody
  • 23,754
  • 3
  • 20
  • 37
João Melo
  • 784
  • 6
  • 16

1 Answers1

2

It will work with plugin library cypress-real-events

import 'cypress-real-events'

it("'new' creates and links to an empty file", () => {
  cy.visit('https://googlechromelabs.github.io/browser-fs-access/demo/');
  cy.contains("button", 'Save Image File')
    .realClick();
});

Stubbing

Although, the problem is what to do next? The test can't interact with the live popup.

I guess stub the call as per example here Window confirm popup.

But that doesn't need cypress-real-events, since the dialog is not actually called.

it("'new' creates and links to an empty file", () => {
  cy.visit('https://googlechromelabs.github.io/browser-fs-access/demo/');

  cy.window().then((win) =>
    cy.stub(win, 'showSaveFilePicker').as('showSaveFilePicker').returns(true),
  )

  cy.contains("button", 'Save Image File')
    .click();

  cy.get('@showSaveFilePicker')
    .should('have.been.calledOnce')
    .invoke('restore')
});
Fody
  • 23,754
  • 3
  • 20
  • 37
  • I hoped that after succeding in the button click, I could find a way to interact with the dialog. I thank you a lot for anticipating that and offering an approach. – João Melo Nov 29 '22 at 11:15