joomla-projects / joomla-cypress

Helpers for using Cypress with Joomla for testing
3 stars 9 forks source link

Joomla Cypress

version

This is a support package that helps in writing end-to-end tests for the Joomla CMS and its extensions with the frontend testing tool Cypress. The Joomla default branch and all active development branches are supported.

Installation

joomla-cypress is distributed with npm and works with the latest version of Cypress. It should be installed as one of your project's devDependencies:

npm install --save-dev @testing-library/joomla-cypress

Usage

To import and load the additional Cypress custom commands you will typically extend the cypress/support/index.js file:

// Add attachFile() command, which is used in installExtensionFromFileUpload()
import 'cypress-file-upload';

// Register Joomla Cypress custom commands from npm module 'joomla-cypress'
import { registerCommands } from "joomla-cypress";
registerCommands();

Cypress Custom Commands for Joomla

The Cypress API is extended with custom commands. As Cypress custom commands, all joomla-cypress extended commands return an Cypress.Chainable.

The target Joomla installation requires the language en-GB for the used Joomla administrator user.

All added commands use Cypress logging, with messages formatted in bold type at the beginning and double hyphens at the end. These log messages can be observed when running the Cypress GUI. This consistent logging format helps to easily identify the steps and status of the custom commands during test execution. For example:

Install an extension from file upload
...
--Install an extension from file upload--

The Cypress custom commands for Joomla are grouped into the following categories:

:point_right: If you would like to see the complete list of custom commands as overview, on GitHub you can use the TOC button at the top right of the README.

User Commands

doAdministratorLogin

The command doAdministratorLogin initiates the administrator login process for the backend. It performs the administrator login with the specified login information or with the Cypress environment variables. The user must be a member of the 'Manager', 'Administrator' or 'Super Users' group to be able to log in to the backend.

By default, a Cypress session is used to cache and restore session data for this user's backend login across Cypress test specs. This speeds up the entire test suite.

See also doAdministratorLogout and doFrontendLogin.

Usage
cy.doAdministratorLogin(user, password, useSnapshot)
Arguments
Example
// Admin login without saving the session
cy.doAdministratorLogin("admin-user", "admin-user-password", false);

doAdministratorLogout

The command doAdministratorLogout initiates user logout process from the backend. All session data (both backend and frontend, for all users) is being cleaned up. A user must be logged into the backend.

See also doAdministratorLogin.

Usage
cy.doAdministratorLogout()
Example
// Do explicit logout to start with new login
cy.doAdministratorLogout();
// Check login action log
cy.doAdministratorLogin("admin-user", "admin-user-password")
  .visit('/administrator/index.php?option=com_actionlogs&view=actionlogs')
  .contains('User admin-user logged in to admin');

doFrontendLogin

The command doFrontendLogin initiates a user login to the frontend. It performs the user login with the specified login information or with the Cypress environment variables. The user must belong to a user group that has the necessary permissions to access the frontend.

By default, Cypress session is used to cache and restore session data for this users frontend login across Cypress test specs. This speeds up the entire test suite.

See also doFrontendLogout and doAdministratorLogin.

Usage
cy.doFrontendLogin(user, password, useSnapshot)
Arguments
Example
// Checks the welcome message with the user's name
cy.doFrontendLogin()
  .visit("/")
  .contains(`Hi ${Cypress.env('name')}`).should('be.visible');

doFrontendLogout

The command doFrontendLogout initiates frontend logout process. All session data (both backend and frontend, for all users) is being cleaned up. A user must be logged into the frontend.

See also doFrontendLogin.

Usage
cy.doFrontendLogin()
Example
// Can log in and out with the default credentials
cy.doFrontendLogin(null, null, false)
  .doFrontendLogout();

createUser

The command createUser creates a new user entry in Joomla. If the user name already exists, the creation will fail. The user entry is activated after creation.

