File size: 4,598 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Page, Response } from 'playwright';
import { getCalypsoURL } from '../../data-helper';
import { reloadAndRetry } from '../../element-helper';

type TrashedMenuItems = 'Restore' | 'Copy link' | 'Delete Permanently';
type GenericMenuItems = 'Trash';

type MenuItems = TrashedMenuItems | GenericMenuItems;
type PostsPageTabs = 'Published' | 'Drafts' | 'Scheduled' | 'Trash';

const selectors = {
	// General
	addNewPostButton: 'a.page-title-action, span.split-page-title-action>a',

	// Post Item
	postRow: 'tr.type-post',
	postItem: ( title: string ) =>
		`a.row-title:has-text("${ title }"), strong>span:has-text("${ title }")`,

	// Status Filter
	statusItem: ( item: string ) => `ul.subsubsub a:has-text("${ item }")`,

	// Actions
	actionItem: ( item: string ) => `.row-actions a:has-text("${ item }")`,
};

/**
 * Represents the Posts page.
 */
export class PostsPage {
	private page: Page;

	/**
	 * Creates an instance of the page.
	 *
	 * @param {Page} page Object representing the base page.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Opens the Posts page.
	 *
	 * Example {@link https://wordpress.com/posts}
	 */
	async visit(): Promise< Response | null > {
		return await this.page.goto( getCalypsoURL( 'posts' ) );
	}

	/**
	 * Clicks on the navigation tab.
	 *
	 * @param {string} name Name of the tab to click.
	 * @returns {Promise<void>} No return value.
	 */
	async clickTab( name: PostsPageTabs ): Promise< void > {
		const locator = this.page.locator( selectors.statusItem( name ) );
		await locator.click();
	}

	/* Page readiness */

	/**
	 * Ensures the post item denoted by the parameter `title` is shown on the page.
	 *
	 * Due to a race condition, sometimes the expected post does not appear
	 * on the list of posts. This can occur when state for multiple posts are being modified
	 * at the same time (eg. several posts being trashed).
	 *
	 * @param {string} title Post title.
	 */
	private async ensurePostShown( title: string ): Promise< void > {
		/**
		 * Closure to wait until the post to appear in the list of posts.
		 *
		 * @param {Page} page Page object.
		 */
		async function waitForPostToAppear( page: Page ): Promise< void > {
			const postLocator = page.locator( `${ selectors.postRow } ${ selectors.postItem( title ) }` );
			await postLocator.waitFor( { state: 'visible', timeout: 20 * 1000 } );
		}

		await reloadAndRetry( this.page, waitForPostToAppear );
	}

	/**
	 * Clicks on the `add new post` button.
	 */
	async newPost(): Promise< void > {
		const locator = this.page.locator( selectors.addNewPostButton );
		await Promise.all( [
			this.page.waitForNavigation( { url: /post-new.php/, timeout: 20 * 1000 } ),
			locator.click(),
		] );
	}

	/* Post actions */

	/**
	 * Given a post title, click on the post, triggering a navigation
	 * to the editor page.
	 *
	 * @param {string} title Partial or full string of the post.
	 */
	async clickPost( title: string ): Promise< void > {
		await this.ensurePostShown( title );

		const locator = this.page.locator( `${ selectors.postRow } ${ selectors.postItem( title ) }` );
		await locator.click();
	}

	/**
	 * Toggles the Post Actions of a matching post.
	 *
	 * @param {string} title Post title on which the actions should be toggled.
	 */
	async togglePostActions( title: string ): Promise< void > {
		await this.ensurePostShown( title );

		const locator = this.page.locator( selectors.postRow, {
			has: this.page.locator( selectors.postItem( title ) ),
		} );
		await locator.hover();
	}

	/* Menu actions */

	/**
	 * Given a post title and target action item, performs the following actions:
	 * 	- locate the post with matching title.
	 * 	- toggle the post action.
	 * 	- click on an action with matching name.
	 *
	 * @param param0 Object parameter.
	 * @param {string} param0.title Title of the post.
	 * @param {MenuItems} param0.action Name of the target action in the menu.
	 */
	async clickActionItemForPost( {
		title,
		action,
	}: {
		title: string;
		action: MenuItems;
	} ): Promise< void > {
		await this.ensurePostShown( title );

		await this.togglePostActions( title );
		await this.clickActionItem( title, action );
	}

	/**
	 * Clicks on the action item.
	 *
	 * @param {string} title Title of the post.
	 * @param {string} menuItem Target menu item.
	 */
	private async clickActionItem( title: string, menuItem: string ): Promise< void > {
		const locator = this.page.locator( selectors.postRow, {
			has: this.page.locator( selectors.postItem( title ) ),
		} );
		await locator.locator( selectors.actionItem( menuItem ) ).click();
	}
}