Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions mediaLibraryAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@
* cy.mediaLibraryAdd('#field_media_assets-media-library-wrapper', 'sample.mp3', 'audio');
*/
Cypress.Commands.add("mediaLibraryAdd", (selector, fileName, type="") => {
// Create a unique variable name for the ajax listener. Cypress does not like it being reused.
const mediaNodeEditAjax = 'mediaNodeEditAjax' + selector + Math.random();
cy.intercept('POST', '/node/*/**').as(mediaNodeEditAjax)
cy.get(selector).within(() => {
const mediaNodeEditAjax = 'mediaNodeEditAjax' + selector + Math.random();
cy.intercept('POST', '/node/*/**').as(mediaNodeEditAjax)
cy.get('input[value="Add media"]').click();
cy.wait('@'+mediaNodeEditAjax).its('response.statusCode').should('eq', 200)
});

const mediaLibraryAjax = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library**').as(mediaLibraryAjax)
const mediaNodeEditAjax2 = 'mediaNodeEditAjax' + selector + Math.random();
cy.intercept('POST', '/node/*/**').as(mediaNodeEditAjax2)

// Get the media modal and add files.
cy.get('.media-library-widget-modal').within(($modal) => {
// Check if we need to select media type
if ($modal.find('.media-library-menu').length) {
if (type.length > 0) {
const buttonClass= ".media-library-menu-" + type
const mediaLibraryAjax = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library**').as(mediaLibraryAjax)
cy.get(buttonClass).click();
cy.wait('@' + mediaLibraryAjax).its('response.statusCode').should('eq', 200)
}
}

// Upload the file.
const mediaLibraryAjax1 = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library?*').as(mediaLibraryAjax1)
cy.get('input[type=file]').selectFile('cypress/fixtures/' + fileName);
cy.wait('@' + mediaLibraryAjax).its('response.statusCode').should('eq', 200)
cy.wait('@' + mediaLibraryAjax1).its('response.statusCode').should('eq', 200)


// Basic check if the file is an image so we know if we have to add some alt tags.
Expand All @@ -44,15 +47,17 @@ Cypress.Commands.add("mediaLibraryAdd", (selector, fileName, type="") => {
}

// Select the uploaded file.
const mediaLibraryAjax2 = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library?*').as(mediaLibraryAjax2)
cy.get('.form-actions button').contains('save', { matchCase: false }).click()
cy.wait('@' + mediaLibraryAjax).its('response.statusCode').should('eq', 200)
cy.wait('@' + mediaLibraryAjax2).its('response.statusCode').should('eq', 200)

// Insert from media library
const mediaLibraryAjax3 = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library**').as(mediaLibraryAjax3)
cy.get('.form-actions button').contains('insert', { matchCase: false }).click()
cy.wait('@' + mediaLibraryAjax).its('response.statusCode').should('eq', 200)
cy.wait('@' + mediaLibraryAjax3).its('response.statusCode').should('eq', 200)
})

// Wait for original node edit form field to update with media library selection.
cy.wait('@'+mediaNodeEditAjax).its('response.statusCode').should('eq', 200)
cy.wait(500)
cy.wait('@'+mediaNodeEditAjax2).its('response.statusCode').should('eq', 200)
});
34 changes: 15 additions & 19 deletions mediaLibrarySelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,46 @@
*/
Cypress.Commands.add("mediaLibrarySelect", (selector, fileName, type="") => {
// Create a unique variable name for the ajax listener. Cypress does not like it being reused.
const mediaNodeEditAjax = 'mediaNodeEditAjax' + selector + Math.random();
cy.intercept('POST', '/node/*/**').as(mediaNodeEditAjax)

cy.get(selector).within(() => {
const mediaNodeEditAjax = 'mediaNodeEditAjax' + selector + Math.random();
cy.intercept('POST', '/node/*/**').as(mediaNodeEditAjax)
cy.get('input[value="Add media"]').click();
cy.wait('@'+mediaNodeEditAjax).its('response.statusCode').should('eq', 200)
});

const mediaLibraryAjax = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library**').as(mediaLibraryAjax)
const viewsAjax = 'viewsAjax' + selector + Math.random();
cy.intercept('GET', '/views/ajax?**').as(viewsAjax)
const mediaNodeEditAjax2 = 'mediaNodeEditAjax' + selector + Math.random();
cy.intercept('POST', '/node/*/**').as(mediaNodeEditAjax2)

// Get the media modal and add files.
cy.get('.media-library-widget-modal').within(($modal) => {
// Check if we need to select media type
if ($modal.find('.media-library-menu').length) {
if (type.length > 0) {
const buttonClass= ".media-library-menu-" + type
const mediaLibraryAjaxMenu = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library**').as(mediaLibraryAjaxMenu)
cy.get(buttonClass).click();
cy.wait('@' + mediaLibraryAjax).its('response.statusCode').should('eq', 200)
cy.wait('@' + mediaLibraryAjaxMenu).its('response.statusCode').should('eq', 200)
}
}

// Search for the file.
cy.get('.views-exposed-form input[name="name"]').clear().type(fileName);
const viewsAjax = 'viewsAjax' + selector + Math.random();
cy.intercept('GET', '/views/ajax?**').as(viewsAjax)
cy.get('.views-exposed-form input[type="submit"]').click();
// Ajax intercept doesn't seem to work in this instance for some reason...
// cy.wait('@' + viewsAjax).its('response.statusCode').should('eq', 200)
// Changing to cy.wait()
cy.wait(7000)
cy.wait('@' + viewsAjax).its('response.statusCode').should('eq', 200)


// Select the first match
cy.get('.media-library-views-form .views-row').first().click();

// Insert from media library
const mediaLibraryAjax2 = 'mediaLibraryAjax' + selector + Math.random();
cy.intercept('POST', '/media-library?**').as(mediaLibraryAjax2)
cy.get('.form-actions button').contains('Insert selected').click()
cy.wait(7000)
// Ajax intercept doesn't seem to work in this instance for some reason...
// const mediaLibraryAjax2 = 'mediaLibraryAjax' + selector + Math.random();
// cy.intercept('POST', '/media-library?**').as(mediaLibraryAjax2)
// cy.wait('@' + mediaLibraryAjax2).its('response.statusCode').should('eq', 200)
cy.wait('@' + mediaLibraryAjax2).its('response.statusCode').should('eq', 200)
})

cy.wait('@'+mediaNodeEditAjax).its('response.statusCode').should('eq', 200)
cy.wait(500)
cy.wait('@'+mediaNodeEditAjax2).its('response.statusCode').should('eq', 200)
});