diff --git a/packages/e2e-test-utils/CHANGELOG.md b/packages/e2e-test-utils/CHANGELOG.md index f995fb1336bcf4..f56e818eb926a8 100644 --- a/packages/e2e-test-utils/CHANGELOG.md +++ b/packages/e2e-test-utils/CHANGELOG.md @@ -1,3 +1,9 @@ +## Master + +### New Features + +- Added `getPageError` function, returning a promise which resolves to an error message present in the page, if any exists. + ## 4.0.0 (2019-12-19) ### Breaking Changes diff --git a/packages/e2e-test-utils/README.md b/packages/e2e-test-utils/README.md index 114efe1482cef5..e8af181a034c1b 100644 --- a/packages/e2e-test-utils/README.md +++ b/packages/e2e-test-utils/README.md @@ -252,27 +252,28 @@ _Returns_ - `Promise`: Promise resolving with post content markup. -# **hasBlockSwitcher** +# **getPageError** -Returns a boolean indicating if the current selected block has a block switcher or not. +Returns a promise resolving to one of either a string or null. A string will +be resolved if an error message is present in the contents of the page. If no +error is present, a null value will be resolved instead. This requires the +environment be configured to display errors. -_Returns_ +_Related_ -- `Promise`: Promise resolving with a boolean. +- -# **hasPageError** +_Returns_ -Returns a promise resolving to a boolean reflecting whether a PHP notice is -present anywhere within the document's markup. This requires the environment -be configured to display errors. +- `Promise`: Promise resolving to a string or null, depending whether a page error is present. -_Related_ +# **hasBlockSwitcher** -- +Returns a boolean indicating if the current selected block has a block switcher or not. _Returns_ -- `Promise`: Promise resolving to a boolean reflecting whether a PHP notice is present. +- `Promise`: Promise resolving with a boolean. # **insertBlock** diff --git a/packages/e2e-test-utils/src/get-page-error.js b/packages/e2e-test-utils/src/get-page-error.js new file mode 100644 index 00000000000000..0182119ce7e38e --- /dev/null +++ b/packages/e2e-test-utils/src/get-page-error.js @@ -0,0 +1,25 @@ +/** + * Regular expression matching a displayed PHP error within a markup string. + * + * @see https://github.com/php/php-src/blob/598175e/main/main.c#L1257-L1297 + * + * @type {RegExp} + */ +const REGEXP_PHP_ERROR = /()?(Fatal error|Recoverable fatal error|Warning|Parse error|Notice|Strict Standards|Deprecated|Unknown error)(<\/b>)?: (.*?) in (.*?) on line ()?\d+(<\/b>)?/; + +/** + * Returns a promise resolving to one of either a string or null. A string will + * be resolved if an error message is present in the contents of the page. If no + * error is present, a null value will be resolved instead. This requires the + * environment be configured to display errors. + * + * @see http://php.net/manual/en/function.error-reporting.php + * + * @return {Promise} Promise resolving to a string or null, depending + * whether a page error is present. + */ +export async function getPageError() { + const content = await page.content(); + const match = content.match( REGEXP_PHP_ERROR ); + return match ? match[ 0 ] : null; +} diff --git a/packages/e2e-test-utils/src/has-page-error.js b/packages/e2e-test-utils/src/has-page-error.js deleted file mode 100644 index ab37c12f38bb5a..00000000000000 --- a/packages/e2e-test-utils/src/has-page-error.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Regular expression matching a displayed PHP error within a markup string. - * - * @see https://github.com/php/php-src/blob/598175e/main/main.c#L1257-L1297 - * - * @type {RegExp} - */ -const REGEXP_PHP_ERROR = /(Fatal error|Recoverable fatal error|Warning|Parse error|Notice|Strict Standards|Deprecated|Unknown error)(<\/b>)?: (.*?) in (.*?) on line /; - -/** - * Returns a promise resolving to a boolean reflecting whether a PHP notice is - * present anywhere within the document's markup. This requires the environment - * be configured to display errors. - * - * @see http://php.net/manual/en/function.error-reporting.php - * - * @return {Promise} Promise resolving to a boolean reflecting whether a PHP - * notice is present. - */ -export async function hasPageError() { - return REGEXP_PHP_ERROR.test( await page.content() ); -} diff --git a/packages/e2e-test-utils/src/index.js b/packages/e2e-test-utils/src/index.js index 477bc395da31a7..d71cacc1cd70c6 100644 --- a/packages/e2e-test-utils/src/index.js +++ b/packages/e2e-test-utils/src/index.js @@ -22,7 +22,7 @@ export { getAvailableBlockTransforms } from './get-available-block-transforms'; export { getBlockSetting } from './get-block-setting'; export { getEditedPostContent } from './get-edited-post-content'; export { hasBlockSwitcher } from './has-block-switcher'; -export { hasPageError } from './has-page-error'; +export { getPageError } from './get-page-error'; export { insertBlock } from './insert-block'; export { installPlugin } from './install-plugin'; export { isCurrentURL } from './is-current-url'; diff --git a/packages/e2e-test-utils/src/test/has-page-error.js b/packages/e2e-test-utils/src/test/get-page-error.js similarity index 66% rename from packages/e2e-test-utils/src/test/has-page-error.js rename to packages/e2e-test-utils/src/test/get-page-error.js index 9cf1c227abee54..ef8c113372f35c 100644 --- a/packages/e2e-test-utils/src/test/has-page-error.js +++ b/packages/e2e-test-utils/src/test/get-page-error.js @@ -1,9 +1,9 @@ /** * Internal dependencies */ -import { hasPageError } from '../has-page-error'; +import { getPageError } from '../get-page-error'; -describe( 'hasPageError', () => { +describe( 'getPageError', () => { let originalPage; beforeEach( () => { @@ -14,31 +14,31 @@ describe( 'hasPageError', () => { global.page = originalPage; } ); - it( 'returns false if there is no error', async () => { + it( 'resolves to null if there is no error', async () => { global.page = { content: () => 'Happy!', }; - expect( await hasPageError() ).toBe( false ); + expect( await getPageError() ).toBe( null ); } ); it.each( [ [ 'PHP, HTML', - 'Notice: Undefined property: WP_Block_Type_Registry::$x in /var/www/html/wp-content/plugins/gutenberg/lib/blocks.php on line 47
', + 'Notice: Undefined property: WP_Block_Type_Registry::$x in /var/www/html/wp-content/plugins/gutenberg/lib/blocks.php on line 47', ], [ 'PHP, Plaintext', 'Notice: Undefined property: WP_Block_Type_Registry::$x in /var/www/html/wp-content/plugins/gutenberg/lib/blocks.php on line 47', ], ] )( - 'returns true if there is an error (%s)', + 'resolves to the error message if there is an error (%s)', async ( _variant, error ) => { global.page = { content: () => error, }; - expect( await hasPageError() ).toBe( true ); + expect( await getPageError() ).toBe( error ); } ); } ); diff --git a/packages/e2e-test-utils/src/visit-admin-page.js b/packages/e2e-test-utils/src/visit-admin-page.js index ff8d1e8cfa664a..0e8ea90954c1b7 100644 --- a/packages/e2e-test-utils/src/visit-admin-page.js +++ b/packages/e2e-test-utils/src/visit-admin-page.js @@ -9,7 +9,7 @@ import { join } from 'path'; import { createURL } from './create-url'; import { isCurrentURL } from './is-current-url'; import { loginUser } from './login-user'; -import { hasPageError } from './has-page-error'; +import { getPageError } from './get-page-error'; /** * Visits admin page; if user is not logged in then it logging in it first, then visits admin page. @@ -25,5 +25,5 @@ export async function visitAdminPage( adminPath, query ) { await visitAdminPage( adminPath, query ); } - expect( await hasPageError() ).toBe( false ); + expect( await getPageError() ).toBe( null ); }