Skip to content

Commit 541b5af

Browse files
committed
fix(provider-webdriver): a 404 naming another W3C error is not an unsupported route
Only the W3C unknown command/method codes, or a bare 404/405 without a W3C error code, switch activateApp/terminateApp to the sibling route and report dispatched: no. An invalid-session 404 stays unclassified.
1 parent 5b3e61c commit 541b5af

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎packages/provider-webdriver/src/webdriver-mutation-resend.test.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ afterEach(() => {
2222
function hangingWebDriverFetch(
2323
hangOnPathSuffix: string,
2424
unsupportedPathSuffix?: string,
25+
unsupportedErrorCode = 'unknown command',
2526
): {
2627
fetch: typeof globalThis.fetch;
2728
callsFor: (pathSuffix: string) => number;
@@ -38,7 +39,7 @@ function hangingWebDriverFetch(
3839
}
3940
if (unsupportedPathSuffix && url.pathname.endsWith(unsupportedPathSuffix)) {
4041
return new Response(
41-
JSON.stringify({ value: { error: 'unknown command', message: 'no such route' } }),
42+
JSON.stringify({ value: { error: unsupportedErrorCode, message: 'refused' } }),
4243
{ status: 404, headers: { 'Content-Type': 'application/json' } },
4344
);
4445
}
@@ -65,8 +66,13 @@ function hangingWebDriverFetch(
6566
async function connectedWebDriverInteractor(
6667
hangOnPathSuffix: string,
6768
unsupportedPathSuffix?: string,
69+
unsupportedErrorCode?: string,
6870
) {
69-
const { fetch, callsFor } = hangingWebDriverFetch(hangOnPathSuffix, unsupportedPathSuffix);
71+
const { fetch, callsFor } = hangingWebDriverFetch(
72+
hangOnPathSuffix,
73+
unsupportedPathSuffix,
74+
unsupportedErrorCode,
75+
);
7076
globalThis.fetch = fetch;
7177
const client = new WebDriverClient({
7278
clientVersion: '0.0.0-test',
@@ -169,3 +175,22 @@ test('an unsupported app-termination route falls back to the sibling route once'
169175
assert.equal(callsFor('/appium/device/terminate_app'), 1);
170176
assert.equal(callsFor('/execute/sync'), 1);
171177
});
178+
179+
// W3C also answers 404 for a session that no longer exists. That is not an unsupported route, so
180+
// the sibling route is not tried and the failure is not classified as never dispatched here.
181+
test('a 404 naming another W3C error does not fall back to the sibling route', async () => {
182+
const { client, callsFor } = await connectedWebDriverInteractor(
183+
'/never-hangs',
184+
'/appium/device/terminate_app',
185+
'invalid session id',
186+
);
187+
188+
await assert.rejects(client.terminateApp('com.example.app'), (error: unknown) => {
189+
assert.ok(error instanceof AppError);
190+
assert.equal(error.details?.dispatched, undefined);
191+
return true;
192+
});
193+
194+
assert.equal(callsFor('/appium/device/terminate_app'), 1);
195+
assert.equal(callsFor('/execute/sync'), 0);
196+
});

‎packages/provider-webdriver/src/webdriver-transport.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,20 @@ const UNSUPPORTED_ROUTE_ERROR_CODES: ReadonlySet<string> = new Set([
279279
'unknown method',
280280
]);
281281

282+
/**
283+
* A W3C `unknown command`/`unknown method` code, or a bare 404/405 that carries no W3C error code
284+
* (a server that does not know the path). A 404 that names another W3C code, such as
285+
* `invalid session id` or `no such element`, is a different answer and never counts.
286+
*/
282287
function isUnsupportedRouteAnswer(status: number, payload: unknown): boolean {
283-
if (status === 404 || status === 405) return true;
284288
const value =
285289
payload && typeof payload === 'object' && 'value' in payload
286290
? (payload as { value?: unknown }).value
287291
: undefined;
288292
const code =
289293
value && typeof value === 'object' ? (value as { error?: unknown }).error : undefined;
290-
return typeof code === 'string' && UNSUPPORTED_ROUTE_ERROR_CODES.has(code);
294+
if (typeof code === 'string') return UNSUPPORTED_ROUTE_ERROR_CODES.has(code);
295+
return status === 404 || status === 405;
291296
}
292297

293298
/**

0 commit comments

Comments
 (0)