Skip to content

Commit

Permalink
Separate utils into PageUtils and RequestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Feb 21, 2022
1 parent 3f34fa2 commit 9851ff3
Show file tree
Hide file tree
Showing 37 changed files with 695 additions and 490 deletions.
11 changes: 1 addition & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions packages/e2e-test-utils-playwright/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ npm install @wordpress/e2e-test-utils-playwright --save-dev

## API

### TestUtils
### PageUtils

Create the test utils instance of the current page.

```js
const testUtils = new TestUtils( page );
const pageUtils = new PageUtils( page );
```

### RequestUtils

Create the request utils instance of the request instance.

```js
const requestUtils = await RequestUtils.setup( {
user: {
username: 'admin',
password: 'password',
},
} );
```

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
1 change: 1 addition & 0 deletions packages/e2e-test-utils-playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/url": "file:../url",
"form-data": "^4.0.0",
"lodash": "^4.17.21",
"node-fetch": "^2.6.0"
},
"peerDependencies": {
Expand Down
22 changes: 0 additions & 22 deletions packages/e2e-test-utils-playwright/src/activate-plugin.js

This file was deleted.

22 changes: 0 additions & 22 deletions packages/e2e-test-utils-playwright/src/activate-theme.js

This file was deleted.

27 changes: 0 additions & 27 deletions packages/e2e-test-utils-playwright/src/blocks.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const WP_ADMIN_USER = {
username: 'admin',
password: 'password',
};
} as const;

