File: D:/HostingSpaces/Neopoints/momsecurity.be/tests/Cypress/support/commands.js
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
Cypress.Commands.add('uploadFile', (fixtureFileName, inputSelector, mimeType = '', waitASecond = true) => {
return cy.get(inputSelector).then(subject => {
let upload = cy.fixture(fixtureFileName, 'base64')
.then(Cypress.Blob.base64StringToBlob)
.then(blob => {
const el = subject[0];
const nameSegments = fixtureFileName.split('/');
const name = nameSegments[nameSegments.length - 1];
const testFile = new File([blob], name, { type: mimeType });
const dataTransfer = new DataTransfer();
dataTransfer.items.add(testFile);
el.files = dataTransfer.files;
el.dispatchEvent(new Event('change'));
return subject;
});
if(!waitASecond) return upload;
else upload.then(_ => cy.wait(1000));
});
});