tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_Errors.js (18969B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const { error } = ChromeUtils.importESModule(
      6  "chrome://remote/content/shared/webdriver/Errors.sys.mjs"
      7 );
      8 
      9 const errors = [
     10  error.WebDriverError,
     11 
     12  error.DetachedShadowRootError,
     13  error.ElementClickInterceptedError,
     14  error.ElementNotAccessibleError,
     15  error.ElementNotInteractableError,
     16  error.InsecureCertificateError,
     17  error.InvalidArgumentError,
     18  error.InvalidCookieDomainError,
     19  error.InvalidElementStateError,
     20  error.InvalidSelectorError,
     21  error.InvalidSessionIDError,
     22  error.InvalidWebExtensionError,
     23  error.JavaScriptError,
     24  error.MoveTargetOutOfBoundsError,
     25  error.NoSuchAlertError,
     26  error.NoSuchElementError,
     27  error.NoSuchFrameError,
     28  error.NoSuchHandleError,
     29  error.NoSuchInterceptError,
     30  error.NoSuchNetworkCollectorError,
     31  error.NoSuchNetworkDataError,
     32  error.NoSuchNodeError,
     33  error.NoSuchRequestError,
     34  error.NoSuchScriptError,
     35  error.NoSuchShadowRootError,
     36  error.NoSuchWindowError,
     37  error.ScriptTimeoutError,
     38  error.SessionNotCreatedError,
     39  error.StaleElementReferenceError,
     40  error.TimeoutError,
     41  error.UnableToSetCookieError,
     42  error.UnableToSetFileInputError,
     43  error.UnavailableNetworkDataError,
     44  error.UnexpectedAlertOpenError,
     45  error.UnknownCommandError,
     46  error.UnknownError,
     47  error.UnsupportedOperationError,
     48 ];
     49 
     50 function notok(condition) {
     51  ok(!condition);
     52 }
     53 
     54 add_task(function test_isError() {
     55  notok(error.isError(null));
     56  notok(error.isError([]));
     57  notok(error.isError(new Date()));
     58 
     59  ok(error.isError(new Components.Exception()));
     60  ok(error.isError(new Error()));
     61  ok(error.isError(new EvalError()));
     62  ok(error.isError(new InternalError()));
     63  ok(error.isError(new RangeError()));
     64  ok(error.isError(new ReferenceError()));
     65  ok(error.isError(new SyntaxError()));
     66  ok(error.isError(new TypeError()));
     67  ok(error.isError(new URIError()));
     68 
     69  errors.forEach(err => ok(error.isError(new err())));
     70 });
     71 
     72 add_task(function test_isWebDriverError() {
     73  notok(error.isWebDriverError(new Components.Exception()));
     74  notok(error.isWebDriverError(new Error()));
     75  notok(error.isWebDriverError(new EvalError()));
     76  notok(error.isWebDriverError(new InternalError()));
     77  notok(error.isWebDriverError(new RangeError()));
     78  notok(error.isWebDriverError(new ReferenceError()));
     79  notok(error.isWebDriverError(new SyntaxError()));
     80  notok(error.isWebDriverError(new TypeError()));
     81  notok(error.isWebDriverError(new URIError()));
     82 
     83  errors.forEach(err => ok(error.isWebDriverError(new err())));
     84 });
     85 
     86 add_task(function test_wrap() {
     87  // webdriver-derived errors should not be wrapped
     88  errors.forEach(err => {
     89    const unwrappedError = new err("foo");
     90    const wrappedError = error.wrap(unwrappedError);
     91 
     92    ok(wrappedError instanceof error.WebDriverError);
     93    ok(wrappedError instanceof err);
     94    equal(wrappedError.name, unwrappedError.name);
     95    equal(wrappedError.status, unwrappedError.status);
     96    equal(wrappedError.message, "foo");
     97  });
     98 
     99  // JS errors should be wrapped in UnknownError and retain their type
    100  // as part of the message field.
    101  const jsErrors = [
    102    Error,
    103    EvalError,
    104    InternalError,
    105    RangeError,
    106    ReferenceError,
    107    SyntaxError,
    108    TypeError,
    109    URIError,
    110  ];
    111 
    112  jsErrors.forEach(err => {
    113    const originalError = new err("foo");
    114    const wrappedError = error.wrap(originalError);
    115 
    116    ok(wrappedError instanceof error.UnknownError);
    117    equal(wrappedError.name, "UnknownError");
    118    equal(wrappedError.status, "unknown error");
    119    equal(wrappedError.message, `${originalError.name}: foo`);
    120  });
    121 });
    122 
    123 add_task(function test_stringify() {
    124  equal("<unprintable error>", error.stringify());
    125  equal("<unprintable error>", error.stringify("foo"));
    126  equal("[object Object]", error.stringify({}));
    127  equal("[object Object]\nfoo", error.stringify({ stack: "foo" }));
    128  equal("Error: foo", error.stringify(new Error("foo")).split("\n")[0]);
    129 
    130  errors.forEach(err => {
    131    const e = new err("foo");
    132 
    133    equal(`${e.name}: foo`, error.stringify(e).split("\n")[0]);
    134  });
    135 });
    136 
    137 add_task(function test_constructor_from_error() {
    138  const data = { a: 3, b: "bar" };
    139  const origError = new error.WebDriverError("foo", data);
    140 
    141  errors.forEach(err => {
    142    const newError = new err(origError);
    143 
    144    ok(newError instanceof err);
    145    equal(newError.message, origError.message);
    146    equal(newError.stack, origError.stack);
    147    equal(newError.data, origError.data);
    148  });
    149 });
    150 
    151 add_task(function test_stack() {
    152  equal("string", typeof error.stack());
    153  ok(error.stack().includes("test_stack"));
    154  ok(!error.stack().includes("add_task"));
    155 });
    156 
    157 add_task(function test_toJSON() {
    158  errors.forEach(err => {
    159    const e0 = new err();
    160    const e0_json = e0.toJSON();
    161    equal(e0_json.error, e0.status);
    162    equal(e0_json.message, "");
    163    equal(e0_json.stacktrace, e0.stack);
    164    equal(e0_json.data, undefined);
    165 
    166    // message property
    167    const e1 = new err("a");
    168    const e1_json = e1.toJSON();
    169 
    170    equal(e1_json.message, e1.message);
    171    equal(e1_json.stacktrace, e1.stack);
    172    equal(e1_json.data, undefined);
    173 
    174    // message and optional data property
    175    const data = { a: 3, b: "bar" };
    176    const e2 = new err("foo", data);
    177    const e2_json = e2.toJSON();
    178 
    179    equal(e2.status, e2_json.error);
    180    equal(e2.message, e2_json.message);
    181    equal(e2_json.data, data);
    182  });
    183 });
    184 
    185 add_task(function test_fromJSON() {
    186  errors.forEach(err => {
    187    Assert.throws(
    188      () => err.fromJSON({ error: "foo" }),
    189      /Not of WebDriverError descent/
    190    );
    191    Assert.throws(
    192      () => err.fromJSON({ error: "Error" }),
    193      /Not of WebDriverError descent/
    194    );
    195    Assert.throws(() => err.fromJSON({}), /Undeserialisable error type/);
    196    Assert.throws(() => err.fromJSON(undefined), /TypeError/);
    197 
    198    // message and stack
    199    const e1 = new err("1");
    200    const e1_json = { error: e1.status, message: "3", stacktrace: "4" };
    201    const e1_fromJSON = error.WebDriverError.fromJSON(e1_json);
    202 
    203    ok(e1_fromJSON instanceof error.WebDriverError);
    204    ok(e1_fromJSON instanceof err);
    205    equal(e1_fromJSON.name, e1.name);
    206    equal(e1_fromJSON.status, e1_json.error);
    207    equal(e1_fromJSON.message, e1_json.message);
    208    equal(e1_fromJSON.stack, e1_json.stacktrace);
    209 
    210    // message and optional data
    211    const e2_data = { a: 3, b: "bar" };
    212    const e2 = new err("1", e2_data);
    213    const e2_json = { error: e1.status, message: "3", data: e2_data };
    214    const e2_fromJSON = error.WebDriverError.fromJSON(e2_json);
    215 
    216    ok(e2_fromJSON instanceof error.WebDriverError);
    217    ok(e2_fromJSON instanceof err);
    218    equal(e2_fromJSON.name, e2.name);
    219    equal(e2_fromJSON.status, e2_json.error);
    220    equal(e2_fromJSON.message, e2_json.message);
    221    equal(e2_fromJSON.data, e2_json.data);
    222 
    223    // parity with toJSON
    224    const e3_data = { a: 3, b: "bar" };
    225    const e3 = new err("1", e3_data);
    226    const e3_json = e3.toJSON();
    227    const e3_fromJSON = error.WebDriverError.fromJSON(e3_json);
    228 
    229    equal(e3_json.error, e3_fromJSON.status);
    230    equal(e3_json.message, e3_fromJSON.message);
    231    equal(e3_json.stacktrace, e3_fromJSON.stack);
    232  });
    233 });
    234 
    235 add_task(function test_WebDriverError() {
    236  let err = new error.WebDriverError("foo");
    237  equal("WebDriverError", err.name);
    238  equal("foo", err.message);
    239  equal("webdriver error", err.status);
    240  ok(err instanceof error.WebDriverError);
    241 });
    242 
    243 add_task(function test_DetachedShadowRootError() {
    244  let err = new error.DetachedShadowRootError("foo");
    245  equal("DetachedShadowRootError", err.name);
    246  equal("foo", err.message);
    247  equal("detached shadow root", err.status);
    248  ok(err instanceof error.WebDriverError);
    249 });
    250 
    251 add_task(function test_ElementClickInterceptedError() {
    252  let otherEl = {
    253    hasAttribute: attr => attr in otherEl,
    254    getAttribute: attr => (attr in otherEl ? otherEl[attr] : null),
    255    nodeType: 1,
    256    localName: "a",
    257  };
    258  let obscuredEl = {
    259    hasAttribute: attr => attr in obscuredEl,
    260    getAttribute: attr => (attr in obscuredEl ? obscuredEl[attr] : null),
    261    nodeType: 1,
    262    localName: "b",
    263    ownerDocument: {
    264      elementFromPoint() {
    265        return otherEl;
    266      },
    267    },
    268    style: {
    269      pointerEvents: "auto",
    270    },
    271  };
    272 
    273  let err1 = new error.ElementClickInterceptedError(
    274    undefined,
    275    undefined,
    276    obscuredEl,
    277    { x: 1, y: 2 }
    278  );
    279  equal("ElementClickInterceptedError", err1.name);
    280  equal(
    281    "Element <b> is not clickable at point (1,2) " +
    282      "because another element <a> obscures it",
    283    err1.message
    284  );
    285  equal("element click intercepted", err1.status);
    286  ok(err1 instanceof error.WebDriverError);
    287 
    288  obscuredEl.style.pointerEvents = "none";
    289  let err2 = new error.ElementClickInterceptedError(
    290    undefined,
    291    undefined,
    292    obscuredEl,
    293    { x: 1, y: 2 }
    294  );
    295  equal(
    296    "Element <b> is not clickable at point (1,2) " +
    297      "because it does not have pointer events enabled, " +
    298      "and element <a> would receive the click instead",
    299    err2.message
    300  );
    301 });
    302 
    303 add_task(function test_ElementNotAccessibleError() {
    304  let err = new error.ElementNotAccessibleError("foo");
    305  equal("ElementNotAccessibleError", err.name);
    306  equal("foo", err.message);
    307  equal("element not accessible", err.status);
    308  ok(err instanceof error.WebDriverError);
    309 });
    310 
    311 add_task(function test_ElementNotInteractableError() {
    312  let err = new error.ElementNotInteractableError("foo");
    313  equal("ElementNotInteractableError", err.name);
    314  equal("foo", err.message);
    315  equal("element not interactable", err.status);
    316  ok(err instanceof error.WebDriverError);
    317 });
    318 
    319 add_task(function test_InsecureCertificateError() {
    320  let err = new error.InsecureCertificateError("foo");
    321  equal("InsecureCertificateError", err.name);
    322  equal("foo", err.message);
    323  equal("insecure certificate", err.status);
    324  ok(err instanceof error.WebDriverError);
    325 });
    326 
    327 add_task(function test_InvalidArgumentError() {
    328  let err = new error.InvalidArgumentError("foo");
    329  equal("InvalidArgumentError", err.name);
    330  equal("foo", err.message);
    331  equal("invalid argument", err.status);
    332  ok(err instanceof error.WebDriverError);
    333 });
    334 
    335 add_task(function test_InvalidCookieDomainError() {
    336  let err = new error.InvalidCookieDomainError("foo");
    337  equal("InvalidCookieDomainError", err.name);
    338  equal("foo", err.message);
    339  equal("invalid cookie domain", err.status);
    340  ok(err instanceof error.WebDriverError);
    341 });
    342 
    343 add_task(function test_InvalidElementStateError() {
    344  let err = new error.InvalidElementStateError("foo");
    345  equal("InvalidElementStateError", err.name);
    346  equal("foo", err.message);
    347  equal("invalid element state", err.status);
    348  ok(err instanceof error.WebDriverError);
    349 });
    350 
    351 add_task(function test_InvalidSelectorError() {
    352  let err = new error.InvalidSelectorError("foo");
    353  equal("InvalidSelectorError", err.name);
    354  equal("foo", err.message);
    355  equal("invalid selector", err.status);
    356  ok(err instanceof error.WebDriverError);
    357 });
    358 
    359 add_task(function test_InvalidSessionIDError() {
    360  let err = new error.InvalidSessionIDError("foo");
    361  equal("InvalidSessionIDError", err.name);
    362  equal("foo", err.message);
    363  equal("invalid session id", err.status);
    364  ok(err instanceof error.WebDriverError);
    365 });
    366 
    367 add_task(function test_InvalidWebExtensionError() {
    368  let err = new error.InvalidWebExtensionError("foo");
    369  equal("InvalidWebExtensionError", err.name);
    370  equal("foo", err.message);
    371  equal("invalid web extension", err.status);
    372  ok(err instanceof error.WebDriverError);
    373 });
    374 
    375 add_task(function test_JavaScriptError() {
    376  let err = new error.JavaScriptError("foo");
    377  equal("JavaScriptError", err.name);
    378  equal("foo", err.message);
    379  equal("javascript error", err.status);
    380  ok(err instanceof error.WebDriverError);
    381 
    382  equal("", new error.JavaScriptError(undefined).message);
    383 
    384  let superErr = new RangeError("foo");
    385  let inheritedErr = new error.JavaScriptError(superErr);
    386  equal("RangeError: foo", inheritedErr.message);
    387  equal(superErr.stack, inheritedErr.stack);
    388  equal(superErr.lineNumber, inheritedErr.lineNumber);
    389  equal(superErr.columnNumber, inheritedErr.columnNumber);
    390 });
    391 
    392 add_task(function test_MoveTargetOutOfBoundsError() {
    393  let err = new error.MoveTargetOutOfBoundsError("foo");
    394  equal("MoveTargetOutOfBoundsError", err.name);
    395  equal("foo", err.message);
    396  equal("move target out of bounds", err.status);
    397  ok(err instanceof error.WebDriverError);
    398 });
    399 
    400 add_task(function test_NoSuchAlertError() {
    401  let err = new error.NoSuchAlertError("foo");
    402  equal("NoSuchAlertError", err.name);
    403  equal("foo", err.message);
    404  equal("no such alert", err.status);
    405  ok(err instanceof error.WebDriverError);
    406 });
    407 
    408 add_task(function test_NoSuchElementError() {
    409  let err = new error.NoSuchElementError("foo");
    410  equal("NoSuchElementError", err.name);
    411  equal("foo", err.message);
    412  equal("no such element", err.status);
    413  ok(err instanceof error.WebDriverError);
    414 });
    415 
    416 add_task(function test_NoSuchFrameError() {
    417  let err = new error.NoSuchFrameError("foo");
    418  equal("NoSuchFrameError", err.name);
    419  equal("foo", err.message);
    420  equal("no such frame", err.status);
    421  ok(err instanceof error.WebDriverError);
    422 });
    423 
    424 add_task(function test_NoSuchHandleError() {
    425  let err = new error.NoSuchHandleError("foo");
    426  equal("NoSuchHandleError", err.name);
    427  equal("foo", err.message);
    428  equal("no such handle", err.status);
    429  ok(err instanceof error.WebDriverError);
    430 });
    431 
    432 add_task(function test_NoSuchInterceptError() {
    433  let err = new error.NoSuchInterceptError("foo");
    434  equal("NoSuchInterceptError", err.name);
    435  equal("foo", err.message);
    436  equal("no such intercept", err.status);
    437  ok(err instanceof error.WebDriverError);
    438 });
    439 
    440 add_task(function test_NoSuchNetworkCollectorError() {
    441  let err = new error.NoSuchNetworkCollectorError("foo");
    442  equal("NoSuchNetworkCollectorError", err.name);
    443  equal("foo", err.message);
    444  equal("no such network collector", err.status);
    445  ok(err instanceof error.WebDriverError);
    446 });
    447 
    448 add_task(function test_NoSuchNetworkDataError() {
    449  let err = new error.NoSuchNetworkDataError("foo");
    450  equal("NoSuchNetworkDataError", err.name);
    451  equal("foo", err.message);
    452  equal("no such network data", err.status);
    453  ok(err instanceof error.WebDriverError);
    454 });
    455 
    456 add_task(function test_NoSuchNodeError() {
    457  let err = new error.NoSuchNodeError("foo");
    458  equal("NoSuchNodeError", err.name);
    459  equal("foo", err.message);
    460  equal("no such node", err.status);
    461  ok(err instanceof error.WebDriverError);
    462 });
    463 
    464 add_task(function test_NoSuchRequestError() {
    465  let err = new error.NoSuchRequestError("foo");
    466  equal("NoSuchRequestError", err.name);
    467  equal("foo", err.message);
    468  equal("no such request", err.status);
    469  ok(err instanceof error.WebDriverError);
    470 });
    471 
    472 add_task(function test_NoSuchScriptError() {
    473  let err = new error.NoSuchScriptError("foo");
    474  equal("NoSuchScriptError", err.name);
    475  equal("foo", err.message);
    476  equal("no such script", err.status);
    477  ok(err instanceof error.WebDriverError);
    478 });
    479 
    480 add_task(function test_NoSuchShadowRootError() {
    481  let err = new error.NoSuchShadowRootError("foo");
    482  equal("NoSuchShadowRootError", err.name);
    483  equal("foo", err.message);
    484  equal("no such shadow root", err.status);
    485  ok(err instanceof error.WebDriverError);
    486 });
    487 
    488 add_task(function test_NoSuchUserContextError() {
    489  let err = new error.NoSuchUserContextError("foo");
    490  equal("NoSuchUserContextError", err.name);
    491  equal("foo", err.message);
    492  equal("no such user context", err.status);
    493  ok(err instanceof error.WebDriverError);
    494 });
    495 
    496 add_task(function test_NoSuchWebExtensionError() {
    497  let err = new error.NoSuchWebExtensionError("foo");
    498  equal("NoSuchWebExtensionError", err.name);
    499  equal("foo", err.message);
    500  equal("no such web extension", err.status);
    501  ok(err instanceof error.WebDriverError);
    502 });
    503 
    504 add_task(function test_NoSuchWindowError() {
    505  let err = new error.NoSuchWindowError("foo");
    506  equal("NoSuchWindowError", err.name);
    507  equal("foo", err.message);
    508  equal("no such window", err.status);
    509  ok(err instanceof error.WebDriverError);
    510 });
    511 
    512 add_task(function test_ScriptTimeoutError() {
    513  let err = new error.ScriptTimeoutError("foo");
    514  equal("ScriptTimeoutError", err.name);
    515  equal("foo", err.message);
    516  equal("script timeout", err.status);
    517  ok(err instanceof error.WebDriverError);
    518 });
    519 
    520 add_task(function test_SessionNotCreatedError() {
    521  let err = new error.SessionNotCreatedError("foo");
    522  equal("SessionNotCreatedError", err.name);
    523  equal("foo", err.message);
    524  equal("session not created", err.status);
    525  ok(err instanceof error.WebDriverError);
    526 });
    527 
    528 add_task(function test_StaleElementReferenceError() {
    529  let err = new error.StaleElementReferenceError("foo");
    530  equal("StaleElementReferenceError", err.name);
    531  equal("foo", err.message);
    532  equal("stale element reference", err.status);
    533  ok(err instanceof error.WebDriverError);
    534 });
    535 
    536 add_task(function test_TimeoutError() {
    537  let err = new error.TimeoutError("foo");
    538  equal("TimeoutError", err.name);
    539  equal("foo", err.message);
    540  equal("timeout", err.status);
    541  ok(err instanceof error.WebDriverError);
    542 });
    543 
    544 add_task(function test_UnableToSetCookieError() {
    545  let err = new error.UnableToSetCookieError("foo");
    546  equal("UnableToSetCookieError", err.name);
    547  equal("foo", err.message);
    548  equal("unable to set cookie", err.status);
    549  ok(err instanceof error.WebDriverError);
    550 });
    551 
    552 add_task(function test_UnableToSetFileInputError() {
    553  let err = new error.UnableToSetFileInputError("foo");
    554  equal("UnableToSetFileInputError", err.name);
    555  equal("foo", err.message);
    556  equal("unable to set file input", err.status);
    557  ok(err instanceof error.WebDriverError);
    558 });
    559 
    560 add_task(function test_UnavailableNetworkDataError() {
    561  let err = new error.UnavailableNetworkDataError("foo");
    562  equal("UnavailableNetworkDataError", err.name);
    563  equal("foo", err.message);
    564  equal("unavailable network data", err.status);
    565  ok(err instanceof error.WebDriverError);
    566 });
    567 
    568 add_task(function test_UnexpectedAlertOpenError() {
    569  let err = new error.UnexpectedAlertOpenError("foo");
    570  equal("UnexpectedAlertOpenError", err.name);
    571  equal("foo", err.message);
    572  equal("unexpected alert open", err.status);
    573  ok(err instanceof error.WebDriverError);
    574 });
    575 
    576 add_task(function test_UnknownCommandError() {
    577  let err = new error.UnknownCommandError("foo");
    578  equal("UnknownCommandError", err.name);
    579  equal("foo", err.message);
    580  equal("unknown command", err.status);
    581  ok(err instanceof error.WebDriverError);
    582 });
    583 
    584 add_task(function test_UnknownError() {
    585  let err = new error.UnknownError("foo");
    586  equal("UnknownError", err.name);
    587  equal("foo", err.message);
    588  equal("unknown error", err.status);
    589  ok(err instanceof error.WebDriverError);
    590 });
    591 
    592 add_task(function test_UnsupportedOperationError() {
    593  let err = new error.UnsupportedOperationError("foo");
    594  equal("UnsupportedOperationError", err.name);
    595  equal("foo", err.message);
    596  equal("unsupported operation", err.status);
    597  ok(err instanceof error.WebDriverError);
    598 });