summaryrefslogtreecommitdiff
path: root/testing/marionette/test_error.js
blob: a905f02f06042b2a4c19224ec2cb7e4dd1d238f4 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const {utils: Cu} = Components;

Cu.import("chrome://marionette/content/error.js");

function notok(condition) {
  ok(!(condition));
}

add_test(function test_isError() {
  notok(error.isError(null));
  notok(error.isError([]));
  notok(error.isError(new Date()));

  ok(error.isError(new Components.Exception()));
  ok(error.isError(new Error()));
  ok(error.isError(new EvalError()));
  ok(error.isError(new InternalError()));
  ok(error.isError(new RangeError()));
  ok(error.isError(new ReferenceError()));
  ok(error.isError(new SyntaxError()));
  ok(error.isError(new TypeError()));
  ok(error.isError(new URIError()));
  ok(error.isError(new WebDriverError()));
  ok(error.isError(new InvalidArgumentError()));

  run_next_test();
});

add_test(function test_isWebDriverError() {
  notok(error.isWebDriverError(new Components.Exception()));
  notok(error.isWebDriverError(new Error()));
  notok(error.isWebDriverError(new EvalError()));
  notok(error.isWebDriverError(new InternalError()));
  notok(error.isWebDriverError(new RangeError()));
  notok(error.isWebDriverError(new ReferenceError()));
  notok(error.isWebDriverError(new SyntaxError()));
  notok(error.isWebDriverError(new TypeError()));
  notok(error.isWebDriverError(new URIError()));

  ok(error.isWebDriverError(new WebDriverError()));
  ok(error.isWebDriverError(new InvalidArgumentError()));
  ok(error.isWebDriverError(new JavaScriptError()));

  run_next_test();
});

add_test(function test_wrap() {
  // webdriver-derived errors should not be wrapped
  equal(error.wrap(new WebDriverError()).name, "WebDriverError");
  ok(error.wrap(new WebDriverError()) instanceof WebDriverError);
  equal(error.wrap(new InvalidArgumentError()).name, "InvalidArgumentError");
  ok(error.wrap(new InvalidArgumentError()) instanceof WebDriverError);
  ok(error.wrap(new InvalidArgumentError()) instanceof InvalidArgumentError);

  // JS errors should be wrapped in WebDriverError
  equal(error.wrap(new Error()).name, "WebDriverError");
  ok(error.wrap(new Error()) instanceof WebDriverError);
  equal(error.wrap(new EvalError()).name, "WebDriverError");
  equal(error.wrap(new InternalError()).name, "WebDriverError");
  equal(error.wrap(new RangeError()).name, "WebDriverError");
  equal(error.wrap(new ReferenceError()).name, "WebDriverError");
  equal(error.wrap(new SyntaxError()).name, "WebDriverError");
  equal(error.wrap(new TypeError()).name, "WebDriverError");
  equal(error.wrap(new URIError()).name, "WebDriverError");

  // wrapped JS errors should retain their type
  // as part of the message field
  equal(error.wrap(new WebDriverError("foo")).message, "foo");
  equal(error.wrap(new TypeError("foo")).message, "TypeError: foo");

  run_next_test();
});

add_test(function test_stringify() {
  equal("<unprintable error>", error.stringify());
  equal("<unprintable error>", error.stringify("foo"));
  equal("[object Object]", error.stringify({}));
  equal("[object Object]\nfoo", error.stringify({stack: "foo"}));
  equal("Error: foo", error.stringify(new Error("foo")).split("\n")[0]);
  equal("WebDriverError: foo",
      error.stringify(new WebDriverError("foo")).split("\n")[0]);
  equal("InvalidArgumentError: foo",
      error.stringify(new InvalidArgumentError("foo")).split("\n")[0]);

  run_next_test();
});