Usage
cy.createUser(name, username, password, email, userGroup)
Arguments
Example
cy.doAdministratorLogin()
  .createUser("Alice in Wonderland", "alice", "CheshireSmile", "alice@in.wonderland");

Joomla Commands

installJoomla

After Joomla download, the command installJoomla runs all 'Joomla Installer' steps to install a Joomla instance on a web server. The installed language pack 'en-GB' is English (United Kingdom). After the command, the 'Joomla Installer' screen remains open in the session.

:warning: The /installation folder is not deleted after the installation. This allows multiple runs of the command installJoomla. For production sites the /installation folder needs to be deleted after installation.

See also installJoomlaMultilingualSite.

Usage
cy.installJoomla(config)
Arguments

:point_right: The use of db_port for a non-standard database port currently (August 2024) only works for MariaDB and MySQL.

Example
const config = {
     sitename: "Sample Joomla Site",
         name: "Joomla Administrator",
     username: "admin",
     password: "admin-password",
        email: "sampleuser@example.com",
      db_type: "MySQLi",
      db_host: "localhost",
      db_port: "3316"
      db_user: "joomla",
  db_password: "joomla-db-user-password",
      db_name: "sample_joomla",
    db_prefix: "sjs_"
}
cy.installJoomla(config);

installJoomlaMultilingualSite

The command installJoomlaMultilingualSite first runs the installJoomla command and continues the 'Joomla Installer' with installation of additional languages.

The /installation folder is deleted after the installation. It is verified that the URL path '/installation' receives an error 404 – Not Found. This command can only run once.

See also installJoomla.

Usage
cy.installJoomlaMultilingualSite(config, languages)
Arguments

:point_right: The use of db_port for a non-standard database port currently (August 2024) only works for MariaDB and MySQL.

Example
const config = {
     sitename: "Sample Joomla Site",
         name: "Joomla Administrator",
     username: "admin",
     password: "admin-password",
        email: "sampleuser@example.com",
      db_type: "MySQLi",
      db_host: "localhost",
      db_port: "3316",
      db_user: "joomla",
  db_password: "joomla-db-user-password",
      db_name: "sample_joomla",
    db_prefix: "sjs_"
}
const languages = ["German", "Japanese", "Spanish", "Ukrainian"];
cy.installJoomlaMultilingualSite(config, languages);

'Welcome to Joomla!' guided tour overlay window

cancelTour

With Joomla 5.1 the 'Welcome to Joomla!' guided tour starts automatically the very first time an user logs in to the Administrator backend. As this overlay window (see screenshot) blocks interaction with the underlying content, it must be closed. The overlay of the guided tour is closed with the cancelTour command.

In Joomla 5.1 the overlay window is closed with the cancel X-button. Since Joomla 5.2 there is an additional button Hide Forever. This is preferably used.

A Joomla administrator must be logged in for this. This command can only be executed once after a Joomla installation and only from version 5.1 upwards.

Usage
cy.cancelTour()
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.cancelTour();

disableStatistics

To remove the system message container 'Enable Joomla Statistics?' from the backend home dashboard the command disableStatistics deactivates the plugin 'System - Joomla! Statistics'. This command can be executed multiple times without causing issues. The Joomla administrator must be logged in to do this.

Usage
cy.disableStatistics()
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.disableStatistics();

setErrorReportingToDevelopment

The command setErrorReportingToDevelopment sets 'Error Reporting' on the 'Server' tab in the Joomla 'Global Configuration' to 'Maximum'. This command can be executed multiple times without causing issues. The Joomla administrator must be logged in to do this.

See also checkForPhpNoticesOrWarnings.

Usage
cy.setErrorReportingToDevelopment()
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.setErrorReportingToDevelopment();

Extensions Commands

installExtensionFromFolder

The command installExtensionFromFolder installs an extension in Joomla from a folder on the server. It navigates to the Joomla extension installer page, selects 'Install from Folder', fills in the path to the folder and completes the installation. The Joomla administrator must be logged in to do this.

