Automating a Quill Text-Editor in Cypress

Published on March 14, 2024

Quill is a modern open-source text editor designed for web applications. It’s often used to provide rich text editing capabilities within web applications, allowing users to format text, insert images, create links, and more.

Quill is built with JavaScript and offers a flexible API that developers can use to customize its behavior and appearance to suit their specific needs. It’s designed to be lightweight, fast, and easy to integrate into web projects.

Quill Text-Editor

Some key features of Quill include:

  1. Rich text editing: Quill allows users to format text using familiar tools like bold, italics, underline, bullet points, and headings.
  2. Embeddable media: Users can insert images, videos, and other media directly into their documents.
  3. Customizable toolbar: Developers can customize the toolbar to include only the formatting options they want to provide to users.
  4. Cross-browser compatibility: Quill works across major web browsers, ensuring a consistent editing experience for users.
  5. Modular architecture: Quill’s architecture is modular, making it easy to extend and customize with plugins.

It provides a convenient solution for implementing rich text editing functionality in web applications, making it popular among developers.

When automating Quill for testing purposes, integration with testing frameworks like Cypress or Selenium may require additional considerations compared to automating a plain text editor.

Enter text to a Quill Text-Editor using Cypress

cy.get(elementSelector).should('be.visible').then(($quillContainer) => {
// Attempt to find the Quill editor within the container
const quillEditor = $quillContainer.find('.ql-editor')[0];

if (quillEditor) {
// Use Quill's API to set content
cy.wrap(quillEditor).type(value);
} else {
// Handle the case when Quill editor is not found
throw new Error('Quill editor not found within the selected element');
}
});

Upload an image into the Quill Editor using Cypress

cy.get('iframe#your-iframe-id').then($iframe => {
const quill = $iframe.contents().find('.ql-editor');

// Click the Quill editor to focus on it
cy.wrap(quill).click();

// Upload an image
cy.fixture('image.jpg').then(fileContent => {
cy.get('input[type="file"]').upload({ fileContent, fileName: 'image.jpg', mimeType: 'image/jpeg' });
});

// Wait for the image to be uploaded (customize wait time as needed)
cy.wait(1000);
});

Clear text in a Quill Text-Editor using Cypress

 cy.get(elementSelector).should('be.visible').then(($quillContainer) => {
// Attempt to find the Quill editor within the container
const quillEditor = $quillContainer.find('.ql-editor')[0];

if (quillEditor) {
// Use Quill's API to clear content
cy.wrap(quillEditor).clear();
} else {
// Handle the case when Quill editor is not found
throw new Error('Quill editor not found within the selected element');
}
});

It is different from automating a standard plain text box/editor due to the complexities involved in handling formatted text, images, and other rich media elements.

And if you like this article, give a clap and follow for more. Thank you! 😊