kanopi/shrubs 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kanopi/shrubs

Composer 安装命令:

composer require --dev kanopi/shrubs

包简介

A set of default support commands to get you jumpstarted configuring Cypress for Drupal.

README 文档

README

shrubs

Shrubs (Drupal Cypress Support Commands)

Common support commands for Cypress when interacting with Drupal.

Table of Contents

Requirements

  • Cypress installed on your local or CI.
  • @TODO Document how we have Cypress set up.

Installation

Install/update composer installers.

Add two entries in composer.json for an install-type and its path:

"installer-types": ["cypress-support"],
"installer-paths": {
 // existing entries omitted...
 "tests/cypress/cypress/support/{$name}": [
   "type:cypress-support"
 ]
}

Tell Cypress where to import the tests

In the support folder for where your Cypress tests are located, edit commands.js and add the following:

// Import commands.js using ES2015 syntax:
import './shrubs/commands'

Requiring Shrubs using Composer

The Shrubs repository is available via Packagist.

Once you have completed the steps above, run the following command:

composer require kanopi/shrubs

Update your CI process to...

@TODO (See what Paul did on Parks)

Available Commands

Drupal Cypress autocomplete

Will select the first match from an autocomplete field.

cy.autocomplete('input[data-drupal-selector="edit-field-episode-show-0-target-id"]', 'Term Name')

Drupal Cypress ckEditor get

Gets the value of a ckeditor instance.

cy.ckeditorGet('#edit-body-wrapper').should('contain', 'hello world')

Drupal Cypress ckEditor type

Set the value of a ckeditor instance.

cy.ckeditorType('#field_body-wrapper', 'hello world');

Drupal Cypress drush

Runs Drush commands in multiple environments. With the correct configuration it can target the following:

  • Docksal
  • Lando
  • DDEV
  • Pantheon
  • Tugboat
cy.drush('status');

Config examples

Set these as environment variables or in your cypress.env.json Docksal

{
  "DRUSH_IS_DOCKSAL" : true
}

Lando

{
  "DRUSH_IS_LANDO" : true
}

DDEV

{
  "DRUSH_IS_DDEV" : true
}

Pantheon

In the format of PANTHEON_SITE_ID.ENVIRONMENT_ID

{
  "DRUSH_IS_PANTHEON" : "mysite.pr-123"
}

Tugboat

{
  "DRUSH_IS_TUGBOAT" : "12345abcdef",
  "TUGBOAT_INSTANCE_ID" : "1234567890"
}
Tugboat CLI

The Tugboat CLI needs a little extra help being installed in AMD64 architecture. For example if you are install the CLI within a CI/CD system like CircleCI or GitHub Actions.

sudo dpkg --add-architecture amd64
sudo apt-get update
sudo apt-get install libc6:amd64 libstdc++6:amd64
wget https://dashboard.tugboatqa.com/cli/linux/tugboat.tar.gz
sudo tar -zxf tugboat.tar.gz -C /usr/local/bin/

Drupal Cypress login

Login through the default Drupal login form. Sets a default login but also passing custom login details

cy.login(); // login as a default user.
cy.login('user', 'password'); // as a specific user

Assuming there is some other process to create the user.

Drupal Cypress login as a specific user

Uses a Drush one time login links to login as a specific user.

cy.loginOneTimeLink('myusername');

Drupal Cypress logout

Logs out of the current session

cy.logout();

Ajax Click

There a clicks that can generate a blocking ajax request. I.E. Opening modals or slideouts that load content with an ajax request.

The function will wrap an intercept/wait combination around the click to make sure the tests don't continue until the ajax request as completed.

It's also meant to deal with the anti-pattern of using wait() for clicks that trigger ajax requests.

Example

cy.ajaxClick("a.product-name", '/jsonapi/*/**')

This replaces code that would look like this.

const jsonApiRequest5 = 'jsonApiRequest' + Math.random();
cy.intercept('GET', '/jsonapi/*/**').as(jsonApiRequest5)
cy.get('a.product-name').click();
cy.wait('@' + jsonApiRequest5).its('response.statusCode').should('eq', 200)

or

cy.get('a.product-name').click();
cy.wait(5000)

Drupal Cypress add item to media library

Uploads a file to the media library and selects it in the field.

Can optionally set the type of media uploaded if there is more than one type available.

Files are expected to be in the fixtures folder at the same level as support and e2e. In most cases, that will be /tests/cypress/cypress/fixtures.

cy.mediaLibraryAdd('#field_media_assets-media-library-wrapper', 'sample.png');
cy.mediaLibraryAdd('#field_media_assets-media-library-wrapper', 'sample.mp3', 'audio');

Drupal Cypress select item in the media library

Open a media browser modal and selects an existing media item

Can optionally set the type of media uploaded if there is more than one type available.

Files are expected to be in the fixtures folder.

cy.mediaLibrarySelect('#field_media_assets-media-library-wrapper', 'sample.png');
cy.mediaLibrarySelect('#field_media_assets-media-library-wrapper', 'sample.png', 'image');

Drupal Cypress upload file

Upload a file through a file field Files should be in the fixtures folder.

cy.uploadFile('#file-field-wrapper', 'example.png');`

logAndStore(message)

Logs a message and stores it in an internal array to be retrieved later using cy.logSummary();

cy.logAndStore('This is a log message.');

logSummary()

Outputs all stored log messages at the end of a test run.

cy.logSummary();

Example with after() Hook

describe('Example Test with log summary', () => {
  beforeEach(() => {
    cy.visit('/example-page');
  });

  it('should log multiple steps', () => {
    cy.logAndStore('Step 1: Visiting the page.');
    cy.logAndStore('Step 2: Clicking a button.');
    cy.logAndStore('Step 3: Validating output.');
  });

  after(() => {
    cy.logSummary();
  });
});

Pantheon interstitial page and Cypress tests

In May 2025 Pantheon added an Interstitial page to projects that are at the Sandbox level. Kanopi has the following work around of overwriting the visit() command.

In e2e.js

/**
 * Overwrite the visit() command to support setting headers globally.
 */
Cypress.Commands.overwrite("visit", (originalVisit, url, options = {}) => {
  const globalHeaders = Cypress.env('visitHeaders') || {};

  // Combine global and specific headers from the unique call of visit().
  const headers = Object.assign({}, globalHeaders, options.headers);

  // Call the real visit with the merged headers
  return originalVisit(url, { ...options, headers });
});

In your cypress.config.js you can add the following environment variable.

env: {
  "visitHeaders" : {
    "Deterrence-Bypass" : "1"
  }
}

Now when the visit() command is done you dont have to worry about a cookie being set prior.

If you want to set the cookie to bypass the page later you can do so with.

cy.setCookie("Deterrence-Bypass", "1");

Issues

For issues and support, please use the issue queue at https://www.drupal.org/project/issues/shrubs?categories=All

Maintainers

Current maintainers:

This project is sponsored by:

kanopi/shrubs 适用场景与选型建议

kanopi/shrubs 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 367.85k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「drupal」 「automation」 「e2e」 「cypress」 「support-commands」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kanopi/shrubs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kanopi/shrubs 我们能提供哪些服务?
定制开发 / 二次开发

基于 kanopi/shrubs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 367.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 31
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 13
  • Forks: 1
  • 开发语言: JavaScript

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2023-09-28