Skip to content

Latest commit

 

History

History
374 lines (314 loc) · 13.4 KB

CHEATSHEET.md

File metadata and controls

374 lines (314 loc) · 13.4 KB

shrubs

Shrubs Cypress Commands Cheatsheet

Administration

Systems Testing

Content Type Testing

Creating a Node

Deleting a Node

  • Delete a node from node page while logged in: Code Snippet
  • Delete a node from the node edit page while logged in: Code Snippet

User Role Testing

The following commands generally best created as support commands.

Then multiple e2e tests can re-use them like this:

  • Test a user/user role's ability to create multiple content types: Code Snippet

Code Snippets

Create a Paragraph

cy.visit('/node/123/edit');
cy.get('.add-paragraph-button').click();
cy.get('#edit-paragraph-field-name').type('My Paragraph Name');
// Fill in other paragraph fields here
cy.get('#edit-submit').click();

Create multiple Paragraphs with additional fields

it('Create a test paragraph.', () => {
  // Login and visit specified node.
  cy.login('cypress', 'cypress')
  cy.visit('/node/289/edit');
  cy.wait(500);
  // Switch to the content tab.
  cy.get('#node-page-edit-form').contains('Content').click();
  // Get the Entity Reference Revisions (Paragraphs) field's dropdown button and click it.
  cy.get('[data-drupal-selector="edit-field-components-add-more"] .dropbutton__toggle').click();
  // Get the name of the paragraph type you want and click that.
  cy.get('input[name="field_components_image_and_text_add_more"]').click();
  // Now we're in the subform and we can fill out the fields, Title, link uri, and link title.
  cy.get('input[name="field_components[0][subform][field_title][0][value]"]').type('Title');
  cy.get('[data-drupal-selector="edit-field-components-0-subform-field-link-0-uri"]').type('www.google.com');
  cy.get('[data-drupal-selector="edit-field-components-0-subform-field-link-0-title"]').type('Google');
  // Add another paragraph and fill out the fields.
  cy.get('[data-drupal-selector="edit-field-components-add-more"] .dropbutton__toggle').click();
  cy.get('input[name="field_components_call_to_action_add_more"]').click();
  cy.get('input[name="field_components[1][subform][field_title][0][value]"]').type('Title');
  cy.get('[data-drupal-selector="edit-field-components-1-subform-field-link-0-uri"]').type('www.google.com');
  cy.get('[data-drupal-selector="edit-field-components-1-subform-field-link-0-title"]').type('Google');
  // Save the node.
  cy.get("#edit-submit--2--gin-edit-form").click();
})

Preview a Node

cy.visit('/node/123/edit');
cy.get('.preview-button').click();
// Verify that you are in preview mode
cy.contains('Preview Mode').should('be.visible');
// Perform actions or assertions in preview mode

Save a Node

cy.visit('/node/add/page');
// Fill in node fields
cy.get('#edit-title-0-value').type('Test Node Title');
cy.get('#edit-body-0-value').type('This is the content of the test node.');
cy.get('#edit-submit').click();
// Verify that the node has been successfully saved
cy.contains('The content has been saved.').should('be.visible');

Change a Node's Path

// Visit edit page for node
cy.visit('/node/123/edit');
// Select URL Alias tab and enter in new path alias
cy.get('#edit-path-0-alias').clear().type('new-path-alias');
cy.get('#edit-submit').click();
// Verify that the node's path alias has been successfully changed
cy.url().should('include', '/new-path-alias');

Add a Node to a Menu

// Visit edit page of node
cy.visit('/node/123/edit');
// Click on Menu settings tab
cy.get('.menu-settings-details summary').click();
// Select what menu you want it to be a part of
cy.get('#edit-menu-parent').select('Main Menu');
// Fill in other menu fields
// Save changes to node menu
cy.get('#edit-submit').click();

Include/Exclude a Node from an XML Sitemap

// Need to test and validate this example code
cy.visit('/node/123/edit');
// Click on xml sitemap tab dropdown
cy.get('.sitemap-settings-details summary').click();
// Click on dropdown tabs for sitemap
cy.get('#edit-xmlsitemap-include').click();
// Select dropdown tab option
cy.get('#your-dropdown-element').select('Option Text');
// Save changes to node
cy.get('#edit-submit').click();

Change Revision State

// Changes revision state to published
cy.visit('/node/1/edit');
cy.get('#edit-revision-state').as('revisionStateField');
cy.get('@revisionStateField').select('published');
cy.get('#edit-form').submit();
cy.get('#revision-state').should('contain', 'published');

Add to a Menu

// Visit content type edit page
cy.visit('/admin/structure/types/manage/blank')
cy.get('#edit-menu-settings').as('menuSettingsFieldset');
cy.get('@menuSettingsFieldset').find('#edit-menu-enabled').as('menu');
cy.get('@menu').check();
cy.get('#edit-submit').click();

Schedule a Publishing Time

cy.visit('node/add/article');
cy.get('#edit-title-0-value').type('My Article Title');
cy.get('#edit-scheduling-options').click();
cy.get('#edit-publish-on').type('2023-12-31 00:00:00');
cy.get('#edit-submit').click();
cy.get('#scheduled-publishing-time').should('contain', '2023-12-31 00:00:00');

