diff --git a/__e2e__/__image_snapshots__/android/hcaptcha-dark.png b/__e2e__/__image_snapshots__/android/hcaptcha-dark.png index e9ee695..19a1aea 100644 Binary files a/__e2e__/__image_snapshots__/android/hcaptcha-dark.png and b/__e2e__/__image_snapshots__/android/hcaptcha-dark.png differ diff --git a/__e2e__/__image_snapshots__/android/hcaptcha-light.png b/__e2e__/__image_snapshots__/android/hcaptcha-light.png index 058f682..63d750d 100644 Binary files a/__e2e__/__image_snapshots__/android/hcaptcha-light.png and b/__e2e__/__image_snapshots__/android/hcaptcha-light.png differ diff --git a/__e2e__/__image_snapshots__/ios/hcaptcha-dark.png b/__e2e__/__image_snapshots__/ios/hcaptcha-dark.png index 6f59821..16b4e5d 100644 Binary files a/__e2e__/__image_snapshots__/ios/hcaptcha-dark.png and b/__e2e__/__image_snapshots__/ios/hcaptcha-dark.png differ diff --git a/__e2e__/__image_snapshots__/ios/hcaptcha-light.png b/__e2e__/__image_snapshots__/ios/hcaptcha-light.png index 21fc5cb..679dfa3 100644 Binary files a/__e2e__/__image_snapshots__/ios/hcaptcha-light.png and b/__e2e__/__image_snapshots__/ios/hcaptcha-light.png differ diff --git a/__e2e__/darkTheme.harness.js b/__e2e__/darkTheme.harness.js index 1c74f56..a86ffe1 100644 --- a/__e2e__/darkTheme.harness.js +++ b/__e2e__/darkTheme.harness.js @@ -1,4 +1,4 @@ -import { describe, test, render, expect } from 'react-native-harness'; +import { describe, test, render, expect, waitFor } from 'react-native-harness'; import { screen } from '@react-native-harness/ui'; import React from 'react'; import { StyleSheet, View } from 'react-native'; @@ -8,23 +8,112 @@ import Hcaptcha from '@hcaptcha/react-native-hcaptcha/Hcaptcha.js'; const siteKey = '10000000-ffff-ffff-ffff-000000000001'; const baseUrl = 'https://hcaptcha.com'; -const WEBVIEW_LOAD_MS = 10000; +// Screenshots are cropped to this view. On a full-screen shot the widget is ~11% of the +// pixels and its light card is close enough to white that removing the widget entirely +// changed only ~0.6% of the screen - under the threshold below, so a missing widget passed. +const WIDGET_TEST_ID = 'hcaptcha-widget-frame'; -const WidgetFixture = ({ theme }) => ( +// `render` resolves once React has committed and native has processed the view-creation +// commands; it does not wait for the WebView to fetch anything, so the widget is still blank +// at that point. Readiness is waited for separately, below. +const MOUNT_MS = 10000; + +// Above the SDK's own 15s loading timeout on purpose: when the page never comes up we want the +// SDK to report `loading timeout` through onMessage and fail this test with that reason, +// rather than the harness killing the test first and leaving only "exceeded timeout". +const READY_MS = 20000; +const POLL_MS = 250; + +// The token arrives with the checkmark, but the checkmark animates in. Rather than guess a +// settle delay, poll until two consecutive frames are identical. Best-effort: if the pixels +// never settle we screenshot anyway and let the diff decide, which keeps a never-quiet widget +// from turning into an unexplained timeout. +const PAINT_SETTLE_MS = 5000; + +// `failureThresholdType: 'percent'` compares `differences / totalPixels` - a 0-1 ratio - so +// this is a fraction, not a percentage: 0.01 is 1% of the pixels. The previous value of 5 +// meant 500%, which no image pair can exceed, so neither assertion below could ever fail. +// Measured on the full-screen baselines, a missing widget is ~0.6% of the screen and the +// wrong theme ~6%; cropped to the widget both are eight to nine times that, so 1% leaves room +// for antialiasing while catching either. +const FAILURE_THRESHOLD = 0.01; + +const WidgetFixture = ({ theme, onMessage }) => ( - + {}} + showLoading + onMessage={onMessage} /> ); +const sameBytes = (a, b) => + a != null && + b != null && + a.length === b.length && + a.every((byte, index) => byte === b[index]); + +const screenshotWidget = async () => + screen.screenshot(await screen.findByTestId(WIDGET_TEST_ID)); + +const waitForStablePaint = async () => { + const deadline = Date.now() + PAINT_SETTLE_MS; + let previous = (await screenshotWidget())?.data; + + while (Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, POLL_MS)); + const current = (await screenshotWidget())?.data; + if (sameBytes(previous, current)) { + return; + } + previous = current; + } +}; + +// The SDK forwards a successful message longer than 35 characters as a token. +const isToken = (m) => m.success === true && m.nativeEvent.data.length > 35; + +/** + * Renders the widget and blocks until it has finished verifying. + * + * The test sitekey passes without a challenge, so the widget's final state - the checked + * checkbox the baselines show - arrives together with the token. Waiting on anything earlier + * is not enough: the SDK's internal ready event only means `hcaptcha.render()` returned, and + * the iframe can still be blank on the network after it. Collected messages are also checked + * for errors on every poll, so a failure ends the test with the SDK's own description instead + * of diffing a screenshot of a spinner or a blank frame. + */ +const renderWidget = async (theme) => { + const messages = []; + + await render( messages.push(e)} />, { + timeout: MOUNT_MS, + }); + + await waitFor( + () => { + const failure = messages.find((m) => m.success === false); + if (failure) { + const { description, data } = failure.nativeEvent; + throw new Error(`hCaptcha reported: ${description || data}`); + } + if (!messages.some(isToken)) { + throw new Error('hCaptcha has not issued a token yet'); + } + }, + { timeout: READY_MS, interval: POLL_MS }, + ); + + await waitForStablePaint(); +}; + const styles = StyleSheet.create({ container: { flex: 1, @@ -49,27 +138,25 @@ const styles = StyleSheet.create({ describe('hCaptcha theme rendering', () => { test('light widget matches baseline', async () => { - await render(, { timeout: WEBVIEW_LOAD_MS }); - await new Promise((r) => setTimeout(r, WEBVIEW_LOAD_MS)); + await renderWidget('light'); - const screenshot = await screen.screenshot(); + const screenshot = await screenshotWidget(); await expect(screenshot).toMatchImageSnapshot({ name: 'hcaptcha-light', threshold: 0.15, - failureThreshold: 5, + failureThreshold: FAILURE_THRESHOLD, failureThresholdType: 'percent', }); }); test('dark widget matches baseline', async () => { - await render(, { timeout: WEBVIEW_LOAD_MS }); - await new Promise((r) => setTimeout(r, WEBVIEW_LOAD_MS)); + await renderWidget('dark'); - const screenshot = await screen.screenshot(); + const screenshot = await screenshotWidget(); await expect(screenshot).toMatchImageSnapshot({ name: 'hcaptcha-dark', threshold: 0.15, - failureThreshold: 5, + failureThreshold: FAILURE_THRESHOLD, failureThresholdType: 'percent', }); }); diff --git a/__e2e__/rn-harness.config.mjs b/__e2e__/rn-harness.config.mjs index 92e53b2..2b2335d 100644 --- a/__e2e__/rn-harness.config.mjs +++ b/__e2e__/rn-harness.config.mjs @@ -13,6 +13,12 @@ const config = { disableViewFlattening: true, bridgeTimeout: Number(process.env.HARNESS_BRIDGE_TIMEOUT || 120000), + // A backstop, not the mechanism: each case waits on the widget's own readiness signal and + // returns as soon as it arrives, so a healthy run finishes well inside this. + // + // This has to live here rather than as Jest's `testTimeout`: the harness reads + // `session.config.testTimeout` first and the schema defaults it to 5000, so it is never + // undefined and a value in jest.harness.config.mjs is never consulted. testTimeout: Number(process.env.HARNESS_TEST_TIMEOUT || 120000), forwardClientLogs: true, diff --git a/__scripts__/setup-e2e-host.js b/__scripts__/setup-e2e-host.js index beeeaf8..66833d0 100644 --- a/__scripts__/setup-e2e-host.js +++ b/__scripts__/setup-e2e-host.js @@ -34,12 +34,17 @@ if (fs.existsSync(hostTestsDir)) { // 2. Install react-native-harness (must happen BEFORE native build so // HarnessUI TurboModule is compiled into the binary) console.log('Installing react-native-harness in host app...'); +// Pinned deliberately. The host app is regenerated from scratch on every CI run, so an +// unpinned range resolves to whatever is newest at that moment and upstream defaults land +// without a commit here: 1.0.0 had no per-test timeout at all, and the `testTimeout: 5000` +// default introduced later turned the e2e suite red on its own. Bump this on purpose. +const HARNESS_VERSION = '1.6.0'; const harnessPkgs = [ 'react-native-harness', '@react-native-harness/platform-android', '@react-native-harness/platform-apple', '@react-native-harness/ui', -].join(' '); +].map((name) => `${name}@${HARNESS_VERSION}`).join(' '); if (pm === 'yarn') { execSync(`yarn add --dev ${harnessPkgs}`, { stdio: 'inherit', cwd: HOST_DIR });