From 915a1b1181048256078f1405f347c979d673f434 Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 27 May 2022 18:24:30 +0300 Subject: [PATCH 1/2] Classic Create Post command --- src/commands/classic-create-post.ts | 50 +++++++++++ src/index.ts | 3 + .../integration/classic-create-post.test.js | 87 +++++++++++++++++++ .../integration/close-welcome-guide.test.js | 2 + .../open-document-settings.test.js | 2 + 5 files changed, 144 insertions(+) create mode 100644 src/commands/classic-create-post.ts create mode 100644 tests/cypress/integration/classic-create-post.test.js diff --git a/src/commands/classic-create-post.ts b/src/commands/classic-create-post.ts new file mode 100644 index 0000000..928ea51 --- /dev/null +++ b/src/commands/classic-create-post.ts @@ -0,0 +1,50 @@ +/** + * Classic Create Post + * + * @example + * ``` + * cy.classicCreatePost() + * ``` + */ +export const classicCreatePost = ({ + postType = 'post', + title = 'Test Post', + content = 'Test content', + status = 'publish', + beforeSave, +}: { + title: string; + postType?: string; + content?: string; + status?: string; + beforeSave?: CallableFunction; +}): void => { + cy.visit(`/wp-admin/post-new.php?post_type=${postType}`); + + cy.get('#title').click().clear().type(title); + + cy.get('#content_ifr').then($iframe => { + const doc = $iframe.contents().find('body#tinymce'); + cy.wrap(doc).find('p:last-child').type(content); + }); + + if ('undefined' !== typeof beforeSave) { + beforeSave(); + } + + cy.intercept('POST', '/wp-admin/post.php', req => { + req.alias = 'savePost'; + }); + + if ('draft' === status) { + cy.get('#save-post').click(); + } else { + cy.get('#publish').click(); + } + + cy.wait('@savePost').then(response => { + const body = new URLSearchParams(response.request?.body); + const id = body.get('post_ID'); + cy.wrap(id); + }); +}; diff --git a/src/index.ts b/src/index.ts index 86a93a9..f542d4d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ /// // Import commands. +import { classicCreatePost } from './commands/classic-create-post'; import { insertBlock } from './commands/insert-block'; import { closeWelcomeGuide } from './commands/close-welcome-guide'; import { wpCliEval } from './commands/wp-cli-eval'; @@ -22,6 +23,7 @@ import { createPost } from './commands/create-post'; declare global { namespace Cypress { interface Chainable { + classicCreatePost: typeof classicCreatePost; insertBlock: typeof insertBlock; closeWelcomeGuide: typeof closeWelcomeGuide; wpCliEval: typeof wpCliEval; @@ -43,6 +45,7 @@ declare global { } // Register commands +Cypress.Commands.add('classicCreatePost', classicCreatePost); Cypress.Commands.add('insertBlock', insertBlock); Cypress.Commands.add('closeWelcomeGuide', closeWelcomeGuide); Cypress.Commands.add('wpCliEval', wpCliEval); diff --git a/tests/cypress/integration/classic-create-post.test.js b/tests/cypress/integration/classic-create-post.test.js new file mode 100644 index 0000000..6e4311e --- /dev/null +++ b/tests/cypress/integration/classic-create-post.test.js @@ -0,0 +1,87 @@ +const { randomName } = require('../support/functions'); + +describe('Command: classicCreatePost', () => { + before(() => { + cy.login(); + cy.activatePlugin('classic-editor'); + }); + + it('Should be able to Classic Create Post', () => { + const title = 'Title ' + randomName(); + const content = 'Content ' + randomName(); + cy.classicCreatePost({ + title: title, + content: content, + }); + + cy.get('.notice-success').should('contain.text', 'Post published'); + cy.get('#title').should('have.value', title); + cy.get('#content_ifr').then($iframe => { + const doc = $iframe.contents().find('body#tinymce'); + cy.wrap(doc).should('contain.text', content); + }); + }); + + it('Should wrap post ID', () => { + const title = 'Title ' + randomName(); + const content = 'Content ' + randomName(); + cy.classicCreatePost({ + title: title, + content: content, + }).then(id => { + cy.visit(`/wp-admin/post.php?post=${id}&action=edit`); + cy.get('#title').should('have.value', title); + }); + }); + + it('Should perform beforeSave', () => { + const title = 'Title ' + randomName(); + const content = 'Content ' + randomName(); + const additional = 'Content ' + randomName(); + cy.classicCreatePost({ + title: title, + content: content, + beforeSave: () => { + cy.get('#content_ifr').then($iframe => { + const doc = $iframe.contents().find('body#tinymce'); + cy.wrap(doc) + .find('p:last-child') + .type('{enter}' + additional); + }); + }, + }); + + cy.get('.notice-success').should('contain.text', 'Post published'); + cy.get('#title').should('have.value', title); + cy.get('#content_ifr').then($iframe => { + const doc = $iframe.contents().find('body#tinymce'); + cy.wrap(doc) + .should('contain.text', content) + .should('contain.text', additional); + }); + }); + + it('Should save draft', () => { + const title = 'Title ' + randomName(); + const content = 'Content ' + randomName(); + cy.classicCreatePost({ + title: title, + content: content, + status: 'draft', + }); + + cy.get('.notice-success').should('contain.text', 'Post draft updated'); + }); + + it('Should be able to create page', () => { + const title = 'Title ' + randomName(); + const content = 'Content ' + randomName(); + cy.classicCreatePost({ + title: title, + content: content, + postType: 'page', + }); + + cy.get('.notice-success').should('contain.text', 'Page published'); + }); +}); diff --git a/tests/cypress/integration/close-welcome-guide.test.js b/tests/cypress/integration/close-welcome-guide.test.js index af9e577..087d136 100644 --- a/tests/cypress/integration/close-welcome-guide.test.js +++ b/tests/cypress/integration/close-welcome-guide.test.js @@ -2,6 +2,8 @@ describe('Command: closeWelcomeGuide', () => { before(() => { cy.login(); + cy.deactivatePlugin('classic-editor'); + // Disable Classic Editor if it's enabled cy.visit('/wp-admin/options-writing.php'); cy.get('body').then($body => { diff --git a/tests/cypress/integration/open-document-settings.test.js b/tests/cypress/integration/open-document-settings.test.js index bb513d6..cd3ea80 100644 --- a/tests/cypress/integration/open-document-settings.test.js +++ b/tests/cypress/integration/open-document-settings.test.js @@ -2,6 +2,8 @@ describe('Commands: openDocumentSettings*', () => { before(() => { cy.login(); + cy.deactivatePlugin('classic-editor'); + // Disable Classic Editor if it's enabled cy.visit('/wp-admin/options-writing.php'); cy.get('body').then($body => { From 928c010177346ed14493daac2832860f2f6e9f88 Mon Sep 17 00:00:00 2001 From: Max Lyuchin Date: Fri, 3 Jun 2022 16:41:26 +0300 Subject: [PATCH 2/2] command docs --- src/commands/classic-create-post.ts | 26 +++++++++++++++++--------- src/interface/post-data.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 src/interface/post-data.ts diff --git a/src/commands/classic-create-post.ts b/src/commands/classic-create-post.ts index 928ea51..df83e81 100644 --- a/src/commands/classic-create-post.ts +++ b/src/commands/classic-create-post.ts @@ -1,9 +1,23 @@ +import PostData from '../interface/post-data'; + /** - * Classic Create Post + * Create Post in Classic Editor + * + * @param postData - Post data. * * @example * ``` - * cy.classicCreatePost() + * cy.classicCreatePost({ + * title: 'Post title', + * content: 'Post content', + * beforeSave: () => { + * // Do something before save. + * }, + * postType: 'page', + * status: 'draft' + * }).then(postID => { + * cy.log(postID); + * }) * ``` */ export const classicCreatePost = ({ @@ -12,13 +26,7 @@ export const classicCreatePost = ({ content = 'Test content', status = 'publish', beforeSave, -}: { - title: string; - postType?: string; - content?: string; - status?: string; - beforeSave?: CallableFunction; -}): void => { +}: PostData): void => { cy.visit(`/wp-admin/post-new.php?post_type=${postType}`); cy.get('#title').click().clear().type(title); diff --git a/src/interface/post-data.ts b/src/interface/post-data.ts new file mode 100644 index 0000000..c1ebbbb --- /dev/null +++ b/src/interface/post-data.ts @@ -0,0 +1,29 @@ +/** + * Post Data + */ +export default interface PostData { + /** + * Post title + */ + title: string; + + /** + * Post type + */ + postType?: string; + + /** + * Post content + */ + content?: string; + + /** + * Post status + */ + status?: string; + + /** + * Before save callback + */ + beforeSave?: CallableFunction; +}