Registry / testing / drupal-test-session-cypress

drupal-test-session-cypress

JSON →
library1.1.66jsnpmunverified

This package, `drupal-test-session-cypress` (current version 1.1.66), provides Cypress commands that streamline interaction with Drupal's Test Session module. Its primary function is to extend the Cypress API with `cy.drupalSession()`, enabling testers to easily manage Drupal user sessions and state directly within their end-to-end tests. This abstraction simplifies common testing scenarios like logging in users, asserting permissions, and resetting session data, which otherwise would require complex custom commands or direct API calls to the Drupal backend. The package is typically installed as a development dependency and integrates by requiring it within Cypress's `support/index.js` (or `.ts`) file, making its commands globally available to Cypress tests. It offers a focused solution for projects leveraging Drupal and Cypress, differentiating itself by tightly coupling with the specific Drupal module. The Drupal Test Session module itself is marked as 'minimally maintained' and 'feature-complete' on Drupal.org, and this Cypress wrapper aligns with that stability.

npm install drupal-test-session-cypress
INSTALL
IMPORT
SIG · DRUPAL-TEST-SESSIO
D
drupal-test-session-cypress
testingjavascriptv1.1.66
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

(global augmentation)
require('drupal-test-session-cypress');
import { drupalSession } from 'drupal-test-session-cypress';
This package augments the global Cypress `cy` object. It should be `require()`-d in your `cypress/support/index.js` or `cypress/support/index.ts` file for global availability. No direct named imports are typically used in test files for the commands themselves.
Cypress
Cypress.env('DRUPAL_BASE_URL')
process.env.DRUPAL_BASE_URL
Environment variables within Cypress tests should be accessed via `Cypress.env()` after being configured in `cypress.json` or `cypress.env.json`, not directly from `process.env` in browser contexts.

Demonstrates how to include the package in the Cypress support file and provides runnable Cypress tests for logging in and logging out Drupal users using `cy.drupalSession()`.

