Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add E2E testing using Cypress #117

Merged
merged 18 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
- develop

jobs:
e2e:
cypress:
runs-on: ubuntu-latest

steps:
Expand All @@ -27,4 +27,4 @@ jobs:
run: npm run env start

- name: Test
run: npm run test:e2e
run: npm run cypress:run
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ vendor
composer.lock
phpunit.xml
.idea
package-lock.json

# Project Files
dist
Expand Down Expand Up @@ -34,3 +33,6 @@ Desktop.ini

# E2E testing
artifacts
tests/cypress/downloads
tests/cypress/screenshots
tests/cypress/videos
17,322 changes: 17,322 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@
"deploy": "npm install && npm run build",
"prepare": "husky install",
"env": "wp-env",
"test:e2e": "wp-scripts test-e2e --config=tests/e2e/jest.config.js",
"test:e2e:watch": "npm run test:e2e -- --puppeteer-interactive",
"test:e2e:dev": "npm run test:e2e -- --puppeteer-interactive --puppeteer-devtools"
"cypress:open": "cypress open --config-file tests/cypress/config.json",
"cypress:run": "cypress run --config-file tests/cypress/config.json"
},
"lint-staged": {
"*.php": [
"./vendor/bin/phpcs --standard=10up-Default --extensions=php --warning-severity=8 -s --runtime-set testVersion 5.3"
],
"tests/**/*.js": [
"./node_modules/.bin/wp-scripts format",
"git add"
]
},
"devDependencies": {
"@wordpress/e2e-test-utils": "^5.4.1",
"@wordpress/env": "^4.1.0",
"@wordpress/prettier-config": "^1.1.0",
"@wordpress/scripts": "^17.1.0",
"cypress": "^8.3.0",
"cypress-file-upload": "^5.0.8",
"husky": "^7.0.1",
"lint-staged": "^11.1.1",
"puppeteer-testing-library": "^0.5.0"
"lint-staged": "^11.1.1"
},
"wp-env": {
"plugins": [
"."
]
}
"prettier": "@wordpress/prettier-config"
}
10 changes: 10 additions & 0 deletions tests/cypress/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"fixturesFolder": "tests/cypress/fixtures",
"integrationFolder": "tests/cypress/integration",
"pluginsFile": "tests/cypress/plugins/index.js",
"screenshotsFolder": "tests/cypress/screenshots",
"videosFolder": "tests/cypress/videos",
"downloadsFolder": "tests/cypress/downloads",
"supportFile": "tests/cypress/support/index.js",
"video": false
}
5 changes: 5 additions & 0 deletions tests/cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
Binary file added tests/cypress/fixtures/example.mp3
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/cypress/integration/admin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe( 'Admin can login and make sure plugin is activated', () => {
it( 'Can activate plugin if it is deactivated', () => {
cy.visitAdminPage( 'plugins.php' );
cy.get( '#deactivate-simple-podcasting' ).click();
cy.get( '#activate-simple-podcasting' ).click();
cy.get( '#deactivate-simple-podcasting' ).should( 'be.visible' );
} );
} );
26 changes: 26 additions & 0 deletions tests/cypress/integration/block.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe( 'Admin can publish posts with podcast block', () => {
it( 'Can insert the block and publish the post', () => {
cy.visitAdminPage( 'post-new.php' );
cy.get( 'button[aria-label="Close dialog"]' ).click();
cy.get( '#post-title-0' ).click().type( 'Test episode' );
cy.get( '.edit-post-header-toolbar__inserter-toggle' ).click();
cy.get( '#block-editor-inserter__search-0' ).type( 'Podcast' );
cy.get( '.editor-block-list-item-podcasting-podcast' ).click();
cy.get( '.edit-post-header-toolbar__inserter-toggle' ).click();
cy.get( '.wp-block-podcasting-podcast input[type="file"]' ).attachFile(
'example.mp3'
);
cy.get( '.wp-block-podcasting-podcast audio' )
.should( 'have.attr', 'src' )
.and( 'include', 'example' );
cy.get( '.editor-post-publish-panel__toggle' ).click();
cy.get( '.editor-post-publish-button' ).click();
cy.get( '.components-snackbar', { timeout: 10000 } ).should(
'be.visible'
);
cy.get( 'a.components-button.components-snackbar__action' ).click();
cy.get( '.wp-block-podcasting-podcast audio' )
.should( 'have.attr', 'src' )
.and( 'include', 'example' );
} );
} );
23 changes: 23 additions & 0 deletions tests/cypress/integration/taxonomy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
describe( 'Admin can create and update podcast taxonomy', () => {
it( 'Can see taxonomy menu item', () => {
cy.visitAdminPage();
cy.get(
'#toplevel_page_edit-tags-taxonomy-podcasting_podcasts-amp-podcasts-true .wp-menu-name'
).should( 'have.text', 'Podcasts' );
} );

it( 'Can visit taxonomy page', () => {
cy.visitAdminPage(
'edit-tags.php?taxonomy=podcasting_podcasts&podcasts=true'
);
cy.get( '.wp-heading-inline' ).should( 'have.text', 'Podcasts' );
cy.get( '.form-wrap h2' ).should( 'have.text', 'Add New Podcast' );
} );

it( 'Can add a new taxonomy', () => {
cy.visitAdminPage(
'edit-tags.php?taxonomy=podcasting_podcasts&podcasts=true'
);
cy.get( '#tag-name' ).click().type( 'Remote work{enter}' );
} );
} );
33 changes: 33 additions & 0 deletions tests/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

const { readConfig } = require( '@wordpress/env/lib/config' );

/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = async ( on, config ) => {
wpEnvConfig = await readConfig( 'wp-env' );

if ( wpEnvConfig ) {
const port = wpEnvConfig.env.tests.port || null;

if ( port ) {
config.baseUrl = wpEnvConfig.env.tests.config.WP_TESTS_DOMAIN;
}
}

return config;
};
46 changes: 46 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })

import 'cypress-file-upload';

Cypress.Commands.add(
'login',
( username = 'admin', password = 'password' ) => {
cy.visit( `/wp-admin/` );
cy.get( 'input#user_login' ).clear();
cy.get( 'input#user_login' ).click().type( username );
cy.get( 'input#user_pass' ).type( `${ password }{enter}` );
}
);

Cypress.Commands.add( 'visitAdminPage', ( page = 'index.php' ) => {
cy.login();
if ( page.includes( 'http' ) ) {
cy.visit( page );
} else {
cy.visit( `/wp-admin/${ page.replace( /^\/|\/$/g, '' ) }` );
}
} );
20 changes: 20 additions & 0 deletions tests/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

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

// Alternatively you can use CommonJS syntax:
// require('./commands')
9 changes: 0 additions & 9 deletions tests/e2e/jest.config.js

This file was deleted.

12 changes: 0 additions & 12 deletions tests/e2e/specs/admin/activate-plugins.test.js

This file was deleted.