Usage
cy.installExtensionFromFolder(path, type)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.installExtensionFromFolder("/joomla-module/src"); // as mounted in docker image

installExtensionFromUrl

The command installExtensionFromUrl installs an extension in Joomla from the given URL. It navigates to the Joomla extension installer page, selects 'Install from URL', fills in URL and completes the installation. The Joomla administrator must be logged in to do this.

Usage
cy.installExtensionFromUrl(url, type)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.installExtensionFromUrl("https://server.org/download/joomla-module.zip");

installExtensionFromFileUpload

The command installExtensionFromFileUpload installs an extension in Joomla from the specified package file in the Cypress Test Runner (web browser) environment. It navigates to the Joomla extension installer page, selects 'Upload Package File', fills the path to the file and completes the installation. The Joomla administrator must be logged in to do this. It is based on the custom command attachFile from the cypress-file-upload module.

Usage
cy.installExtensionFromFileUpload(file, type)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.installExtensionFromFileUpload("manual-examples.zip");

uninstallExtension

The command uninstallExtension removed an installed extension from Joomla. It ensures that there are no warning messages after deletion and checks afterwards that the extension no longer exists. The Joomla administrator must be logged in to do this.

Usage
cy.uninstallExtension(extensionName)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.uninstallExtension("Joomla module tutorial");

installLanguage

The command installLanguage installs a language pack in Joomla. The Joomla administrator must be logged in to do this. If the language pack is already installed, a reinstall is executed.

Usage
cy.installLanguage(languageName)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
// Install German for Switzerland with language tag
cy.installLanguage("de-CH");
// or install with language name
// cy.installLanguage("German, Switzerland");

enablePlugin

The command enablePlugin activates a Joomla plugin so that is becomes operational. If the specified plugin is already activated, the command will fail. The Joomla administrator must be logged in to do this.

Usage
cy.enablePlugin(pluginName)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.enablePlugin("Authentication - LDAP");

setModulePosition

The command setModulePosition sets a module display position within a Joomla template. The Joomla administrator must be logged in to do this.

Usage
cy.setModulePosition(module, position)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.setModulePosition("module-name", "sidebar-right");

publishModule

The command publishModule makes the module active and visible on the Joomla website. It navigates to the Joomla modules list, selects the module and sets the status to Published. It does not matter whether the module has already been published, as the module found is initially set to the status Unpublished. The Joomla administrator must be logged in to do this.

Usage
cy.publishModule(module)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.publishModule("Breadcrumbs");

displayModuleOnAllPages

The command displayModuleOnAllPages sets the menu assignment for the specified module for all pages in Joomla. The Joomla administrator must be logged in to do this.

Usage
cy.displayModuleOnAllPages(module)
Arguments
Example
cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.displayModuleOnAllPages("Login Form");

Support Commands

clickToolbarButton

