Bug 1676736 - Use Assert.rejects to test exceptions in Remote browser chrome test r=jdescottes,webdriver-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D180723
This commit is contained in:
scottlynotlie 2023-08-03 09:32:41 +00:00
Родитель ed29a8210b
Коммит 6dd16cb775
32 изменённых файлов: 444 добавлений и 725 удалений

Просмотреть файл

@ -7,16 +7,17 @@ add_task(async function ({ CDP }) {
const { webSocketDebuggerUrl } = await CDP.Version();
const client = await CDP({ target: webSocketDebuggerUrl });
try {
await client.send("Hoobaflooba");
} catch (e) {
ok(e.message.match(/Invalid method format/));
}
try {
await client.send("Hooba.flooba");
} catch (e) {
ok(e.message.match(/UnknownMethodError/));
}
await Assert.rejects(
client.send("Hoobaflooba"),
/Invalid method format/,
`Fails with invalid method format`
);
await Assert.rejects(
client.send("Hooba.flooba"),
/UnknownMethodError/,
`Fails with UnknownMethodError`
);
await client.close();
});

Просмотреть файл

@ -10,14 +10,9 @@ add_task(async function objectIdInvalidTypes({ client }) {
const { DOM } = client;
for (const objectId of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await DOM.describeNode({ objectId });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/objectId: string value expected/),
await Assert.rejects(
DOM.describeNode({ objectId }),
/objectId: string value expected/,
`Fails with invalid type: ${objectId}`
);
}
@ -26,15 +21,10 @@ add_task(async function objectIdInvalidTypes({ client }) {
add_task(async function objectIdUnknownValue({ client }) {
const { DOM } = client;
let errorThrown = "";
try {
await DOM.describeNode({ objectId: "foo" });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/Could not find object with given id/),
"Fails with unknown objectId"
await Assert.rejects(
DOM.describeNode({ objectId: "foo" }),
/Could not find object with given id/,
`Fails with unknown objectId`
);
});
@ -46,15 +36,10 @@ add_task(async function objectIdIsNotANode({ client }) {
expression: "[42]",
});
let errorThrown = "";
try {
await DOM.describeNode({ objectId: result.objectId });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/Object id doesn't reference a Node/),
"Fails if objectId doesn't reference a DOM node"
await Assert.rejects(
DOM.describeNode({ objectId: result.objectId }),
/Object id doesn't reference a Node/,
`Fails if objectId doesn't reference a DOM node`
);
});

Просмотреть файл

@ -7,14 +7,9 @@ add_task(async function backendNodeIdInvalidTypes({ client }) {
const { DOM } = client;
for (const backendNodeId of [null, true, "foo", [], {}]) {
let errorThrown = "";
try {
await DOM.resolveNode({ backendNodeId });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/backendNodeId: number value expected/),
await Assert.rejects(
DOM.resolveNode({ backendNodeId }),
/backendNodeId: number value expected/,
`Fails for invalid type: ${backendNodeId}`
);
}
@ -23,14 +18,9 @@ add_task(async function backendNodeIdInvalidTypes({ client }) {
add_task(async function backendNodeIdInvalidValue({ client }) {
const { DOM } = client;
let errorThrown = "";
try {
await DOM.resolveNode({ backendNodeId: -1 });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/No node with given id found/),
await Assert.rejects(
DOM.resolveNode({ backendNodeId: -1 }),
/No node with given id found/,
"Fails for unknown backendNodeId"
);
});
@ -60,17 +50,12 @@ add_task(async function executionContextIdInvalidTypes({ client }) {
const { node } = await DOM.describeNode({ objectId: result.objectId });
for (const executionContextId of [null, true, "foo", [], {}]) {
let errorThrown = "";
try {
await DOM.resolveNode({
await Assert.rejects(
DOM.resolveNode({
backendNodeId: node.backendNodeId,
executionContextId,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/executionContextId: integer value expected/),
}),
/executionContextId: integer value expected/,
`Fails for invalid type: ${executionContextId}`
);
}
@ -83,17 +68,12 @@ add_task(async function executionContextIdInvalidValue({ client }) {
const { result } = await Runtime.evaluate({ expression: "document" });
const { node } = await DOM.describeNode({ objectId: result.objectId });
let errorThrown = "";
try {
await DOM.resolveNode({
await Assert.rejects(
DOM.resolveNode({
backendNodeId: node.backendNodeId,
executionContextId: -1,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/Node with given id does not belong to the document/),
}),
/Node with given id does not belong to the document/,
"Fails for unknown executionContextId"
);
});

Просмотреть файл

@ -210,13 +210,14 @@ add_task(async function failsWithNegativeWidth({ client }) {
deviceScaleFactor: 1.0,
};
let errorThrown = false;
try {
await Emulation.setDeviceMetricsOverride(overrideSettings);
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Negative width raised error");
await Assert.rejects(
Emulation.setDeviceMetricsOverride(overrideSettings),
err =>
err.message.includes(
"Width and height values must be positive, not greater than 10000000"
),
"Negative width raised error"
);
await loadURL(DOC_SMALL);
const updatedLayoutMetrics = await Page.getLayoutMetrics();
@ -247,13 +248,14 @@ add_task(async function failsWithTooLargeWidth({ client }) {
deviceScaleFactor: 1.0,
};
let errorThrown = false;
try {
await Emulation.setDeviceMetricsOverride(overrideSettings);
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Too large width raised error");
await Assert.rejects(
Emulation.setDeviceMetricsOverride(overrideSettings),
err =>
err.message.includes(
"Width and height values must be positive, not greater than 10000000"
),
"Too large width raised error"
);
await loadURL(DOC_SMALL);
const updatedLayoutMetrics = await Page.getLayoutMetrics();
@ -284,14 +286,14 @@ add_task(async function failsWithNegativeHeight({ client }) {
deviceScaleFactor: 1.0,
};
let errorThrown = false;
try {
await Emulation.setDeviceMetricsOverride(overrideSettings);
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Negative height raised error");
await Assert.rejects(
Emulation.setDeviceMetricsOverride(overrideSettings),
err =>
err.message.includes(
"Width and height values must be positive, not greater than 10000000"
),
"Negative height raised error"
);
await loadURL(DOC_SMALL);
const updatedLayoutMetrics = await Page.getLayoutMetrics();
@ -320,14 +322,14 @@ add_task(async function failsWithTooLargeHeight({ client }) {
height: MAX_WINDOW_SIZE + 1,
deviceScaleFactor: 1.0,
};
let errorThrown = false;
try {
await Emulation.setDeviceMetricsOverride(overrideSettings);
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Too large height raised error");
await Assert.rejects(
Emulation.setDeviceMetricsOverride(overrideSettings),
err =>
err.message.includes(
"Width and height values must be positive, not greater than 10000000"
),
"Too large height raised error"
);
await loadURL(DOC_SMALL);
const updatedLayoutMetrics = await Page.getLayoutMetrics();
@ -378,13 +380,11 @@ add_task(async function failsWithNegativeRatio({ client }) {
deviceScaleFactor: -1,
};
let errorThrown = false;
try {
await Emulation.setDeviceMetricsOverride(overrideSettings);
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Negative device scale factor raised error");
await Assert.rejects(
Emulation.setDeviceMetricsOverride(overrideSettings),
err => err.message.includes("deviceScaleFactor: must be positive"),
"Negative device scale factor raised error"
);
await loadURL(DOC_SMALL);
const updatedLayoutMetrics = await Page.getLayoutMetrics();

Просмотреть файл

@ -9,14 +9,9 @@ add_task(async function invalidEnabledType({ client }) {
const { Emulation } = client;
for (const enabled of [null, "", 1, [], {}]) {
let errorThrown = "";
try {
await Emulation.setTouchEmulationEnabled({ enabled });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/enabled: boolean value expected/),
await Assert.rejects(
Emulation.setTouchEmulationEnabled({ enabled }),
/enabled: boolean value expected/,
`Fails with invalid type: ${enabled}`
);
}

Просмотреть файл

@ -10,14 +10,9 @@ add_task(async function invalidPlatform({ client }) {
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";
for (const platform of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await Emulation.setUserAgentOverride({ userAgent, platform });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/platform: string value expected/),
await Assert.rejects(
Emulation.setUserAgentOverride({ userAgent, platform }),
/platform: string value expected/,
`Fails with invalid type: ${platform}`
);
}
@ -49,7 +44,8 @@ add_task(async function setAndResetUserAgent({ client }) {
);
});
add_task(async function invalidUserAgent({ Emulation }) {
add_task(async function invalidUserAgent({ client }) {
const { Emulation } = client;
const userAgent = "Mozilla/5.0 (rv: 23) Romanesco/42.0\n";
await loadURL(DOC);
@ -59,13 +55,11 @@ add_task(async function invalidUserAgent({ Emulation }) {
"Custom user agent hasn't been set"
);
let errorThrown = false;
try {
await Emulation.setUserAgentOverride({ userAgent });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Invalid user agent format raised error");
await Assert.rejects(
Emulation.setUserAgentOverride({ userAgent }),
err => err.message.includes("Invalid characters found in userAgent"),
"Invalid user agent format raised error"
);
});
add_task(async function setAndResetPlatform({ client }) {

Просмотреть файл

@ -19,28 +19,20 @@ add_task(async function unknownHandle({ client }) {
const { IO } = client;
const handle = "1000000";
try {
await IO.close({ handle });
ok(false, "Close shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`Invalid stream handle`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.close({ handle }),
err => err.message.includes(`Invalid stream handle`),
"Error contains expected message"
);
});
add_task(async function invalidHandleTypes({ client }) {
const { IO } = client;
for (const handle of [null, true, 1, [], {}]) {
try {
await IO.close({ handle });
ok(false, "Close shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`handle: string value expected`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.close({ handle }),
err => err.message.includes(`handle: string value expected`),
"Error contains expected message"
);
}
});

Просмотреть файл

@ -88,44 +88,32 @@ add_task(async function readAfterClose({ client }) {
ok(!(await IOUtils.exists(stream.path)), "File should no longer exist");
try {
await IO.read({ handle });
ok(false, "Read shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`Invalid stream handle`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.read({ handle }),
err => err.message.includes(`Invalid stream handle`),
"Error contains expected message"
);
});
add_task(async function unknownHandle({ client }) {
const { IO } = client;
const handle = "1000000";
try {
await IO.read({ handle });
ok(false, "Read shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`Invalid stream handle`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.read({ handle }),
err => err.message.includes(`Invalid stream handle`),
"Error contains expected message"
);
});
add_task(async function invalidHandleTypes({ client }) {
const { IO } = client;
for (const handle of [null, true, 1, [], {}]) {
try {
await IO.read({ handle });
ok(false, "Read shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`handle: string value expected`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.read({ handle }),
err => err.message.includes(`handle: string value expected`),
"Error contains expected message"
);
}
});
@ -135,15 +123,11 @@ add_task(async function invalidOffsetTypes({ client }) {
const { handle } = await registerFileStream(contents);
for (const offset of [null, true, "1", [], {}]) {
try {
await IO.read({ handle, offset });
ok(false, "Read shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`offset: integer value expected`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.read({ handle, offset }),
err => err.message.includes(`offset: integer value expected`),
"Error contains expected message"
);
}
});
@ -153,14 +137,10 @@ add_task(async function invalidSizeTypes({ client }) {
const { handle } = await registerFileStream(contents);
for (const size of [null, true, "1", [], {}]) {
try {
await IO.read({ handle, size });
ok(false, "Read shouldn't pass");
} catch (e) {
ok(
e.message.startsWith(`size: integer value expected`),
"Error contains expected message"
);
}
await Assert.rejects(
IO.read({ handle, size }),
err => err.message.includes(`size: integer value expected`),
"Error contains expected message"
);
}
});

Просмотреть файл

@ -21,25 +21,24 @@ registerCleanupFunction(() => {
add_task(async function failureWithoutArguments({ client }) {
const { Network } = client;
let errorThrown = false;
try {
await Network.deleteCookies();
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without any arguments");
await Assert.rejects(
Network.deleteCookies(),
err => err.message.includes("name: string value expected"),
"Fails without any arguments"
);
});
add_task(async function failureWithoutDomainOrURL({ client }) {
const { Network } = client;
let errorThrown = false;
try {
await Network.deleteCookies({ name: "foo" });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without domain or URL");
await Assert.rejects(
Network.deleteCookies({ name: "foo" }),
err =>
err.message.includes(
"At least one of the url and domain needs to be specified"
),
"Fails without domain or URL"
);
});
add_task(async function failureWithInvalidProtocol({ client }) {
@ -47,13 +46,11 @@ add_task(async function failureWithInvalidProtocol({ client }) {
const FTP_URL = `ftp://${DEFAULT_HOSTNAME}`;
let errorThrown = false;
try {
await Network.deleteCookies({ name: "foo", url: FTP_URL });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails for invalid protocol in URL");
await Assert.rejects(
Network.deleteCookies({ name: "foo", url: FTP_URL }),
err => err.message.includes("An http or https url must be specified"),
"Fails for invalid protocol in URL"
);
});
add_task(async function pristineContext({ client }) {

Просмотреть файл

@ -36,15 +36,9 @@ function add_networking_task(taskFn) {
add_networking_task(async function offlineWithoutArguments({ client }) {
const { Network } = client;
let errorThrown = "";
try {
await Network.emulateNetworkConditions();
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/offline: boolean value expected/),
await Assert.rejects(
Network.emulateNetworkConditions(),
/offline: boolean value expected/,
"Fails without any arguments"
);
});
@ -52,15 +46,9 @@ add_networking_task(async function offlineWithoutArguments({ client }) {
add_networking_task(async function offlineWithEmptyArguments({ client }) {
const { Network } = client;
let errorThrown = "";
try {
await Network.emulateNetworkConditions({});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/offline: boolean value expected/),
await Assert.rejects(
Network.emulateNetworkConditions({}),
/offline: boolean value expected/,
"Fails with only empty arguments"
);
});
@ -70,17 +58,10 @@ add_networking_task(async function offlineWithInvalidArguments({ client }) {
const testTable = [null, undefined, 1, "foo", [], {}];
for (const testCase of testTable) {
let errorThrown = "";
try {
await Network.emulateNetworkConditions({ offline: testCase });
} catch (e) {
errorThrown = e.message;
}
const testType = typeof testCase;
ok(
errorThrown.match(/offline: boolean value expected/),
await Assert.rejects(
Network.emulateNetworkConditions({ offline: testCase }),
/offline: boolean value expected/,
`Fails with ${testType}-type argument for offline`
);
}

Просмотреть файл

@ -321,15 +321,9 @@ add_task(async function testUrlsInvalidTypes({ client }) {
const testTable = [null, 1, "foo", true, {}];
for (const testCase of testTable) {
let errorThrown = "";
try {
await Network.getCookies({ urls: testCase });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/urls: array expected/),
await Assert.rejects(
Network.getCookies({ urls: testCase }),
/urls: array expected/,
`Fails the argument type for urls`
);
}
@ -341,15 +335,9 @@ add_task(async function testUrlsEntriesInvalidTypes({ client }) {
const testTable = [[null], [1], [true]];
for (const testCase of testTable) {
let errorThrown = "";
try {
await Network.getCookies({ urls: testCase });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/urls: string value expected at index 0/),
await Assert.rejects(
Network.getCookies({ urls: testCase }),
/urls: string value expected at index 0/,
`Fails the argument type for urls`
);
}

Просмотреть файл

@ -12,51 +12,46 @@ const SECURE_HOST = "example.com";
add_task(async function failureWithoutArguments({ client }) {
const { Network } = client;
let errorThrown = false;
try {
await Network.setCookie();
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without any arguments");
await Assert.rejects(
Network.setCookie(),
err => err.message.includes("name: string value expected"),
"Fails without any arguments"
);
});
add_task(async function failureWithMissingNameAndValue({ client }) {
const { Network } = client;
let errorThrown = false;
try {
await Network.setCookie({
await Assert.rejects(
Network.setCookie({
value: "bar",
domain: "example.org",
});
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without name specified");
}),
err => err.message.includes("name: string value expected"),
"Fails without name specified"
);
errorThrown = false;
try {
await Network.setCookie({
await Assert.rejects(
Network.setCookie({
name: "foo",
domain: "example.org",
});
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without value specified");
}),
err => err.message.includes("value: string value expected"),
"Fails without value specified"
);
});
add_task(async function failureWithMissingDomainAndURL({ client }) {
const { Network } = client;
let errorThrown = false;
try {
await Network.setCookie({ name: "foo", value: "bar" });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without domain and URL specified");
await Assert.rejects(
Network.setCookie({ name: "foo", value: "bar" }),
err =>
err.message.includes(
"At least one of the url and domain needs to be specified"
),
"Fails without domain and URL specified"
);
});
add_task(async function setCookieWithDomain({ client }) {

Просмотреть файл

@ -9,13 +9,11 @@ const DEFAULT_HOST = "example.org";
add_task(async function failureWithoutArguments({ client }) {
const { Network } = client;
let errorThrown = false;
try {
await Network.setCookies();
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Fails without any arguments");
await Assert.rejects(
Network.setCookies(),
err => err.message.includes("Invalid parameters (cookies: array expected)"),
"Fails without any arguments"
);
});
add_task(async function setCookies({ client }) {
@ -58,13 +56,9 @@ add_task(async function setCookiesWithInvalidField({ client }) {
},
];
let errorThrown = false;
try {
await Network.setCookies({ cookies });
} catch (e) {
errorThrown = true;
} finally {
Services.cookies.removeAll();
}
ok(errorThrown, "Fails with an invalid field");
await Assert.rejects(
Network.setCookies({ cookies }),
err => err.message.includes("Invalid cookie fields"),
"Fails with an invalid field"
);
});

Просмотреть файл

@ -56,13 +56,11 @@ add_task(async function invalidFormat({ client }) {
const { Page } = client;
await loadURL(toDataURL("<div>Hello world"));
let errorThrown = false;
try {
await Page.captureScreenshot({ format: "foo" });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "captureScreenshot raised error for invalid image format");
await Assert.rejects(
Page.captureScreenshot({ format: "foo" }),
err => err.message.includes(`Unsupported MIME type: image`),
"captureScreenshot raised error for invalid image format"
);
});
add_task(async function asJPEGFormat({ client }) {
@ -162,13 +160,11 @@ add_task(async function clipMissingProperties({ client }) {
};
clip[prop] = undefined;
let errorThrown = false;
try {
await Page.captureScreenshot({ clip });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, `raised error for missing clip.${prop} property`);
await Assert.rejects(
Page.captureScreenshot({ clip }),
err => err.message.includes(`clip.${prop}: double value expected`),
`raised error for missing clip.${prop} property`
);
}
});

Просмотреть файл

@ -15,17 +15,12 @@ const CLEARED = "Runtime.executionContextsCleared";
add_task(async function frameIdMissing({ client }) {
const { Page } = client;
let errorThrown = "";
try {
await Page.createIsolatedWorld({
await Assert.rejects(
Page.createIsolatedWorld({
worldName: WORLD_NAME_1,
grantUniversalAccess: true,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/frameId: string value expected/),
}),
/frameId: string value expected/,
`Fails with missing frameId`
);
});
@ -34,16 +29,11 @@ add_task(async function frameIdInvalidTypes({ client }) {
const { Page } = client;
for (const frameId of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await Page.createIsolatedWorld({
await Assert.rejects(
Page.createIsolatedWorld({
frameId,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/frameId: string value expected/),
}),
/frameId: string value expected/,
`Fails with invalid type: ${frameId}`
);
}
@ -60,17 +50,12 @@ add_task(async function worldNameInvalidTypes({ client }) {
await loadEvent;
for (const worldName of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await Page.createIsolatedWorld({
await Assert.rejects(
Page.createIsolatedWorld({
frameId,
worldName,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/worldName: string value expected/),
}),
/worldName: string value expected/,
`Fails with invalid type: ${worldName}`
);
}
@ -334,18 +319,13 @@ add_task(async function evaluateInIsolatedAndDefault({ client }) {
});
is(result1.value, 11, "Isolated context incremented the expected value");
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
executionContextId: isolatedContext.id,
functionDeclaration: "arg => ++arg.foo",
arguments: [{ objectId: objDefault.objectId }],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/Could not find object with given id/),
}),
/Could not find object with given id/,
"Contexts do not share objects"
);
});

Просмотреть файл

@ -154,26 +154,19 @@ add_task(async function testNotFound({ client }) {
add_task(async function testInvalidURL({ client }) {
const { Page } = client;
let message = "";
for (let url of ["blah.com", "foo", "https\n//", "http", ""]) {
message = "";
try {
await Page.navigate({ url });
} catch (e) {
message = e.response.message;
}
ok(message.includes("invalid URL"), `Invalid url ${url} causes error`);
await Assert.rejects(
Page.navigate({ url }),
err => err.message.includes("invalid URL"),
`Invalid url ${url} causes error`
);
}
for (let url of [2, {}, true]) {
message = "";
try {
await Page.navigate({ url });
} catch (e) {
message = e.response.message;
}
ok(
message.includes("string value expected"),
await Assert.rejects(
Page.navigate({ url }),
err => err.message.includes("string value expected"),
`Invalid url ${url} causes error`
);
}

Просмотреть файл

@ -9,14 +9,9 @@ add_task(async function toUnknownEntryId({ client }) {
const { entries } = await Page.getNavigationHistory();
const ids = entries.map(entry => entry.id);
let errorThrown = "";
try {
await Page.navigateToHistoryEntry({ entryId: Math.max(...ids) + 1 });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/No entry with passed id/),
await Assert.rejects(
Page.navigateToHistoryEntry({ entryId: Math.max(...ids) + 1 }),
/No entry with passed id/,
"Unknown entry id raised error"
);
});

Просмотреть файл

@ -7,14 +7,11 @@ const TEST_DOC = toDataURL("default-test-page");
add_task(async function FunctionDeclarationMissing({ client }) {
const { Runtime } = client;
let errorThrown = "";
try {
await Runtime.callFunctionOn();
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("functionDeclaration: string value expected"));
await Assert.rejects(
Runtime.callFunctionOn(),
err => err.message.includes("functionDeclaration: string value expected"),
"functionDeclaration: string value expected"
);
});
add_task(async function functionDeclarationInvalidTypes({ client }) {
@ -23,13 +20,11 @@ add_task(async function functionDeclarationInvalidTypes({ client }) {
const { id: executionContextId } = await enableRuntime(client);
for (const functionDeclaration of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({ functionDeclaration, executionContextId });
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("functionDeclaration: string value expected"));
await Assert.rejects(
Runtime.callFunctionOn({ functionDeclaration, executionContextId }),
err => err.message.includes("functionDeclaration: string value expected"),
"functionDeclaration: string value expected"
);
}
});
@ -52,17 +47,15 @@ add_task(async function argumentsInvalidTypes({ client }) {
const { id: executionContextId } = await enableRuntime(client);
for (const args of [null, true, 1, "foo", {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
arguments: args,
executionContextId,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("arguments: array value expected"));
}),
err => err.message.includes("arguments: array value expected"),
"arguments: array value expected"
);
}
});
@ -72,35 +65,30 @@ add_task(async function argumentsPrimitiveTypes({ client }) {
const { id: executionContextId } = await enableRuntime(client);
for (const args of [null, true, 1, "foo", {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
arguments: args,
executionContextId,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("arguments: array value expected"));
}),
err => err.message.includes("arguments: array value expected"),
"arguments: array value expected"
);
}
});
add_task(async function executionContextIdNorObjectIdSpecified({ client }) {
const { Runtime } = client;
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.includes(
"Either objectId or executionContextId must be specified"
)
}),
err =>
err.message.includes(
"Either objectId or executionContextId must be specified"
),
"Either objectId or executionContextId must be specified"
);
});
@ -108,45 +96,38 @@ add_task(async function executionContextIdInvalidTypes({ client }) {
const { Runtime } = client;
for (const executionContextId of [null, true, "foo", [], {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
executionContextId,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("executionContextId: number value expected"));
}),
err => err.message.includes("executionContextId: number value expected"),
"executionContextId: number value expected"
);
}
});
add_task(async function executionContextIdInvalidValue({ client }) {
const { Runtime } = client;
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
executionContextId: -1,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("Cannot find context with specified id"));
}),
err => err.message.includes("Cannot find context with specified id"),
"Cannot find context with specified id"
);
});
add_task(async function objectIdInvalidTypes({ client }) {
const { Runtime } = client;
for (const objectId of [null, true, 1, [], {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({ functionDeclaration: "", objectId });
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("objectId: string value expected"));
await Assert.rejects(
Runtime.callFunctionOn({ functionDeclaration: "", objectId }),
err => err.message.includes("objectId: string value expected"),
"objectId: string value expected"
);
}
});

Просмотреть файл

@ -9,17 +9,15 @@ add_task(async function awaitPromiseInvalidTypes({ client }) {
const { id: executionContextId } = await enableRuntime(client);
for (const awaitPromise of [null, 1, "foo", [], {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
awaitPromise,
executionContextId,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("awaitPromise: boolean value expected"));
}),
err => err.message.includes("awaitPromise: boolean value expected"),
"awaitPromise: boolean value expected"
);
}
});

Просмотреть файл

@ -176,17 +176,15 @@ add_task(async function returnByValueInvalidTypes({ client }) {
const { id: executionContextId } = await enableRuntime(client);
for (const returnByValue of [null, 1, "foo", [], {}]) {
let errorThrown = "";
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration: "",
executionContextId,
returnByValue,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("returnByValue: boolean value expected"));
}),
err => err.message.includes("returnByValue: boolean value expected"),
"returnByValue: boolean value expected"
);
}
});
@ -201,17 +199,15 @@ add_task(async function returnByValueCyclicValue({ client }) {
];
for (const functionDeclaration of functionDeclarations) {
let errorThrown;
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration,
executionContextId,
returnByValue: true,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("Object reference chain is too long"));
}),
err => err.message.includes("Object reference chain is too long"),
"Object reference chain is too long"
);
}
});
@ -227,17 +223,15 @@ add_task(async function returnByValueNotPossible({ client }) {
];
for (const functionDeclaration of functionDeclarations) {
let errorThrown;
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
functionDeclaration,
executionContextId,
returnByValue: true,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("Object couldn't be returned by value"));
}),
err => err.message.includes("Object couldn't be returned by value"),
"Object couldn't be returned by value"
);
}
});

Просмотреть файл

@ -10,13 +10,11 @@ add_task(async function contextIdInvalidValue({ client }) {
await enableRuntime(client);
let errorThrown = "";
try {
await Runtime.evaluate({ expression: "", contextId: -1 });
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("Cannot find context with specified id"));
await Assert.rejects(
Runtime.evaluate({ expression: "", contextId: -1 }),
err => err.message.includes("Cannot find context with specified id"),
"Cannot find context with specified id"
);
});
add_task(async function contextIdNotSpecified({ client }) {

Просмотреть файл

@ -9,16 +9,14 @@ add_task(async function awaitPromiseInvalidTypes({ client }) {
await enableRuntime(client);
for (const awaitPromise of [null, 1, "foo", [], {}]) {
let errorThrown = "";
try {
await Runtime.evaluate({
await Assert.rejects(
Runtime.evaluate({
expression: "",
awaitPromise,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("awaitPromise: boolean value expected"));
}),
err => err.message.includes("awaitPromise: boolean value expected"),
"awaitPromise: boolean value expected"
);
}
});

Просмотреть файл

@ -9,16 +9,14 @@ add_task(async function returnByValueInvalidTypes({ client }) {
await enableRuntime(client);
for (const returnByValue of [null, 1, "foo", [], {}]) {
let errorThrown = "";
try {
await Runtime.evaluate({
await Assert.rejects(
Runtime.evaluate({
expression: "",
returnByValue,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("returnByValue: boolean value expected"));
}),
err => err.message.includes("returnByValue: boolean value expected"),
"returnByValue: boolean value expected"
);
}
});
@ -30,16 +28,14 @@ add_task(async function returnByValueCyclicValue({ client }) {
const expressions = ["const b = { a: 1}; b.b = b; b", "window"];
for (const expression of expressions) {
let errorThrown;
try {
await Runtime.evaluate({
await Assert.rejects(
Runtime.evaluate({
expression,
returnByValue: true,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("Object reference chain is too long"));
}),
err => err.message.includes("Object reference chain is too long"),
"Object reference chain is too long"
);
}
});
@ -51,16 +47,14 @@ add_task(async function returnByValueNotPossible({ client }) {
const expressions = ["Symbol(42)", "[Symbol(42)]", "{a: Symbol(42)}"];
for (const expression of expressions) {
let errorThrown;
try {
await Runtime.evaluate({
await Assert.rejects(
Runtime.evaluate({
expression,
returnByValue: true,
});
} catch (e) {
errorThrown = e.message;
}
ok(errorThrown.includes("Object couldn't be returned by value"));
}),
err => err.message.includes("Object couldn't be returned by value"),
"Object couldn't be returned by value"
);
}
});

Просмотреть файл

@ -55,29 +55,22 @@ async function testObjectRelease({ Runtime }, contextId) {
});
info("Object is released");
try {
await Runtime.callFunctionOn({
await Assert.rejects(
Runtime.callFunctionOn({
executionContextId: contextId,
functionDeclaration: "() => {}",
arguments: [{ objectId: result.objectId }],
});
ok(false, "callFunctionOn with a released object as argument should throw");
} catch (e) {
ok(
e.message.includes("Could not find object with given id"),
"callFunctionOn throws on released argument"
);
}
try {
await Runtime.callFunctionOn({
}),
err => err.message.includes("Could not find object with given id"),
"callFunctionOn throws on released argument"
);
await Assert.rejects(
Runtime.callFunctionOn({
objectId: result.objectId,
functionDeclaration: "() => {}",
});
ok(false, "callFunctionOn with a released object as target should throw");
} catch (e) {
ok(
e.message.includes("Cannot find context with specified id"),
"callFunctionOn throws on released target"
);
}
}),
err => err.message.includes("Cannot find context with specified id"),
"callFunctionOn throws on released target"
);
}

Просмотреть файл

@ -5,24 +5,22 @@
add_task(async function raisesWithoutArguments({ client, tab }) {
const { Target } = client;
let errorThrown = false;
try {
await Target.activateTarget();
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "activateTarget raised error without an argument");
await Assert.rejects(
Target.activateTarget(),
err => err.message.includes(`Unable to find target with id`),
"activateTarget raised error without an argument"
);
});
add_task(async function raisesWithUnknownTargetId({ client, tab }) {
const { Target } = client;
let errorThrown = false;
try {
await Target.activateTarget({ targetId: "-1" });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "activateTarget raised error with unkown target id");
await Assert.rejects(
Target.activateTarget({ targetId: "-1" }),
err => err.message.includes(`Unable to find target with id`),
"activateTarget raised error with unkown target id"
);
});
add_task(async function selectTabInOtherWindow({ client, tab }) {

Просмотреть файл

@ -5,24 +5,22 @@
add_task(async function raisesWithoutArguments({ client, tab }) {
const { Target } = client;
let errorThrown = false;
try {
await Target.attachToTarget();
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "attachToTarget raised error without an argument");
await Assert.rejects(
Target.attachToTarget(),
err => err.message.includes(`Unable to find target with id`),
"attachToTarget raised error without an argument"
);
});
add_task(async function raisesWithUnknownTargetId({ client, tab }) {
const { Target } = client;
let errorThrown = false;
try {
await Target.attachToTarget({ targetId: "-1" });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "attachToTarget raised error with unkown target id");
await Assert.rejects(
Target.attachToTarget({ targetId: "-1" }),
err => err.message.includes(`Unable to find target with id`),
"attachToTarget raised error with unkown target id"
);
});
add_task(

Просмотреть файл

@ -5,24 +5,22 @@
add_task(async function raisesWithoutArguments({ client, tab }) {
const { Target } = client;
let exceptionThrown = false;
try {
await Target.closeTarget();
} catch (e) {
exceptionThrown = true;
}
ok(exceptionThrown, "closeTarget raised error without an argument");
await Assert.rejects(
Target.closeTarget(),
err => err.message.includes(`Unable to find target with id `),
"closeTarget raised error without an argument"
);
});
add_task(async function raisesWithUnknownTargetId({ client, tab }) {
const { Target } = client;
let exceptionThrown = false;
try {
await Target.closeTarget({ targetId: "-1" });
} catch (e) {
exceptionThrown = true;
}
ok(exceptionThrown, "closeTarget raised error with unkown target id");
await Assert.rejects(
Target.closeTarget({ targetId: "-1" }),
err => err.message.includes(`Unable to find target with id `),
"closeTarget raised error with unkown target id"
);
});
add_task(async function triggersTargetDestroyed({ client, tab }) {

Просмотреть файл

@ -10,13 +10,11 @@ add_task(
async function raisesWithoutArguments({ client }) {
const { Target } = client;
let exceptionThrown = false;
try {
await Target.createTarget();
} catch (e) {
exceptionThrown = true;
}
ok(exceptionThrown, "createTarget raised error without a URL");
await Assert.rejects(
Target.createTarget(),
err => err.message.includes("url: string value expected"),
"createTarget raised error without a URL"
);
},
{ createTab: false }
);
@ -28,17 +26,11 @@ add_task(
for (const url of [null, true, 1, [], {}]) {
info(`Checking url with invalid value: ${url}`);
let errorThrown = "";
try {
await Target.createTarget({
await Assert.rejects(
Target.createTarget({
url,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/url: string value expected/),
}),
/url: string value expected/,
`URL fails for invalid type: ${url}`
);
}

Просмотреть файл

@ -225,17 +225,11 @@ add_task(
for (const filter of [null, true, 1, "foo", {}]) {
info(`Checking filter with invalid value: ${filter}`);
let errorThrown = "";
try {
await Target.getTargets({
await Assert.rejects(
Target.getTargets({
filter,
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: array value expected/),
}),
/filter: array value expected/,
`Filter fails for invalid type: ${filter}`
);
}
@ -243,17 +237,11 @@ add_task(
for (const filterEntry of [null, true, 1, "foo", []]) {
info(`Checking filter entry with invalid value: ${filterEntry}`);
let errorThrown = "";
try {
await Target.getTargets({
await Assert.rejects(
Target.getTargets({
filter: [filterEntry],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: object values expected in array/),
}),
/filter: object values expected in array/,
`Filter entry fails for invalid type: ${filterEntry}`
);
}
@ -261,17 +249,11 @@ add_task(
for (const type of [null, true, 1, [], {}]) {
info(`Checking filter entry with type as invalid value: ${type}`);
let errorThrown = "";
try {
await Target.getTargets({
await Assert.rejects(
Target.getTargets({
filter: [{ type }],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: type: string value expected/),
}),
/filter: type: string value expected/,
`Filter entry type fails for invalid type: ${type}`
);
}
@ -279,17 +261,11 @@ add_task(
for (const exclude of [null, 1, "foo", [], {}]) {
info(`Checking filter entry with exclude as invalid value: ${exclude}`);
let errorThrown = "";
try {
await Target.getTargets({
await Assert.rejects(
Target.getTargets({
filter: [{ exclude }],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: exclude: boolean value expected/),
}),
/filter: exclude: boolean value expected/,
`Filter entry exclude for invalid type: ${exclude}`
);
}

Просмотреть файл

@ -19,15 +19,9 @@ add_task(
for (const discover of [null, undefined, 1, "foo", [], {}]) {
info(`Checking discover with invalid value: ${discover}`);
let errorThrown = "";
try {
await Target.setDiscoverTargets({ discover });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/discover: boolean value expected/),
await Assert.rejects(
Target.setDiscoverTargets({ discover }),
/discover: boolean value expected/,
`Discover fails for invalid type: ${discover}`
);
}
@ -42,15 +36,9 @@ add_task(
for (const filter of [null, true, 1, "foo", {}]) {
info(`Checking filter with invalid value: ${filter}`);
let errorThrown = "";
try {
await Target.setDiscoverTargets({ discover: true, filter });
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: array value expected/),
await Assert.rejects(
Target.setDiscoverTargets({ discover: true, filter }),
/filter: array value expected/,
`Filter fails for invalid type: ${filter}`
);
}
@ -58,18 +46,12 @@ add_task(
for (const filterEntry of [null, undefined, true, 1, "foo", []]) {
info(`Checking filter entry with invalid value: ${filterEntry}`);
let errorThrown = "";
try {
await Target.setDiscoverTargets({
await Assert.rejects(
Target.setDiscoverTargets({
discover: true,
filter: [filterEntry],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: object values expected in array/),
}),
/filter: object values expected in array/,
`Filter entry fails for invalid type: ${filterEntry}`
);
}
@ -77,18 +59,12 @@ add_task(
for (const type of [null, true, 1, [], {}]) {
info(`Checking filter entry with type as invalid value: ${type}`);
let errorThrown = "";
try {
await Target.setDiscoverTargets({
await Assert.rejects(
Target.setDiscoverTargets({
discover: true,
filter: [{ type }],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: type: string value expected/),
}),
/filter: type: string value expected/,
`Filter entry type fails for invalid type: ${type}`
);
}
@ -96,18 +72,12 @@ add_task(
for (const exclude of [null, 1, "foo", [], {}]) {
info(`Checking filter entry with exclude as invalid value: ${exclude}`);
let errorThrown = "";
try {
await Target.setDiscoverTargets({
await Assert.rejects(
Target.setDiscoverTargets({
discover: true,
filter: [{ exclude }],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: exclude: boolean value expected/),
}),
/filter: exclude: boolean value expected/,
`Filter entry exclude for invalid type: ${exclude}`
);
}
@ -120,18 +90,13 @@ add_task(
const { Target } = client;
// Check filter cannot be given with discover: false
let errorThrown = "";
try {
await Target.setDiscoverTargets({
await Assert.rejects(
Target.setDiscoverTargets({
discover: false,
filter: [{}],
});
} catch (e) {
errorThrown = e.message;
}
ok(
errorThrown.match(/filter: should not be present when discover is false/),
}),
/filter: should not be present when discover is false/,
`Error throw when given filter with discover false`
);
},

Просмотреть файл

@ -15,19 +15,19 @@ add_task(async function test_module_error() {
const rootMessageHandler = createRootMessageHandler("session-id-error");
info("Call a module method which will throw");
try {
await rootMessageHandler.handleCommand({
await Assert.rejects(
rootMessageHandler.handleCommand({
moduleName: "commandwindowglobalonly",
commandName: "testError",
destination: {
type: WindowGlobalMessageHandler.type,
id: browsingContextId,
},
});
ok(false, "Error from window global module was not caught");
} catch (e) {
ok(true, "Error from window global module caught");
}
}),
err => err.message.includes("error-from-module"),
"Error from window global module caught"
);
rootMessageHandler.destroy();
});

Просмотреть файл

@ -42,16 +42,11 @@ add_task(async function test_no_retry() {
// Reloading the tab will reject the pending query with an AbortError.
await BrowserTestUtils.reloadTab(tab);
try {
await onBlockedOneTime;
ok("false", "onBlockedOneTime should not have resolved");
} catch (e) {
is(
e.name,
"AbortError",
"Caught the expected abort error when reloading"
);
}
await Assert.rejects(
onBlockedOneTime,
e => e.name == "AbortError",
"Caught the expected abort error when reloading"
);
} finally {
await cleanup(rootMessageHandler, tab);
}
@ -146,19 +141,14 @@ add_task(async function test_retry() {
info("Reload one more time");
await BrowserTestUtils.reloadTab(tab);
try {
info(
"The call to blockedElevenTimes now exceeds the maximum attempts allowed"
);
await onBlockedElevenTimes;
ok("false", "blockedElevenTimes should not have resolved");
} catch (e) {
is(
e.name,
"AbortError",
"Caught the expected abort error when reloading"
);
}
info(
"The call to blockedElevenTimes now exceeds the maximum attempts allowed"
);
await Assert.rejects(
onBlockedElevenTimes,
e => e.name == "AbortError",
"Caught the expected abort error when reloading"
);
} finally {
await cleanup(rootMessageHandler, tab);
}