Schedule a Unpublishing Time

cy.visit('node/1/edit');
cy.get('#edit-scheduling-options').click();
cy.get('#edit-unpublish-on').type('2023-12-31 00:00:00');
cy.get('#edit-submit').click();
cy.get('#scheduled-unpublishing-time').should('contain', '2023-12-31 00:00:00');

Write a Revision Log Message

cy.visit('node/1/edit');
cy.get('#edit-revision-information').click();
cy.get('#edit-revision-log-0-value').type('This is a revision log message.');
cy.get('#edit-submit').click();
cy.get('#revision-log-message').should('contain', 'This is a revision log message');

Open a field group tab in the admin

cy.visit('/node/[nodeID]/edit');
// Switch to the horizontal content tab.
cy.get('#node-page-edit-form').contains('Content').click()

Create a Content Type

// Visit any content type creation page.
cy.visit('/node/add/event')
// Set title, body, and image.
const nodeTitle = "TEST CONTENT - " +  randCompanyName();
cy.get("#edit-title-wrapper").type(nodeTitle);
const nodeBody = randLines();
cy.ckeditorType('#edit-body-wrapper', nodeBody);
cy.mediaLibrarySelect('#field_image-media-library-wrapper', 'image-sample_01.png', 'image')
cy.get("#edit-submit--2--gin-edit-form").click();
// Validate.
cy.get('main').should('contain', nodeTitle);
cy.get('main').should('contain', nodeBody);

Test if a user/user role can't create content

// User should hit a 403 error on a restricted node/add.
cy.request({
  url: '/node/add/event',
  followRedirect: false,
  failOnStatusCode: false
}).then((resp) => {
    expect(resp.status).to.eq(403)
})

Test a user/user role's ability to create multiple content types

// Create support commands for each content type and run in series with user role.
cy.login('cypress', 'cypress');
cy.createDocument();
cy.createEvent();
cy.createPage();
cy.createPerson();
cy.logout();

Test if User or User Role can delete a Content type

it('Test if user role can delete content type.', () => {
    // Login with role and visit node of specific content type.
    cy.login('roleToTestName', 'roleToTestPassword')
    cy.visit(nodePathOfContentType);
    // Click node Delete button.
    cy.get('.nav-link').contains('Delete').click();
    // Click confirm delete.
    cy.get('#edit-submit').contains('Delete').click();
    // Validate, we should be on the home page after deletion.
    cy.location('pathname').should('eq', '/');
})
it('Test if user role can't delete content type.', () => {
    // Login with role and visit node of specific content type.
    cy.login('roleToTestName', 'roleToTestPassword')
    cy.visit(nodePathOfContentType);
    // Assert that the button should not exist on the page.
    cy.get('.nav-link').contains('Delete').should('not.exist');
})

Delete a piece of content from the node page

Cypress.Commands.add('deleteContentFromNode', (nodeID) => {
  let nodePath = 'node/' + nodeID;
  // Visit a node
  cy.visit(nodePath);
  // Click node Delete button.
  cy.get('.nav-link').contains('Delete').click();
  // Click confirm delete.
  cy.get('#edit-submit').contains('Delete').click();
  // Validate, we should be on the home page.
  cy.location('pathname').should('eq', '/');
})

Delete a piece of content from the node edit page

// Visit the node edit page.
cy.visit('node/[nodeID]/edit?destination=/admin/content')
// Click node Delete button.
cy.get('#edit-delete--2').click();
// Create a unique variable name for the ajax listener.  Cypress does not like it being reused.
const deleteModalAjax = 'deleteModalAjax' + '#edit-delete--2' + Math.random();
cy.intercept('POST', '/media-library**').as(deleteModalAjax)
// Get the delete modal and delete the node.
cy.get('.ui-dialog-buttonpane').within(($modal) => {
  // Get the delete button in the modal and click.
  cy.get('.button--danger').contains('Delete', { matchCase: false }).click();
})
// Validate, we should be on the admin/content page.
cy.get('h1').should('contain', 'Content');

Check if you are on the front or home page

// Visit the home page
// Update this URL based on your application's structure
cy.visit('/');
// Check if the current URL is the home page
cy.location('pathname').should('eq', '/');

Tests based on Lighthouse mobile and dekstop viewport sizes

describe('Test based on Lighthouse viewport width.', () => {
  context('Desktop', () => {
    it('Desktop Test item', () => {
      // Vist the home slug and set to Lighthouse desktop viewport size.
      cy.visit('/')
      cy.viewport(940, 1350)
      // Do things.
    })
  })
  context('Mobile', () => {
    it('Mobile Test item', () => {
      // Vist the home slug and set to Lighthouse mobile viewport size.
      cy.visit('/')
      cy.viewport(940, 1350)
      // Do things.
    })
  })
})

Check Sendgrid works

describe('Check sendgrid is sending emails', () => {
    it('Login and send a test email', () => {
        cy.login()
        cy.visit('/admin/config/services/sendgrid/test')
        const mailinatorEmail = '1234567890@mailinator.com';
        cy.get('#edit-to').clear().type(mailinatorEmail);
        cy.get('#edit-submit').click()
        cy.get('.messages-list .messages--status').should('contain', 'SendGrid test email sent')    
    })
})