The command clickToolbarButton clicks on a Joomla backend button by using an internal mapping table. For example the button 'Enable' is mapped to click on CSS selector `#toolbar-publish button'. For the button 'transition' it is possible to give an additional subselector string.

Usage
cy.clickToolbarButton(button, subselector)
Arguments
Example
cy.clickToolbarButton('Save & Close');

checkForPhpNoticesOrWarnings

The command checkForPhpNoticesOrWarnings checks for PHP notices and warnings in the HTML page source. It looks for keywords such as 'Deprecated' in bold followed by a colon. If such a styled keyword is found, the Cypress test fails with the PHP problem message. Looking for:

To make the PHP notes and warnings visible, the command setErrorReportingToDevelopment needs to be executed once beforehand.

:point_right: This command could be used in Cypress afterEach() to run after each test block.

See also setErrorReportingToDevelopment.

Usage
cy.checkForPhpNoticesOrWarnings()
Example
cy.checkForPhpNoticesOrWarnings();

checkForSystemMessage

The command checkForSystemMessage checks that a Joomla system message exists, is visible and contains a specific text.

Usage
cy.checkForSystemMessage(contain)
Arguments
Example

This command is used, for example, in the command installExtensionFromFolder to check if the installation of the extension was successful by checking if there is a system message containing "was successful".

cy.checkForSystemMessage('was successful');

searchForItem

The command searchForItem searches for a specific name in a Joomla list and clicks on the checkbox at the beginning of the corresponding list entry. As a prerequisite, the list must be open. The search field is used to avoid paging. The list entry being searched for is selected as the result.

Usage
cy.searchForItem(name)
Arguments
Example

This command is used, for example, in the enablePlugin command to select the plugin to be enabled.

cy.doAdministratorLogin("admin-user", "admin-user-password");
cy.visit('/administrator/index.php?option=com_plugins');
cy.searchForItem(pluginName);

setFilter

The command setFilter sets a filter on the list view in Joomla.

Usage
cy.setFilter(name, value)
Arguments
Example

In the following example, the list of user entries is filtered to "State: Activated":

// Using 'username' and 'password' from Cypress.env()
cy.doAdministratorLogin()
  // Open user list
  .visit('/administrator/index.php?option=com_users')
  // Only enabled user entries
  .setFilter('state', 'Enabled');

checkAllResults

Joomla list views are page by page. The command checkAllResults displays all list entries in one page by marking 'All' entries in the Joomla list view. As a prerequisite, the list must be open.

Usage
cy.checkAllResults()
Example
// Show all extensions in one single page:
cy.doAdministratorLogin
  .visit('/administrator/index.php?option=com_installer&view=manage')
  .checkAllResults();

createMenuItem

The command createMenuItem creates a new menu item in Joomla.

The Language parameter is only used if the Joomla website is set up multingual. This means that at least one additional language must be installed and the "System - Language Filter" plugin must be activated. Otherwise, the language selection is ignored. For the language parameter the language name (e.g. "Japanese (Japan)") or the language tag (e.g. "ja-JP") can be used. Default is "All".

Usage
cy.createMenuItem(menuTitle, menuCategory, menuItem, menu, language)
Arguments
Example

The following code creates a menu entry 'Spotlight Story' for featured articles as a Japanese menu entry, for the Japanese language and in the Japanese menu.

// Create Japanese menu item 'Spotlight Stories'
cy.doAdministratorLogin("admin-user", "admin-user-password");
  .createMenuItem('スポットライト・ストーリー', 'Articles', 'Featured Articles', 'Menu 日本語', 'ja-JP');

createCategory

The command createCategorycreates a new category in Joomla with the specified title. Categories can be created to organise:

If the category with the specified title and extension already exists, the command will fail.

Usage
cy.createCategory(title, extension)
Arguments
Example
// Create Monday banners category
cy.doAdministratorLogin()
  .createCategory("Monday Banners", "com_banners");

selectOptionInFancySelect

The command selectOptionInFancySelect selects an option from a fancy select field in Joomla.

Usage
cy.selectOptionInFancySelect(selectId, option)
Arguments
Example
cy.selectOptionInFancySelect("#jform_countries", "Germany");

toggleSwitch

The command toggleSwitch toggles a switch field in Joomla identified by fieldName to the specified value name.

Usage
cy.toggleSwitch(fieldName, valueName)
Arguments
Example
cy.toggleSwitch("Published", "Yes");

Common Commands

iframe

The command iframe works with iframe elements. It waits for the iframe to be fully loaded and resolves with the iframe's body content, allowing to interact with elements inside the iframe seamlessly.

Usage
cy.get('iframe').iframe()
Example
cy.get('iframe').iframe().then($body => {
  // You can now interact with the iframe's body as a Cypress element
  cy.wrap($body).find('selector-within-iframe').click();
});

Usage Examples from the Field

You can see the practical use in various projects: