File size: 2,692 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
import { Page } from 'playwright';
import { envVariables } from '../..';
import { getCalypsoURL } from '../../data-helper';
import { clickNavTab } from '../../element-helper';

type AdvertisingTab = 'Ready to promote' | 'Campaigns';

/**
 * Page representing the Tools > Advertising page.
 */
export class AdvertisingPage {
	private page: Page;

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

	/**
	 * Navigates directly to the Advertising page for the site.
	 *
	 * @param {string} siteSlug Site slug.
	 */
	async visit( siteSlug: string ) {
		await this.page.goto( getCalypsoURL( `advertising/${ siteSlug }` ) );
	}

	/**
	 * Click on the tab name matching the given parameter `name`.
	 *
	 * @param {AdvertisingTab} name Name of the tab to click on the top of the page.
	 */
	async clickTab( name: AdvertisingTab ) {
		await clickNavTab( this.page, name );
	}

	/**
	 * Clicks on a button with the accessible name matching supplied parameter `name`,
	 * narrowed down by either one of the following parameters.
	 *
	 * If the parameter `row` is specified, this method will look for a button
	 * in a specific row. Rows are 0-indexed.
	 *
	 * Alternatively, if the parameter `postTitle` is defined, this method will
	 * click on the button matching the accessible name found in a row with the matching
	 * post title.
	 *
	 * If neither is defined, an error is thrown.
	 *
	 * @param {string} name Accessible name of the button.
	 * @param param1 Keyed object parameter.
	 * @param {number} [param1.row] Row number to look for the button. 0-indexed.
	 * @param {string} [param1.postTitle] Post title to locate the button by.
	 * @throws {Error} If neither `postTitle` or `row` is defined.
	 */
	async clickButtonByNameOnRow(
		name: string,
		{ row, postTitle }: { row?: number; postTitle?: string } = {}
	) {
		// Wait for promote the banner to finish loading on desktop.
		if ( envVariables.VIEWPORT_NAME === 'desktop' ) {
			await this.page
				.getByRole( 'main' )
				.locator( '.posts-list-banner__container, .tsp-banner__container' )
				.waitFor( { timeout: 20 * 1000 } ); // Banner can be pretty slow on some sites.
		}

		if ( row !== undefined && row >= 0 ) {
			await this.page
				.getByRole( 'row' )
				.nth( row + 1 ) // The header row is counted as one row.
				.getByRole( 'button', { name: name } )
				.click();
		} else if ( postTitle ) {
			await this.page
				.getByRole( 'row' )
				.filter( { hasText: postTitle } )
				.getByRole( 'button', { name: name } )
				.click();
		} else {
			throw new Error( 'Must pass in either row or postTitle.' );
		}
	}
}