Skip to content

[Proposal] Facilitate for modern file selection #198

Description

@HarelM

In Maputnik we use two ways to open a file.

  1. Using the showOpenFilePicker which allows opening a file and writing back to it, which is very good for the use case of this app
  2. When the above doesn't exist, like in firefox, we use the regular <label><a/><input type="file"/></label>, which exited since the dawn of history.

To overcome be able to test this we use the following helper method - it receives the fixture name (file), the button test id, the input test id and checks which approach should be mocked.
This is method is browser agnostic and work well on firefox and chrome alike.

openFileByFixture: (fixture: string, buttonTestId: string, inputTestId: string) => {
      cy.window().then((win) => {
        const file = {
          text: cy.stub().resolves(cy.fixture(fixture).then(JSON.stringify)),
        }
        const fileHandle = {
          getFile: cy.stub().resolves(file),
        }
        if (!win.showOpenFilePicker) {
          this.helper.get.elementByTestId(inputTestId).selectFile("cypress/fixtures/" + fixture, { force: true });
        } else {
          cy.stub(win, 'showOpenFilePicker').resolves([fileHandle])
          this.helper.get.elementByTestId(buttonTestId).click();
        }
      });
    },

I'm proposing to add this method to this library if there's an interest.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions