From eef00117adac9c705e9ef8084e4e62b818709159 Mon Sep 17 00:00:00 2001 From: Ben Sheridan-Edwards Date: Tue, 15 Sep 2026 15:00:48 +0100 Subject: [PATCH] fix(driver): reject unavailable page source --- src/driver.ts | 10 ++----- test/driver.test.ts | 67 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/driver.ts b/src/driver.ts index 4e7c955..0ea4066 100644 --- a/src/driver.ts +++ b/src/driver.ts @@ -14,7 +14,7 @@ export type Platform = "android" | "ios"; export interface Driver { /** The platform under test — selects how a cross-platform selector maps to source. */ readonly platform: Platform; - /** Current page-source XML (best-effort; empty string on driver error). */ + /** Current page-source XML; rejects when the driver cannot read the source. */ source(): Promise; /** Idle for the given milliseconds; also used as the poll interval by the waits. */ pause(ms: number): Promise; @@ -55,13 +55,7 @@ export function wdioDriver(): Driver { get platform(): Platform { return browser.isAndroid ? "android" : "ios"; }, - source: () => - browser.getPageSource().catch((err: unknown) => { - // Don't let a dead/unreachable session masquerade as "element not visible"; - // surface it so a timeout's cause is visible, then degrade to empty source. - console.warn(`[nativeproof] getPageSource failed: ${err}`); - return ""; - }), + source: () => browser.getPageSource(), pause: (ms: number) => browser.pause(ms), tapAt: (x: number, y: number) => tapAt(x, y), pressAt: async (x: number, y: number, options = {}) => { diff --git a/test/driver.test.ts b/test/driver.test.ts index d81f247..4474857 100644 --- a/test/driver.test.ts +++ b/test/driver.test.ts @@ -3,6 +3,8 @@ import { afterEach, test } from "node:test"; import { _setGlobal } from "@wdio/globals"; import * as driverModule from "../src/driver.js"; import { iosExactNodeXPath, iosNodeCanUseNativeClick, wdioDriver } from "../src/driver.js"; +import { expect } from "../src/expect.js"; +import { by, Locator } from "../src/locator.js"; const ELEMENT_ID = "element-6066-11e4-a52e-4f735466cecf"; @@ -229,3 +231,68 @@ test("exactNodeXPath must not read placeholderValue as the value predicate", () assert.match(selector, /@value='abc'/); assert.doesNotMatch(selector, /Enter ID/); }); + +for (const platform of ["ios", "android"] as const) { + test(`wdioDriver ${platform} source failures cannot satisfy positive or negative assertions`, async () => { + const browser = fakeBrowser({ isAndroid: platform === "android" }); + browser.getPageSource = async () => { + throw new Error(`${platform} source unavailable`); + }; + _setGlobal("browser", browser, false); + const ready = new Locator(wdioDriver(), by.text("Ready")); + + await assert.rejects( + () => expect(ready).toBeVisible({ timeout: 0 }), + new RegExp(`${platform} source unavailable`), + ); + await assert.rejects( + () => expect(ready).not.toBeVisible({ timeout: 0 }), + new RegExp(`${platform} source unavailable`), + ); + }); + + test(`wdioDriver ${platform} assertions recover before deciding presence or absence`, async () => { + const present = + platform === "ios" + ? '' + : ''; + const browser = fakeBrowser({ isAndroid: platform === "android" }); + let reads = 0; + browser.getPageSource = async () => { + reads += 1; + if (reads === 1) throw new Error(`${platform} transient source failure`); + return present; + }; + _setGlobal("browser", browser, false); + const ready = new Locator(wdioDriver(), by.text("Ready")); + + await expect(ready).toBeVisible({ timeout: 50, interval: 1 }); + assert.equal(reads, 2); + + reads = 0; + await assert.rejects( + () => expect(ready).not.toBeVisible({ timeout: 5, interval: 1 }), + /assertion not met/, + ); + assert.ok(reads >= 2, `expected recovery read on ${platform}, got ${reads}`); + + reads = 0; + browser.getPageSource = async () => { + reads += 1; + if (reads === 1) throw new Error(`${platform} transient source failure`); + return ""; + }; + await expect(ready).not.toBeVisible({ timeout: 50, interval: 1 }); + assert.equal(reads, 2); + }); + + test(`wdioDriver ${platform} distinguishes legitimate absence from source unavailability`, async () => { + const browser = fakeBrowser({ isAndroid: platform === "android" }); + browser.getPageSource = async () => ""; + _setGlobal("browser", browser, false); + const ready = new Locator(wdioDriver(), by.text("Ready")); + + await expect(ready).not.toBeVisible({ timeout: 0 }); + await assert.rejects(() => expect(ready).toBeVisible({ timeout: 0 }), /assertion not met/); + }); +}