-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy path29.webview.test.js
455 lines (343 loc) · 16.4 KB
/
29.webview.test.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
const {expectElementSnapshotToMatch} = require("./utils/snapshot");
const {waitForCondition} = require("./utils/waitForCondition");
const {expectToThrow} = require('./utils/custom-expects');
const jestExpect = require('expect').default;
const MockServer = require('../mock-server/mock-server');
describe('WebView', () => {
beforeEach(async () => {
await device.reloadReactNative();
await element(by.text('WebView')).tap();
});
describe('single web-view scenario', () => {
const expectWebViewToMatchSnapshot = async (snapshotName) => {
const webViewElement = element(by.id('webViewFormWithScrolling'));
await expectElementSnapshotToMatch(webViewElement, snapshotName, 0.993);
};
describe('matchers', () => {
describe(':ios:', () => {
it('should not find element by invalid index', async () => {
await expect(web.element(by.web.tag('p')).atIndex(100)).not.toExist();
});
it('should find element by hrefContains', async () => {
await expect(web.element(by.web.hrefContains('w3schools'))).toExist();
});
it('should find element by href', async () => {
await expect(web.element(by.web.href('https://www.w3schools.com'))).toExist();
});
it('should raise an error when element does not exists but expect to exist', async () => {
await expectToThrow(async () => {
await expect(web.element(by.web.id('nonExistentElement'))).toExist();
});
});
it('should raise an error when does element not exists at index', async () => {
await expectToThrow(async () => {
await expect(web.element(by.web.tag('p')).atIndex(100)).toExist();
});
});
it('should find element by label', async () => {
await expect(web.element(by.web.label('first-webview'))).toExist();
});
});
it('should find element by id', async () => {
await expect(web.element(by.web.id('pageHeadline'))).toExist();
});
it('should find element by tag', async () => {
await expect(web.element(by.web.tag('body'))).toExist();
});
it('should find element by index', async () => {
await expect(web.element(by.web.tag('p')).atIndex(0)).toExist();
});
it('should find element by class name', async () => {
await expect(web.element(by.web.className('specialParagraph'))).toExist();
});
it('should find element by css selector', async () => {
await expect(web.element(by.web.cssSelector('.specialParagraph'))).toExist();
});
it('should find element by xpath', async () => {
await expect(web.element(by.web.xpath('//p[@class="specialParagraph"]'))).toExist();
});
it('should find element by name', async () => {
await expect(web.element(by.web.name('fname'))).toExist();
});
it('should assert that an element is not visible', async () => {
await expect(web.element(by.web.id('nonExistentElement'))).not.toExist();
});
});
describe('actions', () => {
describe('input', () => {
const inputElement = web.element(by.web.id('fname'));
describe(':ios:', () => {
it('should type text in input regardless of content-editable parameter on ios', async () => {
await inputElement.typeText('Test', false);
await inputElement.typeText('er', true);
await expect(inputElement).toHaveText('Tester');
});
it('should type text in input', async () => {
await inputElement.typeText('Test');
await inputElement.typeText('er');
await expect(inputElement).toHaveText('Tester');
});
it('should type text in input with `email` type', async () => {
await inputElement.runScript((el) => { el.type = 'email'; });
await inputElement.typeText('Tester');
await expect(inputElement).toHaveText('Tester');
});
it('should keep cursor position on end while typing', async () => {
await inputElement.typeText('Test');
await expectWebViewToMatchSnapshot('typing-keep-cursor-position-webview-input-1');
await inputElement.typeText('er');
await expectWebViewToMatchSnapshot('typing-keep-cursor-position-webview-input-2');
});
it('should not type more text than `maxlength` limit', async () => {
const text = '0123456789 ABCDEF';
await inputElement.typeText(text);
const maxlength = 10;
const expectedText = text.substring(0, maxlength);
await expect(inputElement).toHaveText(expectedText);
});
it('should clear text in input', async () => {
await inputElement.typeText('Test');
await inputElement.clearText();
await expect(inputElement).toHaveText('');
});
it('should replace text in input', async () => {
await inputElement.typeText('Temp');
await inputElement.replaceText('Tester');
await expect(inputElement).toHaveText('Tester');
});
it('should tap on submit button and update result', async () => {
await inputElement.typeText('Tester');
await web.element(by.web.id('submit')).tap();
await expect(inputElement).toHaveText('Tester');
});
});
it('should select all text in input', async () => {
await inputElement.typeText('Tester');
await inputElement.selectAllText();
await expectWebViewToMatchSnapshot('select-all-text-in-webview');
});
it('should focus on input', async () => {
await inputElement.focus();
await expectWebViewToMatchSnapshot('focus-on-input-webview');
});
it('should move cursor to end', async () => {
await inputElement.typeText('Tester');
await inputElement.moveCursorToEnd();
await expectWebViewToMatchSnapshot('move-cursor-to-end-webview');
});
});
describe('content-editable', () => {
const contentEditableElement = web.element(by.web.id('contentEditable'));
describe(':ios:', () => {
it('should type text in content-editable regardless of content-editable parameter on ios', async () => {
await contentEditableElement.typeText('Tes', false);
await contentEditableElement.typeText('te', true);
await contentEditableElement.typeText('r');
await expect(contentEditableElement).toHaveText('Name: Tester');
});
it('should clear text in content-editable', async () => {
await contentEditableElement.clearText();
await expect(contentEditableElement).toHaveText('');
});
it('should replace text in content-editable', async () => {
await contentEditableElement.replaceText('Tester');
await expect(contentEditableElement).toHaveText('Tester');
});
it('should type text in content-editable', async () => {
await contentEditableElement.typeText('Test', true);
await contentEditableElement.typeText('er', true);
await expect(contentEditableElement).toHaveText('Name: Tester');
});
it('should keep cursor position on end while typing', async () => {
await contentEditableElement.typeText('Test', true);
await expectWebViewToMatchSnapshot('typing-keep-cursor-position-webview-content-editable-1');
await contentEditableElement.typeText('er', true);
await expectWebViewToMatchSnapshot('typing-keep-cursor-position-webview-content-editable-2');
});
});
it('should select all text in content-editable', async () => {
await contentEditableElement.selectAllText();
await expectWebViewToMatchSnapshot('select-all-text-in-content-editable-webview');
});
it('should focus on content-editable', async () => {
await contentEditableElement.focus();
await expectWebViewToMatchSnapshot('focus-on-content-editable-webview');
});
it('should move cursor to end', async () => {
await contentEditableElement.moveCursorToEnd();
await expectWebViewToMatchSnapshot('move-cursor-to-end-content-editable-webview');
});
});
it('should scroll to view', async () => {
await web.element(by.web.id('bottomParagraph')).scrollToView();
await expectWebViewToMatchSnapshot('scroll-to-view-webview');
});
it('should run script', async () => {
const headline = web.element(by.web.id('pageHeadline'));
await headline.runScript('(el) => { el.textContent = "Changed"; }');
await expect(headline).toHaveText('Changed');
});
it('should run script with non-string function as parameter', async () => {
const headline = web.element(by.web.id('pageHeadline'));
await headline.runScript((el) => { el.textContent = "Changed"; });
await expect(headline).toHaveText('Changed');
});
it('should run script with arguments', async () => {
const headline = web.element(by.web.id('pageHeadline'));
await headline.runScript('(el, text) => { el.textContent = text; }', ['Changed']);
await expect(headline).toHaveText('Changed');
});
it('should run script with arguments with non-string function as parameter', async () => {
const headline = web.element(by.web.id('pageHeadline'));
await headline.runScript((el, text) => { el.textContent = text; }, ['Changed']);
await expect(headline).toHaveText('Changed');
});
it('should return value from run script', async () => {
const headline = web.element(by.web.id('pageHeadline'));
const textContent = await headline.runScript('(el) => { return el.textContent; }');
await jestExpect(textContent).toBe('First Webview');
});
it('should raise error when script fails', async () => {
const headline = web.element(by.web.id('pageHeadline'));
await expectToThrow(async () => {
await headline.runScript('(el) => { el.textContent = "Changed"; throw new Error("Error"); }');
});
});
});
describe('getters', () => {
it(':ios: should get the web page url', async () => {
await web.element(by.web.id('w3link')).tap();
await waitForCondition(
() => web.element(by.web.tag('body')).getCurrentUrl(),
(result) => result === 'https://www.w3schools.com/',
5000
);
});
it('should get the web page title', async () => {
const title = await web.element(by.web.tag('body')).getTitle();
await jestExpect(title).toBe('First Webview');
});
it('should get text from element', async () => {
const source = await web.element(by.web.id('pageHeadline')).getText();
await jestExpect(source).toBe('First Webview');
});
});
});
describe('multiple web-views scenario',() => {
/** @type {Detox.WebViewElement} */
let webview;
beforeEach(async () => {
await element(by.id('toggle2ndWebviewButton')).tap();
webview = web(by.id('webView'));
});
it('should have a title', async () => {
const title = await webview.element(by.web.tag('body')).getTitle();
await jestExpect(title).toBe('Dummy Webview');
});
it('should have a paragraph', async () => {
await expect(webview.element(by.web.id('message'))).toExist();
await expect(webview.element(by.web.id('message'))).toHaveText('This is a dummy webview.');
});
it('should throw on multiple matches', async () => {
await element(by.id('toggle3rdWebviewButton')).tap();
await jestExpect(async () => {
await expect(web(by.id('webView')).element(by.web.id('message'))).toExist();
}).rejects.toThrowError();
await device.launchApp();
});
describe('at-index support', () => {
beforeEach(async () => {
await element(by.id('toggle3rdWebviewButton')).tap();
});
describe(':ios:', () => {
it('should find web-view by index', async () => {
await expect(web(by.id('webView')).atIndex(0).element(by.web.id('message'))).toExist();
await expect(web(by.id('webView')).atIndex(1).element(by.web.id('message'))).toExist();
});
it('should throw on index out of bounds', async () => {
await expectToThrow(async () => {
await expect(web(by.id('webView')).atIndex(2).element(by.web.id('message'))).toExist();
});
});
});
// Not implemented yet
it(':android: should throw on usage of atIndex', async () => {
await expectToThrow(async () => {
await expect(web(by.id('webView')).atIndex(0).element(by.web.id('message'))).toExist();
});
});
});
});
});
describe(':ios: WebView CORS (inner frame)', () => {
/** @type {Detox.WebViewElement} */
let webview;
let webviewElement;
const mockServer = new MockServer();
beforeAll(async () => {
mockServer.init();
if (device.getPlatform() === 'android') {
// Android needs to reverse the port in order to access the mock server
await device.reverseTcpPort(mockServer.port);
}
});
afterAll(async () => {
await mockServer.close();
});
const launchAndNavigateToInnerFrame = async (shouldDisableWebKitSecurity) => {
await device.launchApp({
newInstance: true,
launchArgs: {
detoxDisableWebKitSecurity:
shouldDisableWebKitSecurity !== undefined ? (shouldDisableWebKitSecurity ? 'true' : 'false') : undefined,
},
});
await element(by.text('WebView')).tap();
await element(by.id('toggle3rdWebviewButton')).tap();
webview = web(by.id('webView'));
webviewElement = element(by.id('webView'));
};
describe('detoxDisableWebKitSecurity', () => {
it('should find element in cross-origin frame when `detoxDisableWebKitSecurity` is `true`', async () => {
await launchAndNavigateToInnerFrame(true);
await expect(webview.element(by.web.tag('h1'))).toExist();
await expect(webview.element(by.web.tag('h1'))).toHaveText('Hello World!');
});
it('should not find element in cross-origin frame when `detoxDisableWebKitSecurity` is `false`', async () => {
await launchAndNavigateToInnerFrame(false);
await expect(webview.element(by.web.tag('h1'))).not.toExist();
});
it('should not find element in cross-origin frame when `detoxDisableWebKitSecurity` is not set', async () => {
await launchAndNavigateToInnerFrame();
await expect(webview.element(by.web.tag('h1'))).not.toExist();
});
});
describe('asSecured()', () => {
beforeEach(async () => {
await launchAndNavigateToInnerFrame();
});
it('should not find non-existing element in cross-origin frame with `asSecured()`', async () => {
await expect(web.element(by.web.label('Hello Worldd!')).asSecured()).not.toExist();
await expect(web.element(by.web.label('Non-existing element')).atIndex(3).asSecured()).not.toExist();
});
it('should find elements in cross-origin frame by type with `asSecured()`', async () => {
await expect(web.element(by.web.type('textField')).atIndex(1).asSecured()).toExist();
});
it('should find elements in cross-origin frame by label with `asSecured()`', async () => {
await expect(web.element(by.web.label('Hello World!')).asSecured()).toExist();
});
it('should type text in cross-origin frame with `asSecured()`', async () => {
await web.element(by.web.type('textField')).atIndex(1).asSecured().typeText('Test');
await expectElementSnapshotToMatch(webviewElement, 'type-text-in-cross-origin-frame');
await web.element(by.web.type('textField')).asSecured().atIndex(1).replaceText('Test 2');
await expectElementSnapshotToMatch(webviewElement, 'replace-text-in-cross-origin-frame');
await web.element(by.web.type('textField')).asSecured().atIndex(1).clearText();
await expectElementSnapshotToMatch(webviewElement, 'clear-text-in-cross-origin-frame');
});
it('should tap on cross-origin frame element with `asSecured()`', async () => {
await web.element(by.web.type('textField')).asSecured().atIndex(1).typeText('Test');
await web.element(by.web.label('Submit')).asSecured().tap();
await expectElementSnapshotToMatch(webviewElement, 'tap-on-cross-origin-frame-element');
});
});
});