Skip to content

Latest commit

 

History

History
75 lines (64 loc) · 3.16 KB

DrupalCKEditorAndMediaBrowser.md

File metadata and controls

75 lines (64 loc) · 3.16 KB

Test interacts with CKEditor and Media Browser (Drupal 7)

The below example test interacts WYSIWYG input on a Drupal node form page. Things to note:

// Taken from https://stackoverflow.com/a/56451395.
Cypress.Commands.add('waitForBrowser', () => {
  cy.window().then((win) => {
    return new Cypress.Promise((resolve) => win['requestIdleCallback'](resolve))
  })
})
  • Use of the iframe custom function.
  • Use of wrap to interact with the iframe.
  • CKEditor loads in the buttons but there are initially in a "disabled" state and then they become enabled. Cypress can clicks the button even though it is disabled, the click is acknowledged as valid but the media browser does not open. A solution I found is to check that aria-disabled is not true which I think provided enough time for button to be enabled and for the test to actually continue properly.
  • In Drupal 10 and CKEditor 5 iframes are not used so this setup is not needed. Take a look at drupal-ckeditor.cy.js for how examples of Drupal 10 and CKEdttor 5 tests.

Test code

it('YouTube video is added via media browser', () => {
  const options = {
    path: '/node/add/post',
  }

  // Track GET and POST of the media browser URL.
  cy.intercept(
    '/media/browser?render=media-popup&id=media_wysiwyg&plugins=',
  ).as('mediaBrowserURL')

  cy.visit(options.path)
  cy.waitForBrowser()
    .then(() => {
      cy.get('a[title="Media browser"]')
        .should('have.attr', 'aria-disabled', 'false')
        .click()
    })
    .then(() => {
      // Inside the media browser iframe.
      cy.wait('@mediaBrowserURL')
        .get('iframe#mediaBrowser')
        .iframe()
        .then((iframes) => {
          cy.wrap(iframes[0]).find('a[title="Web"]').click()
          cy.wrap(iframes[0])
            .find('#media-internet-add-upload')
            .should('include.text', 'YouTube')
          cy.wrap(iframes[0])
            .find('#edit-embed-code')
            .type('https://www.youtube.com/watch?v=ck6QG9ME2aU')
          cy.wrap(iframes[0]).find('#media-internet-add-upload').submit()
        })
    })

  // Wait for error message.
  cy.wait('@mediaBrowserURL')
    .get('iframe#mediaBrowser')
    .iframe()
    .then((iframes) => {
      cy.wrap(iframes[0]).find('.messages.error').should('not.exist')
    })
})

Useful references