add_test(function test_pprint() {
  equal('[object Object] {"foo":"bar"}', error.pprint`${{foo: "bar"}}`);

  equal("[object Number] 42", error.pprint`${42}`);
  equal("[object Boolean] true", error.pprint`${true}`);
  equal("[object Undefined] undefined", error.pprint`${undefined}`);
  equal("[object Null] null", error.pprint`${null}`);

  let complexObj = {toJSON: () => "foo"};
  equal('[object Object] "foo"', error.pprint`${complexObj}`);

  let cyclic = {};
  cyclic.me = cyclic;
  equal("[object Object] <cyclic object value>", error.pprint`${cyclic}`);

  let el = {
    nodeType: 1,
    localName: "input",
    id: "foo",
    classList: {length: 1},
    className: "bar baz",
  };
  equal('<input id="foo" class="bar baz">', error.pprint`${el}`);

  run_next_test();
});

add_test(function test_toJSON() {
  let e0 = new WebDriverError();
  let e0s = e0.toJSON();
  equal(e0s.error, "webdriver error");
  equal(e0s.message, "");
  equal(e0s.stacktrace, e0.stack);

  let e1 = new WebDriverError("a");
  let e1s = e1.toJSON();
  equal(e1s.message, e1.message);
  equal(e1s.stacktrace, e1.stack);

  let e2 = new JavaScriptError("first", "second", "third", "fourth");
  let e2s = e2.toJSON();
  equal(e2.status, e2s.error);
  equal(e2.message, e2s.message);
  ok(e2s.stacktrace.match(/second/));
  ok(e2s.stacktrace.match(/third/));
  ok(e2s.stacktrace.match(/fourth/));

  run_next_test();
});

add_test(function test_fromJSON() {
  Assert.throws(() => WebDriverError.fromJSON({error: "foo"}),
      /Not of WebDriverError descent/);
  Assert.throws(() => WebDriverError.fromJSON({error: "Error"}),
      /Not of WebDriverError descent/);
  Assert.throws(() => WebDriverError.fromJSON({}),
      /Undeserialisable error type/);
  Assert.throws(() => WebDriverError.fromJSON(undefined),
      /TypeError/);

  // stacks will be different
  let e1 = new WebDriverError("1");
  let e1r = WebDriverError.fromJSON({error: "webdriver error", message: "1"});
  ok(e1r instanceof WebDriverError);
  equal(e1r.name, e1.name);
  equal(e1r.status, e1.status);
  equal(e1r.message, e1.message);

  // stacks will be different
  let e2 = new InvalidArgumentError("2");
  let e2r = WebDriverError.fromJSON({error: "invalid argument", message: "2"});
  ok(e2r instanceof WebDriverError);
  ok(e2r instanceof InvalidArgumentError);
  equal(e2r.name, e2.name);
  equal(e2r.status, e2.status);
  equal(e2r.message, e2.message);

  // test stacks
  let e3j = {error: "no such element", message: "3", stacktrace: "4"};
  let e3r = WebDriverError.fromJSON(e3j);
  ok(e3r instanceof WebDriverError);
  ok(e3r instanceof NoSuchElementError);
  equal(e3r.name, "NoSuchElementError");
  equal(e3r.status, e3j.error);
  equal(e3r.message, e3j.message);
  equal(e3r.stack, e3j.stacktrace);

  // parity with toJSON
  let e4 = new JavaScriptError("first", "second", "third", "fourth");
  let e4s = e4.toJSON();
  deepEqual(e4, WebDriverError.fromJSON(e4s));

  run_next_test();
});

