-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
29.webview.test.js
304 lines (231 loc) · 10.7 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
const {expectElementSnapshotToMatch} = require("./utils/snapshot");
const {waitForCondition} = require("./utils/waitForCondition");
const jestExpect = require('expect').default;
describe('Web View', () => {
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);
};
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 jestExpect(async () => {
await expect(web.element(by.web.id('nonExistentElement'))).toExist();
}).rejects.toThrowError();
});
it('should raise an error when does element not exists at index', async () => {
await jestExpect(async () => {
await expect(web.element(by.web.tag('p')).atIndex(100)).toExist();
}).rejects.toThrowError();
});
});
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 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 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 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 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 jestExpect(async () => {
await headline.runScript('(el) => { el.textContent = "Changed"; throw new Error("Error"); }');
}).rejects.toThrowError();
});
});
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('toggleDummyWebViewButton')).tap();
webview = web(by.id('dummyWebView'));
});
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('toggleDummyWebView2Button')).tap();
await jestExpect(async () => {
await expect(web(by.id('dummyWebView')).element(by.web.id('message'))).toExist();
}).rejects.toThrowError();
await device.launchApp();
});
describe('at-index support', () => {
beforeEach(async () => {
await element(by.id('toggleDummyWebView2Button')).tap();
});
describe(':ios:', () => {
it('should find web-view by index', async () => {
await expect(web(by.id('dummyWebView')).atIndex(0).element(by.web.id('message'))).toExist();
await expect(web(by.id('dummyWebView')).atIndex(1).element(by.web.id('message'))).toExist();
});
it('should throw on index out of bounds', async () => {
await jestExpect(async () => {
await expect(web(by.id('dummyWebView')).atIndex(2).element(by.web.id('message'))).toExist();
}).rejects.toThrowError();
});
});
it(':android: should throw on usage of atIndex', async () => {
await jestExpect(async () => {
await expect(web(by.id('dummyWebView')).atIndex(0).element(by.web.id('message'))).toExist();
}).rejects.toThrowError();
});
});
});
});