File size: 3,866 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
import { Page } from 'playwright';
import { reloadAndRetry, waitForElementEnabled } from '../../element-helper';

const selectors = {
	searchInput: '.search-component__input',
	firstResultItem: '.domain-suggestion:first-child .domain-suggestion__content',
	domainSuggestionRow: '.domain-suggestion',
};

/**
 * Component for the domain search feature.
 *
 * This class applies to multiple locations within WordPress.com that displays a domain search component.
 * Examples:
 * 	- Upgrades > Domains
 * 	- Signup flow
 */
export class DomainSearchComponent {
	private page: Page;

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

	/**
	 * Searches for a domain using the keyword.
	 *
	 * @param {string} keyword Keyword to use in domain search.
	 */
	async search( keyword: string ): Promise< void > {
		/**
		 *
		 * Closure to pass into the retry method.
		 *
		 * @param {Page} page Page object.
		 */
		async function searchDomainClosure( page: Page ): Promise< void > {
			const [ response ] = await Promise.all( [
				page.waitForResponse( /suggestions\?/ ),
				page.getByRole( 'searchbox' ).fill( keyword ),
			] );

			if ( ! response ) {
				const errorText = await page.getByRole( 'status', { name: 'Notice' } ).innerText();
				throw new Error(
					`Encountered error while searching for domain.\nOriginal error: ${ errorText }`
				);
			}
		}

		// Domain lookup service is external to Automattic and sometimes it returns an error.
		// Retry a few times when this is encountered.
		await reloadAndRetry( this.page, searchDomainClosure );
	}

	/**
	 * Select a domain matching the keyword.
	 *
	 * The keyword can be anything that uniquely identifies the desired domain name
	 * in the search results listing.
	 *
	 * Examples:
	 * 	keyword = uniquewordpresscomsite.com
	 * 	keyword = .com
	 *
	 * If multiple matches are found, the first match is attmpted.
	 *
	 * @param {string} keyword Unique keyword to select domains.
	 * @returns {string} Domain that was selected.
	 */
	async selectDomain( keyword: string, waitForContinueButton: boolean = true ): Promise< string > {
		const targetRow = this.page.locator( selectors.domainSuggestionRow ).filter( {
			has: this.page.getByLabel( keyword ),
		} );
		await targetRow.waitFor();

		const target = targetRow.getByRole( 'button' );
		await target.waitFor();

		const selectedDomain = await targetRow.getByRole( 'heading' ).getAttribute( 'aria-label' );

		if ( ! selectedDomain ) {
			throw new Error( `No domain found for keyword: ${ keyword }` );
		}

		await target.click();

		if ( waitForContinueButton ) {
			const continueButton = await waitForElementEnabled( this.page, 'button:text("Continue")', {
				timeout: 30 * 1000,
			} );

			// Now click the enabled button using dispatchEvent to handle issues with the environment badge staying on top of the button.
			await Promise.all( [
				continueButton.dispatchEvent( 'click' ),
				this.page.waitForNavigation(),
			] );
		}

		return selectedDomain;
	}

	/**
	 * Select the first domain suggestion.
	 *
	 * @returns {string} Domain that was selected.
	 */
	async selectFirstSuggestion(): Promise< string > {
		const targetItem = await this.page.waitForSelector( selectors.firstResultItem );
		// Heading element inside a given result contains the full domain name string.
		const selectedDomain = await targetItem
			.waitForSelector( 'h3' )
			.then( ( el ) => el.innerText() );

		await Promise.all( [ this.page.waitForNavigation(), targetItem.click() ] );

		return selectedDomain;
	}

	/**
	 * Clicks on a button matching the text.
	 *
	 * @param {string} text Exact text to match on.
	 */
	async clickButton( text: string ): Promise< void > {
		await this.page.getByRole( 'button', { name: text, exact: true } ).click();
	}
}