fix(preview): make in-app browser automation work on real app pages #7127
Closedgb1095(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/7127/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/preview/previewAutomationErrors.ts lines 254 to 261OutdatedResolvedby gb1095(GitHub profile, opens in a new tab)@@ -183,3 +249,13 @@183249 input: PreviewAutomationOperationContext & { readonly cause: unknown },184250 ): PreviewAutomationHostError {185251 if (isPreviewAutomationHostError(input.cause)) return input.cause;252+ const notFound = targetNotFoundDiagnostics(input.cause);253+ if (notFound) {254+ return new PreviewAutomationTargetNotFoundHostError({255+ requestId: input.requestId,256+ operation: input.operation,257+ environmentId: input.environmentId,258+ threadId: input.threadId,259+ tabId: input.tabId,260+ ...notFound,261+ });apps/desktop/src/preview/Manager.ts lines 3764 to 3773OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3655,7 +3753,21 @@36553753 tabId: Schema.String,36563754 selectorKind: PreviewAutomationSelectorKind,36573755 selectorLength: Schema.optionalKey(Schema.Number),3756+ failureKind: Schema.optionalKey(3757+ Schema.Literals(["missing", "hidden", "disabled", "ambiguous"]),3758+ ),3759+ matchCount: Schema.optionalKey(Schema.Number),36583760 },36593761 ) {36603762 override get message(): string {36613763 const target = previewAutomationTargetLabel(this.selectorKind, this.selectorLength);3764+ if (this.failureKind === "hidden") {3765+ return `Preview automation ${this.operation} found ${target} in tab ${this.tabId}, but it is not visible`;3766+ }3767+ if (this.failureKind === "disabled") {3768+ return `Preview automation ${this.operation} found ${target} in tab ${this.tabId}, but it is disabled`;3769+ }3770+ if (this.failureKind === "ambiguous") {3771+ const count = this.matchCount ?? 0;3772+ return `Preview automation ${this.operation} matched ${count} elements for ${target} in tab ${this.tabId}`;3773+ }apps/web/src/components/preview/previewAutomationErrors.ts lines 190 to 198OutdatedResolvedby gb1095(GitHub profile, opens in a new tab)@@ -136,0 +178,21 @@178+ threadId: ThreadId,179+ tabId: Schema.NullOr(PreviewTabId),180+ failureKind: Schema.optional(Schema.Literals(["missing", "hidden", "disabled", "ambiguous"])),181+ matchCount: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),182+ },183+) {184+ get responseTag() {185+ return "PreviewAutomationExecutionError" as const;186+ }187+188+ override get message(): string {189+ const tab = this.tabId ?? "unassigned";190+ if (this.failureKind === "hidden") {191+ return `Preview automation ${this.operation} request ${this.requestId} found a target in tab ${tab}, but it is not visible.`;192+ }193+ if (this.failureKind === "disabled") {194+ return `Preview automation ${this.operation} request ${this.requestId} found a target in tab ${tab}, but it is disabled.`;195+ }196+ if (this.failureKind === "ambiguous") {197+ return `Preview automation ${this.operation} request ${this.requestId} matched ${this.matchCount ?? 0} elements in tab ${tab}.`;198+ }
- apps/desktop/src/preview/Manager.ts line 3489Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3398,4 +3474,16 @@33983474 send,33993475 `(() => {34003476 try {3401- const selectorMatched = ${locatorJson ? `(() => { const injected = globalThis.__t3PlaywrightInjected; return injected.querySelector(injected.parseSelector(${locatorJson}), document, false) !== null; })()` : "true"};3477+ const root = ${scopeJson} === "document"3478+ ? document.documentElement3479+ : (document.querySelector("main") || document.documentElement);3480+ const selectorMatched = ${3481+ locatorJson3482+ ? `(() => {3483+ const injected = globalThis.__t3PlaywrightInjected;3484+ const parsed = injected.parseSelector(${locatorJson});3485+ const element = injected.querySelector(parsed, root, false);3486+ if (!element) return false;3487+ const visible = injected.elementState(element, "visible");3488+ if (!visible.matches) return false;3489+ if (element.getAttribute("role") === "dialog") {apps/web/src/components/preview/PreviewAutomationHosts.tsx line 538OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -515,3 +534,5 @@515534 previousSetting,516535 serverEpoch: operationState.serverEpoch,517536 };537+ };538+ const applied = await runBrowserViewportMutation(packages/contracts/src/ipc.ts lines 965 to 970OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -956,3 +957,14 @@956957 tabId: DesktopPreviewTabIdSchema,957958 });958959960+export const DesktopPreviewAutomationSnapshotInputSchema = Schema.Struct({961+ tabId: DesktopPreviewTabIdSchema,962+ include: Schema.optional(Schema.Array(PreviewAutomationSnapshotInclude)),963+});964+965+export const DesktopPreviewAutomationSetViewportInputSchema = Schema.Struct({966+ tabId: DesktopPreviewTabIdSchema,967+ width: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),968+ height: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),969+ clear: Schema.optional(Schema.Boolean),970+});apps/web/src/components/preview/PreviewAutomationHosts.tsx line 502OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -490,4 +499,4 @@490499 const ready = await requireReadyTab();491500 const input = request.input as PreviewAutomationResizeInput;492501 const setting = resolvePreviewViewport(input);493- const applied = await runBrowserViewportMutation(ready.runtimeTabId, async () => {502+ if (ready.bridge.automation.setViewport) {
- apps/web/src/components/preview/PreviewAutomationHosts.tsx line 580Resolvedby gb1095(GitHub profile, opens in a new tab)@@ -577,4 +603,3 @@577603 }578604 case "snapshot": {579605 const ready = await requireReadyTab();580- return await ready.bridge.automation.snapshot(ready.runtimeTabId);apps/web/src/components/preview/PreviewAutomationHosts.tsx line 510OutdatedResolvedby gb1095(GitHub profile, opens in a new tab)@@ -490,4 +499,12 @@490499 const ready = await requireReadyTab();491500 const input = request.input as PreviewAutomationResizeInput;492501 const setting = resolvePreviewViewport(input);493- const applied = await runBrowserViewportMutation(ready.runtimeTabId, async () => {502+ if (ready.bridge.automation.setViewport) {503+ if (setting._tag === "fill") {504+ await ready.bridge.automation.setViewport(ready.runtimeTabId, { clear: true });505+ } else {506+ await ready.bridge.automation.setViewport(ready.runtimeTabId, {507+ width: setting.width,508+ height: setting.height,509+ });510+ }
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
gb1095(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
7f4b4792-75db-4d03-844b-619d0055471bYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share