File size: 3,303 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
import { Page } from 'playwright';
import { getCalypsoURL } from '../../data-helper';

const selectors = {
	// Reader main stream
	readerCard: '.reader-post-card',
	streamPlaceholder: 'span.reader__placeholder-text',
	actionButton: ( action: 'Share' | 'Comment' ) =>
		`.reader-post-actions__item:has-text("${ action }")`,

	// Post
	relatedPostsPlaceholder: '.is-placeholder',
	commentTextArea: '.comments__form textarea',
	commentSubmitButton: '.comments__form button:text("Send")',
	comment: ( commentText: string ) => `div:text( '${ commentText }' )`,
};

/**
 * Page representing the Reader Page.
 */
export class ReaderPage {
	private page: Page;

	/**
	 * Constructs an instance of the component.
	 *
	 * @param {Page} page The underlying page.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Opens the Reader page.
	 *
	 * Example {@link https://wordpress.com/reader}
	 */
	async visit(): Promise< void > {
		await this.page.goto( getCalypsoURL( 'reader' ) );
		await this.page.waitForURL( /reader/ );
	}

	/**
	 * Visits a post in the Reader.
	 *
	 * This method supports either a 1-indexed number or partial or full string matching.
	 *
	 * 	index: 1-indexed value, starting from top of page.
	 * 	text: partial or full text matching of text contained in a reader entry. If multiple
	 * 		matches are found,  the first match is used.
	 *
	 * @param param0 Keyed object parameter.
	 * @param {number} param0.index n-th post to view on the reader page. 1-indexed.
	 * @param {string} param0.text Text string to match.
	 * @throws {Error} If neither index or text are specified.
	 */
	async visitPost( { index, text }: { index?: number; text?: string } = {} ): Promise< void > {
		// Wait for main reader stream to populate.
		// Use the `last` method to narrow down the locators in case more than 1 placeholders exist.
		await this.page.locator( selectors.streamPlaceholder ).last().waitFor( { state: 'hidden' } );

		let selector = '';

		if ( index ) {
			selector = `:nth-match(${ selectors.readerCard }, ${ index })`;
		} else if ( text ) {
			selector = `${ selectors.readerCard }:has-text("${ text }")`;
		} else {
			throw new Error( 'Unable to select and visit post - specify one of index or text.' );
		}

		await Promise.all( [
			this.page.waitForURL( /reader\/feeds\/[\d]+\/posts.*/ ),
			this.page.click( selector ),
		] );
	}

	/**
	 * Submits a given string of text as comment on a post.
	 *
	 * This method requires that current page is on an article that supports comments.
	 * Otherwise, this method will throw.
	 *
	 * @param {string} comment Text of the comment.
	 */
	async comment( comment: string ): Promise< void > {
		// Wait for related posts card to generate.
		await this.page.waitForSelector( selectors.relatedPostsPlaceholder, { state: 'hidden' } );

		// Force scroll.
		const elementHandle = await this.page.waitForSelector( selectors.commentTextArea );
		await this.page.evaluate(
			( element: SVGElement | HTMLElement ) => element.scrollIntoView(),
			elementHandle
		);
		await this.page.fill( selectors.commentTextArea, comment );

		await Promise.all( [
			this.page.waitForResponse(
				( response ) => response.status() === 200 && response.url().includes( 'new?' )
			),
			this.page.click( selectors.commentSubmitButton ),
		] );
	}
}