File size: 5,690 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Page } from 'playwright';

/**
 * Step name in site setup flow.
 *
 * @see client/landing/stepper/declarative-flow/site-setup-flow.ts for all step names
 */
export type StepName =
	| 'goals'
	| 'vertical'
	| 'intent'
	| 'design-setup'
	| 'options'
	| 'designChoices';
type WriteActions = 'Start writing' | 'Start learning' | 'View designs';

const selectors = {
	// Generic
	button: ( text: string ) => `button:text("${ text }")`,
	backLink: 'button:text("Back")',

	// Inputs
	blogNameInput: 'input[name="siteTitle"]:not(:disabled)',
	taglineInput: 'input[name="tagline"]:not(:disabled)',

	// Themes
	individualThemeContainer: ( name: string ) => `.design-button-container:has-text("${ name }")`,

	// Goals
	goalButton: ( goal: string ) =>
		`.select-card-checkbox__container:has-text("${ goal.toLowerCase() }")`,
	selectedGoalButton: ( goal: string ) =>
		`.select-card-checkbox__container:has(:checked):has-text("${ goal }")`,

	// Step containers
	contentAgnosticContainer: '.step-container',
	themePickerContainer: '.design-picker',
	goalsStepContainer: '.goals-step',
	verticalsStepContainer: '.site-vertical',
	intentStepContainer: '.intent-step',
	optionsStepContainer: '.is-step-write',
	designChoicesStepContainer: '.design-choices',
};

/**
 * Class encapsulating the flow when starting a new start ('/start')
 */
export class StartSiteFlow {
	private page: Page;

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

	/**
	 * Given text, clicks on the first instance of the button with the text.
	 *
	 * @param {string} text User-visible text on the button.
	 */
	async clickButton( text: string ): Promise< void > {
		await this.page.getByRole( 'button', { name: text } ).click();
	}

	/**
	 * Returns the step name of the current page
	 */
	async getCurrentStep(): Promise< StepName > {
		// Make sure the container is loaded first, then we can see which it is.
		await this.page.waitForSelector( selectors.contentAgnosticContainer );
		if ( ( await this.page.locator( selectors.goalsStepContainer ).count() ) > 0 ) {
			return 'goals';
		}
		if ( ( await this.page.locator( selectors.intentStepContainer ).count() ) > 0 ) {
			return 'intent';
		}
		if ( ( await this.page.locator( selectors.themePickerContainer ).count() ) > 0 ) {
			return 'design-setup';
		}
		if ( ( await this.page.locator( selectors.optionsStepContainer ).count() ) > 0 ) {
			return 'options';
		}
		if ( ( await this.page.locator( selectors.designChoicesStepContainer ).count() ) > 0 ) {
			return 'designChoices';
		}
		throw new Error( 'Unknown or invalid step' );
	}

	/**
	 * Select a goal by text.
	 *
	 * @param {string} goal The goal to select
	 */
	async selectGoal( goal: string ): Promise< void > {
		await this.page.click( selectors.goalButton( goal ) );
		await this.page.waitForSelector( selectors.selectedGoalButton( goal ) );
	}

	/**
	 * Select a design choice by text.
	 *
	 * @param {string} choice The button text
	 */
	async clickDesignChoice( choice: 'theme' | 'ai' ): Promise< void > {
		// It's best to select the element using accessible text
		const choiceLabel = choice === 'theme' ? 'Start with a theme' : 'Create with AI (BETA)';

		await this.page.getByRole( 'button', { name: choiceLabel } ).click();
	}

	/**
	 * Enter blog name.
	 *
	 * @param {string} name Name for the blog.
	 */
	async enterBlogName( name: string ): Promise< void > {
		const defaultInputlocator = this.page.locator( selectors.blogNameInput );

		await defaultInputlocator.fill( name );

		// Verify the data is saved as expected.
		const filledInputLocator = this.page.locator( selectors.blogNameInput );
		const readBack = await filledInputLocator.inputValue();
		if ( readBack !== name ) {
			throw new Error( `Failed to set blog name: expected ${ name }, got ${ readBack }` );
		}
	}

	/**
	 * Enter blog tagline.
	 *
	 * @param {string} tagline Tagline for the blog.
	 */
	async enterTagline( tagline: string ): Promise< void > {
		const locator = this.page.locator( selectors.taglineInput );
		await locator.fill( tagline );

		// Verify the data is saved as expected.
		const readBack = await locator.inputValue();
		if ( readBack !== tagline ) {
			throw new Error( `Failed to set blog tagline: expected ${ tagline }, got ${ readBack }` );
		}
	}

	/* Write Goal */

	/**
	 * Performs action available in the Write intent.
	 *
	 * @param {WriteActions} action Actions for the Write intent.
	 */
	async clickWriteAction( action: WriteActions ) {
		await this.page.getByRole( 'button', { name: action } ).click();

		if ( action === 'Start writing' ) {
			// Extended timeout because the site is being
			// headstarted at this time.
			await this.page.waitForURL( /setup\/site-setup\/processing/, { timeout: 20 * 1000 } );
		}
		if ( action === 'Start learning' ) {
			await this.page.waitForURL( /setup\/site-setup\/courses/ );
		}
		if ( action === 'View designs' ) {
			await this.page.waitForURL( /setup\/site-setup\/design-setup/ );
		}
	}

	/**
	 * Validates we've landed on the design picker screen.
	 */
	async validateOnDesignPickerScreen(): Promise< void > {
		await this.page.waitForSelector( selectors.themePickerContainer );
	}

	/**
	 * Navigate back one screen in the flow.
	 */
	async goBackOneScreen(): Promise< void > {
		await this.page.click( selectors.backLink );
	}

	/**
	 * Clicks button to preview a specific theme from theme selection screen.
	 *
	 * @param {string} themeName Name of theme, e.g. "Zoologist".
	 */
	async selectTheme( themeName: string ): Promise< void > {
		await this.page.getByRole( 'link', { name: themeName } ).first().click();
	}
}