-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy patheditor-page.js
480 lines (417 loc) · 14.4 KB
/
editor-page.js
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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* Internal dependencies
*/
import {
isAndroid,
swipeUp,
swipeDown,
typeString,
toggleHtmlMode,
swipeFromTo,
} from '../helpers/utils';
export default class EditorPage {
driver;
accessibilityIdKey;
accessibilityIdXPathAttrib;
paragraphBlockName = 'Paragraph';
verseBlockName = 'Verse';
orderedListButtonName = 'Convert to ordered list';
constructor( driver ) {
this.driver = driver;
this.accessibilityIdKey = 'name';
this.accessibilityIdXPathAttrib = 'name';
if ( isAndroid() ) {
this.accessibilityIdXPathAttrib = 'content-desc';
this.accessibilityIdKey = 'contentDescription';
}
driver.setImplicitWaitTimeout( 5000 );
}
async getBlockList() {
return await this.driver.hasElementByAccessibilityId( 'block-list' );
}
// Finds the wd element for new block that was added and sets the element attribute
// and accessibilityId attributes on this object and selects the block
// position uses one based numbering
async getBlockAtPosition(
blockName,
position = 1,
options = { autoscroll: false }
) {
const blockLocator = `//*[contains(@${ this.accessibilityIdXPathAttrib }, "${ blockName } Block. Row ${ position }")]`;
const elements = await this.driver.elementsByXPath( blockLocator );
const lastElementFound = elements[ elements.length - 1 ];
if ( elements.length === 0 && options.autoscroll ) {
const firstBlockVisible = await this.getFirstBlockVisible();
const lastBlockVisible = await this.getLastBlockVisible();
// exit if no block is found
if ( ! firstBlockVisible || ! lastBlockVisible ) {
return lastElementFound;
}
const firstBlockAccessibilityId = await firstBlockVisible.getAttribute(
this.accessibilityIdKey
);
const firstBlockRowMatch = /Row (\d+)\./.exec(
firstBlockAccessibilityId
);
const firstBlockRow =
firstBlockRowMatch && Number( firstBlockRowMatch[ 1 ] );
const lastBlockAccessibilityId = await lastBlockVisible.getAttribute(
this.accessibilityIdKey
);
const lastBlockRowMatch = /Row (\d+)\./.exec(
lastBlockAccessibilityId
);
const lastBlockRow =
lastBlockRowMatch && Number( lastBlockRowMatch[ 1 ] );
if ( firstBlockRow && position < firstBlockRow ) {
if ( firstBlockRow === 1 ) {
// we're at the top already stop recursing
return lastElementFound;
}
// scroll up
await swipeDown( this.driver );
} else if ( lastBlockRow && position > lastBlockRow ) {
// scroll down
await swipeUp( this.driver );
}
return await this.getBlockAtPosition(
blockName,
position,
options
);
}
return lastElementFound;
}
async getFirstBlockVisible() {
const firstBlockLocator = `//*[contains(@${ this.accessibilityIdXPathAttrib }, " Block. Row ")]`;
const elements = await this.driver.elementsByXPath( firstBlockLocator );
return elements[ 0 ];
}
async getLastBlockVisible() {
const firstBlockLocator = `//*[contains(@${ this.accessibilityIdXPathAttrib }, " Block. Row ")]`;
const elements = await this.driver.elementsByXPath( firstBlockLocator );
return elements[ elements.length - 1 ];
}
async hasBlockAtPosition( position = 1, blockName = '' ) {
return (
undefined !==
( await this.getBlockAtPosition( blockName, position ) )
);
}
async getTitleElement( options = { autoscroll: false } ) {
//TODO: Improve the identifier for this element
const elements = await this.driver.elementsByXPath(
`//*[contains(@${ this.accessibilityIdXPathAttrib }, "Post title.")]`
);
if ( elements.length === 0 && options.autoscroll ) {
await swipeDown( this.driver );
return this.getTitleElement( options );
}
return elements[ elements.length - 1 ];
}
async getTextViewForHtmlViewContent() {
const accessibilityId = 'html-view-content';
let blockLocator = `//*[@${ this.accessibilityIdXPathAttrib }="${ accessibilityId }"]`;
if ( ! isAndroid() ) {
blockLocator += '//XCUIElementTypeTextView';
}
return await this.driver.elementByXPath( blockLocator );
}
// Converts to lower case and checks for a match to lowercased html content
// Ensure to take additional steps to handle text being changed by auto correct
async verifyHtmlContent( html ) {
await toggleHtmlMode( this.driver, true );
const htmlContentView = await this.getTextViewForHtmlViewContent();
const text = await htmlContentView.text();
expect( text.toLowerCase() ).toBe( html.toLowerCase() );
await toggleHtmlMode( this.driver, false );
}
// set html editor content explicitly
async setHtmlContentAndroid( html ) {
await toggleHtmlMode( this.driver, true );
const htmlContentView = await this.getTextViewForHtmlViewContent();
await htmlContentView.setText( html );
await toggleHtmlMode( this.driver, false );
}
async dismissKeyboard() {
await this.driver.sleep( 1000 ); /// wait for any keyboard animations
const keyboardShown = await this.driver.isKeyboardShown();
if ( ! keyboardShown ) {
return;
}
if ( isAndroid() ) {
return await this.driver.hideDeviceKeyboard();
}
const hideKeyboardToolbarButton = await this.driver.elementByXPath(
'//XCUIElementTypeButton[@name="Hide keyboard"]'
);
await hideKeyboardToolbarButton.click();
}
// =========================
// Block toolbar functions
// =========================
async addNewBlock( blockName ) {
// Click add button
let identifier = 'Add block';
if ( isAndroid() ) {
identifier = 'Add block, Double tap to add a block';
}
const addButton = await this.driver.elementByAccessibilityId(
identifier
);
await addButton.click();
// Click on block of choice
const blockButton = await this.findBlockButton( blockName );
if ( isAndroid() ) {
await blockButton.click();
} else {
await this.driver.execute( 'mobile: tap', {
element: blockButton,
x: 10,
y: 10,
} );
}
}
// Attempts to find the given block button in the block inserter control.
async findBlockButton( blockName ) {
if ( isAndroid() ) {
const size = await this.driver.getWindowSize();
const x = size.width / 2;
// Checks if the Block Button is available, and if not will scroll to the second half of the available buttons.
while (
! ( await this.driver.hasElementByAccessibilityId( blockName ) )
) {
swipeFromTo(
this.driver,
{ x, y: size.height - 100 },
{ x, y: size.height - 450 }
);
}
return await this.driver.elementByAccessibilityId( blockName );
}
const blockButton = await this.driver.elementByAccessibilityId(
blockName
);
const size = await this.driver.getWindowSize();
const height = size.height - 5;
while ( ! ( await blockButton.isDisplayed() ) ) {
await this.driver.execute( 'mobile: dragFromToForDuration', {
fromX: 50,
fromY: height,
toX: 50,
toY: height - 450,
duration: 0.5,
} );
}
return blockButton;
}
async clickToolBarButton( buttonName ) {
const toolBarButton = await this.driver.elementByAccessibilityId(
buttonName
);
await toolBarButton.click();
}
// =========================
// Inline toolbar functions
// =========================
// position of the block to move up
async moveBlockUpAtPosition( position, blockName = '' ) {
if ( ! ( await this.hasBlockAtPosition( position, blockName ) ) ) {
throw Error( `No Block at position ${ position }` );
}
const parentLocator = `//*[@${ this.accessibilityIdXPathAttrib }="${ blockName } Block. Row ${ position }."]`;
let blockLocator = `${ parentLocator }/following-sibling::*`;
blockLocator += isAndroid() ? '' : '//*';
blockLocator += `[@${
this.accessibilityIdXPathAttrib
}="Move block up from row ${ position } to row ${ position - 1 }"]`;
const moveUpButton = await this.driver.elementByXPath( blockLocator );
await moveUpButton.click();
}
// position of the block to move down
async moveBlockDownAtPosition( position, blockName = '' ) {
if ( ! ( await this.hasBlockAtPosition( position, blockName ) ) ) {
throw Error( `No Block at position ${ position }` );
}
const parentLocator = `//*[contains(@${ this.accessibilityIdXPathAttrib }, "${ blockName } Block. Row ${ position }.")]`;
let blockLocator = `${ parentLocator }/following-sibling::*`;
blockLocator += isAndroid() ? '' : '//*';
blockLocator += `[@${
this.accessibilityIdXPathAttrib
}="Move block down from row ${ position } to row ${ position + 1 }"]`;
const moveDownButton = await this.driver.elementByXPath( blockLocator );
await moveDownButton.click();
}
// position of the block to remove
// Block will no longer be present if this succeeds
async removeBlockAtPosition( blockName = '', position = 1 ) {
if ( ! ( await this.hasBlockAtPosition( position, blockName ) ) ) {
throw Error( `No Block at position ${ position }` );
}
const buttonElementName = isAndroid()
? '//*'
: '//XCUIElementTypeButton';
const blockActionsMenuButtonIdentifier = `Open Block Actions Menu`;
const blockActionsMenuButtonLocator = `${ buttonElementName }[contains(@${ this.accessibilityIdXPathAttrib }, "${ blockActionsMenuButtonIdentifier }")]`;
if ( isAndroid() ) {
const block = await this.getBlockAtPosition( blockName, position );
let checkList = await this.driver.elementsByXPath(
blockActionsMenuButtonLocator
);
while ( checkList.length === 0 ) {
await swipeUp( this.driver, block ); // Swipe up to show remove icon at the bottom
checkList = await this.driver.elementsByXPath(
blockActionsMenuButtonLocator
);
}
}
const blockActionsMenuButton = await this.driver.elementByXPath(
blockActionsMenuButtonLocator
);
await blockActionsMenuButton.click();
const removeActionButtonIdentifier = 'Remove block';
const removeActionButtonLocator = `${ buttonElementName }[contains(@${ this.accessibilityIdXPathAttrib }, "${ removeActionButtonIdentifier }")]`;
const removeActionButton = await this.driver.elementByXPath(
removeActionButtonLocator
);
await removeActionButton.click();
}
// =========================
// Paragraph Block functions
// =========================
async getTextViewForParagraphBlock( block ) {
let textViewElementName = 'XCUIElementTypeTextView';
if ( isAndroid() ) {
textViewElementName = 'android.widget.EditText';
}
const accessibilityId = await block.getAttribute(
this.accessibilityIdKey
);
const blockLocator = `//*[@${
this.accessibilityIdXPathAttrib
}=${ JSON.stringify( accessibilityId ) }]//${ textViewElementName }`;
return await this.driver.elementByXPath( blockLocator );
}
async typeTextToParagraphBlock( block, text, clear ) {
const textViewElement = await this.getTextViewForParagraphBlock(
block
);
await typeString( this.driver, textViewElement, text, clear );
await this.driver.sleep( 1000 ); // Give time for the block to rerender (such as for accessibility)
}
async sendTextToParagraphBlock( position, text, clear ) {
const paragraphs = text.split( '\n' );
for ( let i = 0; i < paragraphs.length; i++ ) {
// Select block first
const block = await this.getBlockAtPosition(
this.paragraphBlockName,
position + i
);
await block.click();
await this.typeTextToParagraphBlock(
block,
paragraphs[ i ],
clear
);
if ( i !== paragraphs.length - 1 ) {
await this.typeTextToParagraphBlock( block, '\n', false );
}
}
}
async getTextForParagraphBlock( block ) {
const textViewElement = await this.getTextViewForParagraphBlock(
block
);
const text = await textViewElement.text();
return text.toString();
}
async getTextForParagraphBlockAtPosition( position ) {
// Select block first
let block = await this.getBlockAtPosition(
this.paragraphBlockName,
position
);
await block.click();
block = await this.getBlockAtPosition(
this.paragraphBlockName,
position
);
const text = await this.getTextForParagraphBlock( block );
return text.toString();
}
// =========================
// List Block functions
// =========================
async getTextViewForListBlock( block ) {
let textViewElementName = 'XCUIElementTypeTextView';
if ( isAndroid() ) {
textViewElementName = 'android.widget.EditText';
}
const accessibilityId = await block.getAttribute(
this.accessibilityIdKey
);
const blockLocator = `//*[@${
this.accessibilityIdXPathAttrib
}=${ JSON.stringify( accessibilityId ) }]//${ textViewElementName }`;
return await this.driver.elementByXPath( blockLocator );
}
async sendTextToListBlock( block, text ) {
const textViewElement = await this.getTextViewForListBlock( block );
// Cannot clear list blocks because it messes up the list bullet
const clear = false;
return await typeString( this.driver, textViewElement, text, clear );
}
async clickOrderedListToolBarButton() {
await this.clickToolBarButton( this.orderedListButtonName );
}
// =========================
// Image Block functions
// =========================
async selectEmptyImageBlock( block ) {
const accessibilityId = await block.getAttribute(
this.accessibilityIdKey
);
const blockLocator = `//*[@${ this.accessibilityIdXPathAttrib }="${ accessibilityId }"]//XCUIElementTypeButton[@name="Image block. Empty"]`;
const imageBlockInnerElement = await this.driver.elementByXPath(
blockLocator
);
await imageBlockInnerElement.click();
}
async chooseMediaLibrary() {
const mediaLibraryButton = await this.driver.elementByAccessibilityId(
'WordPress Media Library'
);
await mediaLibraryButton.click();
}
async enterCaptionToSelectedImageBlock( caption, clear = true ) {
const imageBlockCaptionField = await this.driver.elementByXPath(
'//XCUIElementTypeButton[starts-with(@name, "Image caption.")]'
);
await imageBlockCaptionField.click();
await typeString( this.driver, imageBlockCaptionField, caption, clear );
}
// =========================
// Heading Block functions
// =========================
// Inner element changes on iOS if Heading Block is empty
async getTextViewForHeadingBlock( block, empty ) {
let textViewElementName = empty
? 'XCUIElementTypeStaticText'
: 'XCUIElementTypeTextView';
if ( isAndroid() ) {
textViewElementName = 'android.widget.EditText';
}
const accessibilityId = await block.getAttribute(
this.accessibilityIdKey
);
const blockLocator = `//*[@${ this.accessibilityIdXPathAttrib }="${ accessibilityId }"]//${ textViewElementName }`;
return await this.driver.elementByXPath( blockLocator );
}
async sendTextToHeadingBlock( block, text, clear = true ) {
const textViewElement = await this.getTextViewForHeadingBlock(
block,
true
);
return await typeString( this.driver, textViewElement, text, clear );
}
}