// cypress/support/index.ts or cypress/support/e2e.ts // Ensure this line is present to make drupalSession commands available. require('drupal-test-session-cypress'); // Any other global Cypress configurations or custom commands can go here. // cypress/e2e/drupal-session.cy.ts (example test file) describe('Drupal Session Management with Cypress', () => { // Configure DRUPAL_BASE_URL in cypress.config.ts/js or cypress.env.json // e.g., set CYPRESS_DRUPAL_BASE_URL=http://your-drupal.local const drupalBaseUrl = Cypress.env('DRUPAL_BASE_URL') || 'http://localhost:8888'; beforeEach(() => { // It's often necessary to visit the base URL before session commands // to ensure Cypress is within the correct origin for cookie and session handling. cy.visit(drupalBaseUrl); }); it('should allow logging in an administrator user', () => { // Use cy.drupalSession to create and log in a user with the 'administrator' role. // This command interacts with the Drupal Test Session module on the backend. cy.drupalSession({ user: { name: 'testadmin', mail: 'testadmin@example.com', roles: ['administrator'], }, // Set kill to false to maintain the session for subsequent commands or tests. kill: false, }).then((userSession) => { // Assertions after successful login expect(userSession).to.have.property('name', 'testadmin'); cy.url().should('include', '/user'); cy.get('.toolbar-icon-user').should('be.visible'); // Example for Drupal admin toolbar cy.contains('Hello testadmin').should('be.visible'); // Check for a greeting (adjust selector as needed) }); }); it('should reset the Drupal session, effectively logging out', () => { // First, log in a user to establish a session. cy.drupalSession({ user: { name: 'tempuser', roles: ['authenticated'] }, kill: false, }); // Now, call drupalSession with kill: true to destroy the session. cy.drupalSession({ kill: true }).then(() => { // Verify logout by checking for a login form or absence of user-specific elements. cy.visit(drupalBaseUrl); // Revisit to ensure the page reflects the new session state cy.get('a[href="/user/login"]').should('be.visible'); // Check for login link cy.get('.toolbar-icon-user').should('not.exist'); cy.contains('tempuser').should('not.exist'); }); }); });
Debug
Known issues
gotchaThis package is a client for the Drupal Test Session module. The module must be installed and properly configured on your Drupal site for the Cypress commands to function. Without the Drupal module, the commands will fail with backend errors.
fix
Ensure the 'Test Session' module is enabled and accessible at the `/test-session` endpoint on your Drupal site. Consult the Drupal Test Session module documentation for setup instructions.
affects: >=1.0.0
gotchaThe `DRUPAL_BASE_URL` environment variable (accessed via `Cypress.env('DRUPAL_BASE_URL')`) must be correctly configured if your Drupal site is not running on `http://localhost:8888`. Incorrect configuration will lead to commands targeting the wrong URL.
fix
Set `DRUPAL_BASE_URL` in your `cypress.config.ts` or `cypress.env.json` file, or as a system environment variable prefixed with `CYPRESS_` (e.g., `CYPRESS_DRUPAL_BASE_URL=https://my-drupal.com`).
affects: >=1.0.0
gotchaThe package must be `require()`-d in your `cypress/support/index.js` or `cypress/support/e2e.ts` file. If this step is missed, Cypress will report that `cy.drupalSession` is not a function, as the global commands will not have been registered.
fix
Add `require('drupal-test-session-cypress');` to your primary Cypress support file (e.g., `cypress/support/e2e.ts`).
affects: >=1.0.0
gotchaWhen using `cy.drupalSession()` within tests, it's often crucial to ensure that Cypress is currently visiting the Drupal site's origin (e.g., `cy.visit(drupalBaseUrl)` in a `beforeEach` hook). Without being on the correct origin, cookie and session management commands might behave unexpectedly or fail due to cross-origin policies.
fix
Include `cy.visit(Cypress.env('DRUPAL_BASE_URL'))` or a specific Drupal page in your `beforeEach` hook before making `cy.drupalSession()` calls.
affects: >=1.0.0
Errors
Common errors & fixes
CypressError: `cy.drupalSession` is not a function.
The `drupal-test-session-cypress` package was not correctly required in the Cypress support file, so its commands were not registered globally.
fix
Add `require('drupal-test-session-cypress');` to your `cypress/support/index.js` or `cypress/support/e2e.ts` file.
Error: "DRUPAL_BASE_URL" is not defined or is empty. Please set the DRUPAL_BASE_URL environment variable.
The `DRUPAL_BASE_URL` environment variable, which `drupal-test-session-cypress` relies on, has not been set or is empty in the Cypress configuration.
fix
Define `DRUPAL_BASE_URL` in your `cypress.config.ts/js` (e.g., `env: { DRUPAL_BASE_URL: 'http://my-drupal.local' }`), `cypress.env.json`, or as a system environment variable prefixed with `CYPRESS_` (e.g., `CYPRESS_DRUPAL_BASE_URL=http://my-site.com`).
CypressError: cy.request() failed with status code 404: Not Found
The Drupal Test Session module endpoint (usually `/test-session`) was not found, likely because the module is not installed, not enabled, or the `DRUPAL_BASE_URL` is incorrect.
fix
Verify that the Drupal Test Session module is installed and enabled on your Drupal site. Double-check the `DRUPAL_BASE_URL` in your Cypress configuration to ensure it points to the correct Drupal instance.
Upgrade
Version history
1.1.66latest on npm
Audit
Dependencies
cypressrequiredRequired as the underlying testing framework that this package extends. This is typically a devDependency.
Drupal Test Session modulerequiredThis package is a client-side wrapper; the corresponding Drupal module must be installed and configured on the Drupal backend for `cy.drupalSession()` commands to function.
Agent activity
6 hits · last 30 days
node
6
Resources
drupal-test-session-cypress — npm install drupal-test-session-cypress · libregistry