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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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<div style="width: 45%; float:left" align="left"><a href="./library_objects.md"><-- Library objects</a> </div>
<div style="width: 5%; float:left" align="center"><a href="./../README.md">Top</a></div>
<div style="width: 45%; float:right"align="right"><a href="./patterns_tricks_gotchas.md">Patterns, Tricks, and Gotchas --></a> </div>

<br><br>

# Style Guide

<!-- TOC -->

- [Style Guide](#style-guide)
  - [Variable naming](#variable-naming)
  - [Use async/await](#use-asyncawait)
  - [Selectors](#selectors)
    - [No selectors within class definition](#no-selectors-within-class-definition)
    - [Extract repetitive selectors](#extract-repetitive-selectors)
    - [Do not use Xpath](#do-not-use-xpath)
    - [Prefer user-facing selectors](#prefer-user-facing-selectors)
    - [Naming](#naming)
    - [Involve minimal selectors in methods](#involve-minimal-selectors-in-methods)
    - [Convert repetitive variations to dynamic selector](#convert-repetitive-variations-to-dynamic-selector)
  - [Test steps](#test-steps)
    - [Only one top-level describe block](#only-one-top-level-describe-block)
    - [Do not use modal verbs](#do-not-use-modal-verbs)
    - [Prefer smaller steps](#prefer-smaller-steps)
  - [Single responsibility function](#single-responsibility-function)
  - [Destructure parameters](#destructure-parameters)

<!-- /TOC -->

## Variable naming

Variables that derive from a page/component/flow object (eg. SidebarComponent) should be named after the object following camelCase convention.

**Avoid**:

```typescript
const bar = new SidebarComponent( page );
const mhp = new MyHomePage( page );
```

**Instead**:

```typescript
const sidebarComponent = new SidebarComponent( page );
const myHomePage = new MyHomePage( page );
```

---

## Use async/await

We use async functions and `await` to wait for commands to finish. This lets asynchronous methods execute like synchronous methods.
For every method which returns a promise or thenable object `await` should be used. Keep in mind that `await` is only valid inside async function.

We don't chain function calls together and avoid using `.then` calls.

**Avoid**:

```typescript
async function openModal() {
	const modal = await this.page.waitForSelector( 'modal-open' );
	await modal.click().then( () => this.page.waitForSelector( 'modal-is-open' ) );
}
```

**Instead**:

```typescript
async function openModal() {
	const modal = await this.page.waitForSelector('modal-open');
	await modal.click();
	await this.page.waitForSelector( 'modal-is-open' ) );
}
```

---

## Selectors

Selectors are the core of automated e2e testing. The Playwright project has an excellent documentation page on [selectors](https://playwright.dev/docs/selectors) and [best practices](https://playwright.dev/docs/selectors#best-practices).

Ideally, a selector satisfies all of the following:

- **unique**: one selector, one element.
- **reliable**: the same element is selected with each iteration.
- **brief**: selector is short and easy to read.

The following guidance are in addition to suggestions by the Playwright project.

### No selectors within class definition

Place selectors within the same file, but outside of the class representing the object. Never place a selector within the class definition itself.

For examples see [EditorPage](https://github.com/Automattic/wp-calypso/blob/8428228ee6547007faf3e765133d2396967d504a/packages/calypso-e2e/src/lib/pages/editor-page.ts) or [NotificationComponent](https://github.com/Automattic/wp-calypso/blob/8428228ee6547007faf3e765133d2396967d504a/packages/calypso-e2e/src/lib/components/notifications-component.ts).

**Avoid**:

```typescript
class SomeObject() {
	submitButtonSelector: 'button[type="submit"]'
}
```

**Instead**:

```typescript
const selectors = {
	submitButton: 'button[type="submit"]',
};

class SomeObject() {
	async someMethod(): {
		await this.page.click( selectors.submitButton )
	}
}
```

### Extract repetitive selectors

While one-off selectors within a code block is acceptable, if the same selector is used more than twice, move the selector into the `selectors` object:

**Avoid**:

```typescript
class SomeObject() {
	async doSomething() {
		await this.page.click( '#submit' );
	}

	async doSomethingElse() {
		await this.page.click( '#submit' ); // Notice the #submit selector is used twice.
	}
}
```

**Instead**:

```typescript
const selectors = {
	submitButton: '#submit', // Move it into the `selectors` object.
}

class SomeObject() {
	async doSomething() {
		await this.page.click( selectors.submitButton );
	}

	async doSomethingElse() {
		await this.page.click( selectors.submitButton );
	}
}

```

### Do not use Xpath

This is simple; do not use Xpath selectors.

**Avoid**:

```typescript
const locator = page.locator( '//button' );
```

**Instead**:

```typescript
await locator = page.locator( 'button' ); // or 'button[type="submit"]' or literally anything else.
```

### Prefer user-facing selectors

Where possible, prefer user-facing attributes such as ARIA, user-facing text, role selectors. Use CSS selectors as last resort if no other suitable selectors can be found.

See also: [the Playwright maintainers' definitive guide](https://playwright.dev/docs/selectors#best-practices).

**Avoid**:

```typescript
await page.click( 'div.someclass .yet-another-class .attribute .very-long-attribute' );
await page.click( 'button .is-highlighted' );
```

**Instead**:

```typescript
await page.click( 'button:has-text("Submit")' ); // Text based selector.
await page.click( 'button[aria-label="Some Class"]' ); // ARIA selector.
await page.click( 'role=spinbutton[name="Continue"]' ); // Role-based selector.
```

### Naming

Name selectors based on function, type and description. Try to avoid using element location unless multiple similar buttons exist. This way the selector name does not become outdated if the UI changes.

Do not append the term 'Selector' or similar to the selector name. It is redundant.

**Avoid**:

```typescript
const selectors = {
	contactButtonOnHeaderPane: '.button contact-us', // What if the button moves?
	secondButtonOnPopupSelector: '.button send-form', // Breaks the 'selector' sub-rule.
	select: 'select.month', // Not descriptive at all.
};
```

**Instead**:

```typescript
const selectors = {
	contactUsButton: '.button contact-us',
	submitFormButton: '.button send-form',
	monthSelect: 'select.month',
};
```

### Involve minimal selectors in methods

When method(s) need to wait on an element on the page, involve the minimal number of selectors as possible. Playwright has a strong [auto-wait mechanism](https://playwright.dev/docs/actionability) that handles 95% of the cases.

For instance, when loading the Calypso Media page:

- Good: wait either on the gallery being present, or the thumbnails having generated.
- Bad: wait on the header text _and_ the gallery _and_ the upload button _and_ the thumbnails.

**Avoid**:

```typescript
await this.page.waitForSelector( 'h1:has-text("New Page")' ); // Waiting for the h1 accomplishes nothing except to add another selector to complicate matters.
await this.page.fill( 'input[placeholder="New Text"]' );
```

**Instead**:

```typescript
await this.page.fill( 'input[placeholder="New Text"]' );
```

### Convert repetitive variations to dynamic selector

Combine similar or repetitive selectors into a dynamic selector.
Dynamic selector is also useful when the target selector depends on a known conditional (eg. mobile/desktop, language).

**Avoid**:

```typescript
const selectors = {
	submitButton: 'button:text("Submit")',
	cancelButton: 'button:text("Cancel")',
	pauseButton: 'button:text("Pause")',
	// Note the repetitive selectors varying only by text.
};
```

**Instead**:

```typescript
const selectors = {
	button: ( action: string ) => `button:text("${ action }")`,
};

// then, in the POM

async funtion clickButton( text: string ) {
	await this.page.click( selectors.button( text ) );
}
```

---

## Test steps

### Only one top-level `describe` block

Only place one top/root-level `describe` block.

Multiple root-level `describe` blocks are a sign that the file needs to be split into smaller files or the flow re-examined.

**Avoid**:

```typescript
describe('Feature 1', function()) {}

describe('Feature 2', function()) {}

describe('Feature 3', function()) {}
```

**Instead**:

```typescript
// In spec1.ts
describe( 'Feature: Use sub-feature 1', function () {
	describe( 'Feature 1', function () {} );
} );

// In spec2.ts
describe( 'Feature: Use sub-feature 2', function () {
	describe( 'Feature 2', function () {} );
} );
```

### Do not use modal verbs

Avoid the use of modal verbs such as `can`, `should`, `could` or `must`.
Instead state the action(s) the step is expected to perform, or the end result of what _should_ happen after this step.

**Avoid**:

```typescript
it( 'Can log in' );

it( 'Should be able to start new post' );
```

**Instead**:

```typescript
it( 'Log In' );

it( 'Start new post' );
```

### Prefer smaller steps

Break large steps into smaller pieces for clarity and ease of debugging.

**Avoid**:

```typescript
it( 'Log in, select home page and start a search', async function () {
	// too many things done here.
} );
```

**Instead**:

```typescript
it( 'Log In', async function () {} );

it( 'Navigate to home page', async function () {} );

it( 'Search for ${string}', async function () {} );
```

---

## Single responsibility function

Each function should focus on one or two key responsibilities.
Avoid overloading the function to perform increasing number of tasks.

**Avoid**

```typescript
async function publish(
	tags = '',
	saveFirst = false,
	visitAfter = false,
	setDate = ''
): Promise< void > {
	// This function is responsible for too many tasks.
	// The first clue is the long list of parameters.
	// A publishing function should publish and only publish, and do that well.
}
```

**Instead**

```typescript
async function publish( { visitAfter }: { visitAfter: boolean } = {} ): Promise< void > {
	// Publish post, and possibly visit the post after publishing.
}

async function saveDraft(): Promise< void > {
	// Saves post as draft first.
}

async function applyTags( tags: string[] ): Promise< void > {
	// Applies tags.
}

async function setPublishDate( date: string ): Promise< void > {
	// Sets the publish date.
}
```

## Destructure parameters

Use destructuring for default values as this makes calling the function explicit and avoids boolean traps.

**Avoid**:

```typescript
constructor( selector: string, visit:boolean = true, culture:string = 'en', flow:string = '', domainFirst:boolean = false, domainFirstDomain:string = '' ) {}

// In another file

const startPage = new StartPage( selector, true, 'en', '', true, '' ).displayed();
```

**Instead**:

```typescript
constructor( selector: string, { visit = true, culture = 'en', flow = '', domainFirst = false, domainFirstDomain = '' }: {visit: boolean, culture: string, flow: string, domainFirst: boolean, domainFirstDomain: string} = {} ) {}

// In another file

const startPage = new StartPage( selector, { visit: true, domainFirst: true } ).displayed();
```