add_test(function test_WebDriverError() {
  let err = new WebDriverError("foo");
  equal("WebDriverError", err.name);
  equal("foo", err.message);
  equal("webdriver error", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_ElementClickInterceptedError() {
  let otherEl = {
    nodeType: 1,
    localName: "a",
    classList: [],
  };
  let obscuredEl = {
    nodeType: 1,
    localName: "b",
    classList: [],
    ownerDocument: {
      elementFromPoint: function (x, y) {
        return otherEl;
      },
    },
    style: {
      pointerEvents: "auto",
    }
  };

  let err1 = new ElementClickInterceptedError(obscuredEl, {x: 1, y: 2});
  equal("ElementClickInterceptedError", err1.name);
  equal("Element <b> is not clickable at point (1,2) " +
      "because another element <a> obscures it",
      err1.message);
  equal("element click intercepted", err1.status);
  ok(err1 instanceof WebDriverError);

  obscuredEl.style.pointerEvents = "none";
  let err2 = new ElementClickInterceptedError(obscuredEl, {x: 1, y: 2});
  equal("Element <b> is not clickable at point (1,2) " +
      "because it does not have pointer events enabled, " +
      "and element <a> would receive the click instead",
      err2.message);

  run_next_test();
});

add_test(function test_ElementNotAccessibleError() {
  let err = new ElementNotAccessibleError("foo");
  equal("ElementNotAccessibleError", err.name);
  equal("foo", err.message);
  equal("element not accessible", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_ElementNotInteractableError() {
  let err = new ElementNotInteractableError("foo");
  equal("ElementNotInteractableError", err.name);
  equal("foo", err.message);
  equal("element not interactable", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_InvalidArgumentError() {
  let err = new InvalidArgumentError("foo");
  equal("InvalidArgumentError", err.name);
  equal("foo", err.message);
  equal("invalid argument", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_InvalidElementStateError() {
  let err = new InvalidElementStateError("foo");
  equal("InvalidElementStateError", err.name);
  equal("foo", err.message);
  equal("invalid element state", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_InvalidSelectorError() {
  let err = new InvalidSelectorError("foo");
  equal("InvalidSelectorError", err.name);
  equal("foo", err.message);
  equal("invalid selector", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_InvalidSessionIDError() {
  let err = new InvalidSessionIDError("foo");
  equal("InvalidSessionIDError", err.name);
  equal("foo", err.message);
  equal("invalid session id", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_JavaScriptError() {
  let err = new JavaScriptError("foo");
  equal("JavaScriptError", err.name);
  equal("foo", err.message);
  equal("javascript error", err.status);
  ok(err instanceof WebDriverError);

  equal("undefined", new JavaScriptError(undefined).message);
  // TODO(ato): Bug 1240550
  //equal("funcname @file", new JavaScriptError("message", "funcname", "file").stack);
  equal("funcname @file, line line",
      new JavaScriptError("message", "funcname", "file", "line").stack);

  // TODO(ato): More exhaustive tests for JS stack computation

  run_next_test();
});

add_test(function test_NoAlertOpenError() {
  let err = new NoAlertOpenError("foo");
  equal("NoAlertOpenError", err.name);
  equal("foo", err.message);
  equal("no such alert", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_NoSuchElementError() {
  let err = new NoSuchElementError("foo");
  equal("NoSuchElementError", err.name);
  equal("foo", err.message);
  equal("no such element", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_NoSuchFrameError() {
  let err = new NoSuchFrameError("foo");
  equal("NoSuchFrameError", err.name);
  equal("foo", err.message);
  equal("no such frame", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_NoSuchWindowError() {
  let err = new NoSuchWindowError("foo");
  equal("NoSuchWindowError", err.name);
  equal("foo", err.message);
  equal("no such window", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_ScriptTimeoutError() {
  let err = new ScriptTimeoutError("foo");
  equal("ScriptTimeoutError", err.name);
  equal("foo", err.message);
  equal("script timeout", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_SessionNotCreatedError() {
  let err = new SessionNotCreatedError("foo");
  equal("SessionNotCreatedError", err.name);
  equal("foo", err.message);
  equal("session not created", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_StaleElementReferenceError() {
  let err = new StaleElementReferenceError("foo");
  equal("StaleElementReferenceError", err.name);
  equal("foo", err.message);
  equal("stale element reference", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_TimeoutError() {
  let err = new TimeoutError("foo");
  equal("TimeoutError", err.name);
  equal("foo", err.message);
  equal("timeout", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_UnableToSetCookieError() {
  let err = new UnableToSetCookieError("foo");
  equal("UnableToSetCookieError", err.name);
  equal("foo", err.message);
  equal("unable to set cookie", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_UnknownCommandError() {
  let err = new UnknownCommandError("foo");
  equal("UnknownCommandError", err.name);
  equal("foo", err.message);
  equal("unknown command", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_UnknownError() {
  let err = new UnknownError("foo");
  equal("UnknownError", err.name);
  equal("foo", err.message);
  equal("unknown error", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});

add_test(function test_UnsupportedOperationError() {
  let err = new UnsupportedOperationError("foo");
  equal("UnsupportedOperationError", err.name);
  equal("foo", err.message);
  equal("unsupported operation", err.status);
  ok(err instanceof WebDriverError);

  run_next_test();
});