const {
WP_USERNAME = WP_ADMIN_USER.username,
Expand Down
25 changes: 0 additions & 25 deletions packages/e2e-test-utils-playwright/src/create-url.js

This file was deleted.

20 changes: 0 additions & 20 deletions packages/e2e-test-utils-playwright/src/deactivate-plugin.js

This file was deleted.

62 changes: 2 additions & 60 deletions packages/e2e-test-utils-playwright/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,5 @@
/**
* External dependencies
*/
import type { Browser, Page, BrowserContext } from '@playwright/test';

/**
* Internal dependencies
*/
import { activatePlugin } from './activate-plugin';
import { activateTheme } from './activate-theme';
import { deleteAllBlocks } from './blocks';
import { clickBlockToolbarButton } from './click-block-toolbar-button';
import { createNewPost } from './create-new-post';
import { createURL } from './create-url';
import { deactivatePlugin } from './deactivate-plugin';
import { getCurrentUser } from './get-current-user';
import { getPageError } from './get-page-error';
import { isCurrentURL } from './is-current-url';
import { loginUser } from './login-user';
import { deleteAllPosts } from './posts';
import {
rest as __experimentalRest,
batch as __experimentalBatch,
} from './rest-api';
import { showBlockToolbar } from './show-block-toolbar';
import { switchUserToAdmin } from './switch-user-to-admin';
import { switchUserToTest } from './switch-user-to-test';
import { visitAdminPage } from './visit-admin-page';
import { deleteAllWidgets } from './widgets';

class TestUtils {
browser: Browser;
page: Page;
context: BrowserContext;

constructor( page: Page ) {
this.page = page;
this.context = page.context();
this.browser = this.context.browser()!;
}

activatePlugin = activatePlugin;
activateTheme = activateTheme;
clickBlockToolbarButton = clickBlockToolbarButton;
createNewPost = createNewPost;
createURL = createURL;
deactivatePlugin = deactivatePlugin;
deleteAllBlocks = deleteAllBlocks;
deleteAllPosts = deleteAllPosts;
getCurrentUser = getCurrentUser;
getPageError = getPageError;
isCurrentURL = isCurrentURL;
loginUser = loginUser;
showBlockToolbar = showBlockToolbar;
switchUserToAdmin = switchUserToAdmin;
switchUserToTest = switchUserToTest;
visitAdminPage = visitAdminPage;
deleteAllWidgets = deleteAllWidgets;
__experimentalRest = __experimentalRest;
__experimentalBatch = __experimentalBatch;
}

export { TestUtils };
export { PageUtils } from './page';
export { RequestUtils } from './request';
15 changes: 0 additions & 15 deletions packages/e2e-test-utils-playwright/src/is-current-url.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Clicks a block toolbar button.
*
* @this {import('./').TestUtils}
* @this {import('./').PageUtils}
* @param {string} label The text string of the button label.
* @param {string} [type] The type of button label: 'ariaLabel' or 'content'.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { addQueryArgs } from '@wordpress/url';
/**
* Creates new post.
*
* @this {import('./').TestUtils}
* @this {import('./').PageUtils}
* @param {Object} object Object to create new post, along with tips enabling option.
* @param {string} [object.postType] Post type of the new post.
* @param {string} [object.title] Title of the new post.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Get the username of the user that's currently logged into WordPress (if any).
*
* @this {import('./').TestUtils}
* @this {import('./').PageUtils}
* @return {string?} username The user that's currently logged into WordPress (if any).
*/
export async function getCurrentUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const REGEXP_PHP_ERROR = /(<b>)?(Fatal error|Recoverable fatal error|Warning|Par
*
* @see http://php.net/manual/en/function.error-reporting.php
*
* @this {import('./').TestUtils}
* @this {import('./').PageUtils}
* @return {Promise<?string>} Promise resolving to a string or null, depending
* whether a page error is present.
*/
Expand Down
43 changes: 43 additions & 0 deletions packages/e2e-test-utils-playwright/src/page/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import type { Browser, Page, BrowserContext } from '@playwright/test';

/**
* Internal dependencies
*/
import { clickBlockToolbarButton } from './click-block-toolbar-button';
import { createNewPost } from './create-new-post';
import { getCurrentUser } from './get-current-user';
import { getPageError } from './get-page-error';
import { isCurrentURL } from './is-current-url';
import { loginUser } from './login-user';
import { showBlockToolbar } from './show-block-toolbar';
import { switchUserToAdmin } from './switch-user-to-admin';
import { switchUserToTest } from './switch-user-to-test';
import { visitAdminPage } from './visit-admin-page';

class PageUtils {
browser: Browser;
page: Page;
context: BrowserContext;

constructor( page: Page ) {
this.page = page;
this.context = page.context();
this.browser = this.context.browser()!;
}

clickBlockToolbarButton = clickBlockToolbarButton;
createNewPost = createNewPost;
getCurrentUser = getCurrentUser;
getPageError = getPageError;
isCurrentURL = isCurrentURL;
loginUser = loginUser;
showBlockToolbar = showBlockToolbar;
switchUserToAdmin = switchUserToAdmin;
switchUserToTest = switchUserToTest;
visitAdminPage = visitAdminPage;
}

export { PageUtils };
18 changes: 18 additions & 0 deletions packages/e2e-test-utils-playwright/src/page/is-current-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Internal dependencies
*/
import { WP_BASE_URL } from '../config';

/**
* Checks if current URL is a WordPress path.
*
* @this {import('./').PageUtils}
* @param {string} WPPath String to be serialized as pathname.
* @return {boolean} Boolean represents whether current URL is or not a WordPress path.
*/
export function isCurrentURL( WPPath ) {
const currentURL = new URL( this.page.url() );
const expectedURL = new URL( WPPath, WP_BASE_URL );

return expectedURL.pathname === currentURL.pathname;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Internal dependencies
*/
import { WP_USERNAME, WP_PASSWORD } from './shared/config';
import { WP_USERNAME, WP_PASSWORD } from '../config';

/**
* Performs log in with specified username and password.
*
* @this {import('./').TestUtils}
* @this {import('./').PageUtils}
* @param {?string} username String to be used as user credential.
* @param {?string} password String to be used as user credential.
*/
Expand All @@ -15,7 +15,7 @@ export async function loginUser(
password = WP_PASSWORD
) {
if ( ! this.isCurrentURL( 'wp-login.php' ) ) {
await this.page.goto( this.createURL( 'wp-login.php' ) );
await this.page.goto( 'wp-login.php' );
}

await this.page.press( '#user_login', 'Control+A' );
Expand Down
Loading

0 comments on commit 9851ff3

Please sign in to comment.