Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat: add live view link in credential testing UI (SKY-8329) (#5175)
Celal Zamanoğluccb253ac29d8
diff --git a/skyvern-frontend/src/routes/credentials/CredentialItem.tsx b/skyvern-frontend/src/routes/credentials/CredentialItem.tsxindex cc77a63b9..eba1972e1 100644--- a/skyvern-frontend/src/routes/credentials/CredentialItem.tsx+++ b/skyvern-frontend/src/routes/credentials/CredentialItem.tsx@@ -5,7 +5,11 @@ import {isSecretCredential,} from "@/api/types";import { useState } from "react";-import { Pencil1Icon } from "@radix-ui/react-icons";+import {+ ExternalLinkIcon,+ Pencil1Icon,+ ReloadIcon,+} from "@radix-ui/react-icons";import { Button } from "@/components/ui/button";import {Tooltip,@@ -18,6 +22,7 @@ import { getHostname } from "@/util/getHostname";import { CredentialsModal } from "./CredentialsModal";import { credentialTypeToModalType } from "./useCredentialModalState";import { SaveIcon } from "@/components/icons/SaveIcon";+import { useCredentialTestStore } from "@/store/useCredentialTestStore";type Props = {credential: CredentialApiResponse;@@ -30,6 +35,11 @@ type Props = {function CredentialItem({ credential, onStartBackgroundTest }: Props) {const [editModalOpen, setEditModalOpen] = useState(false);+ const activeTest = useCredentialTestStore((s) =>+ s.activeTest?.credentialId === credential.credential_id+ ? s.activeTest+ : null,+ );const credentialData = credential.credential;const modalType = credentialTypeToModalType(credential.credential_type);const getTotpTypeDisplay = (totpType: string) => {@@ -116,6 +126,20 @@ function CredentialItem({ credential, onStartBackgroundTest }: Props) {{credential.name}</p><p className="text-sm text-slate-400">{credential.credential_id}</p>+ {activeTest && (+ <div className="flex items-center gap-1 text-xs">+ <ReloadIcon className="size-3 animate-spin text-blue-400" />+ <a+ href={`/runs/${activeTest.workflowRunId}/overview`}+ target="_blank"+ rel="noopener noreferrer"+ className="flex items-center gap-1 text-blue-400 hover:text-blue-300"+ >+ Testing login+ <ExternalLinkIcon className="size-3" />+ </a>+ </div>+ )}{credential.browser_profile_id && (<div className="flex items-center gap-1 text-xs"><TooltipProvider>diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsxindex ece170524..d0a5198d9 100644--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx@@ -42,6 +42,7 @@ import { HelpTooltip } from "@/components/HelpTooltip";import { Label } from "@/components/ui/label";import { Textarea } from "@/components/ui/textarea";import { getHostname } from "@/util/getHostname";+import { ExternalLinkIcon } from "@radix-ui/react-icons";const PASSWORD_CREDENTIAL_INITIAL_VALUES = {name: "",@@ -175,10 +176,13 @@ function CredentialsModal({);// The temporary credential ID and workflow run ID created by the test-login endpointconst [testCredentialId, setTestCredentialId] = useState<string | null>(null);- // testWorkflowRunId is stored only as a ref (not state) because it's never- // rendered — it's only needed by cancelTest/close to call the cancel API.- // Refs mirror state so cancelTest always has the latest IDs regardless of- // React's async render cycle (e.g. cancel during the startTest HTTP call).+ // Workflow run ID used to render the "watch live" link — must be state so the+ // link appears immediately after the POST returns (refs don't trigger re-renders).+ const [testWorkflowRunId, setTestWorkflowRunId] = useState<string | null>(+ null,+ );+ // Refs mirror state so cancelTest/close always have the latest IDs regardless+ // of React's async render cycle (e.g. cancel during the startTest HTTP call).const testCredentialIdRef = useRef<string | null>(null);const testWorkflowRunIdRef = useRef<string | null>(null);const pollIntervalRef = useRef<ReturnType<typeof setTimeout> | null>(null);@@ -311,6 +315,7 @@ function CredentialsModal({setTestStatus("idle");setTestFailureReason(null);setTestCredentialId(null);+ setTestWorkflowRunId(null);testCredentialIdRef.current = null;testWorkflowRunIdRef.current = null;pollStartTimeRef.current = null;@@ -490,6 +495,7 @@ function CredentialsModal({}setTestCredentialId(data.credential_id);+ setTestWorkflowRunId(data.workflow_run_id);setTestStatus("testing");pollStartTimeRef.current = Date.now();@@ -982,9 +988,22 @@ function CredentialsModal({)}{isTestInProgress && (- <div className="flex items-center gap-2 pl-7 text-sm text-muted-foreground">- <ReloadIcon className="size-4 animate-spin" />- <span>{TEST_STATUS_MESSAGES[testMessageIndex]}</span>+ <div className="space-y-1 pl-7">+ <div className="flex items-center gap-2 text-sm text-muted-foreground">+ <ReloadIcon className="size-4 animate-spin" />+ <span>{TEST_STATUS_MESSAGES[testMessageIndex]}</span>+ </div>+ {testWorkflowRunId && (+ <a+ href={`/runs/${testWorkflowRunId}/overview`}+ target="_blank"+ rel="noopener noreferrer"+ className="flex items-center gap-1 text-xs text-blue-400 hover:text-blue-300"+ >+ <ExternalLinkIcon className="size-3" />+ Watch Skyvern test login live+ </a>+ )}</div>)}{testStatus === "completed" && (@@ -1120,6 +1139,7 @@ function CredentialsModal({testCredentialIdRef.current = null;testWorkflowRunIdRef.current = null;setTestCredentialId(null);+ setTestWorkflowRunId(null);toast({title: "Test canceled",description: "The credential test has been canceled.",diff --git a/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.ts b/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.tsindex 410959f7c..f304ffa25 100644--- a/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.ts+++ b/skyvern-frontend/src/routes/credentials/useBackgroundCredentialTest.ts@@ -8,6 +8,7 @@ import type {TestLoginResponse,} from "@/api/types";import { getHostname } from "@/util/getHostname";+import { useCredentialTestStore } from "@/store/useCredentialTestStore";const MAX_POLL_DURATION_MS = 5 * 60 * 1000;const POLL_INTERVAL_MS = 5000;@@ -30,6 +31,10 @@ type ActiveTest = {* The hook polls the backend, shows toast notifications on completion/failure,* and invalidates the credentials query so the list updates.*+ * Active test state is persisted in a zustand store backed by sessionStorage+ * so it survives both SPA navigation and full page reloads. On mount, the hook+ * checks for persisted state and resumes polling if a test was in progress.+ ** Instantiate this in a component that outlives the modal (e.g. CredentialsPage)* so polling survives modal close.*/@@ -37,17 +42,28 @@ function useBackgroundCredentialTest() {const credentialGetter = useCredentialGetter();const queryClient = useQueryClient();const activeTestRef = useRef<ActiveTest | null>(null);+ const { setActiveTest, clearActiveTest } = useCredentialTestStore();+ // Full cleanup: stop polling AND clear the persisted store.+ // Used when a test reaches a terminal state (completed/failed/timeout).const cleanup = useCallback(() => {if (activeTestRef.current?.timeoutId) {clearTimeout(activeTestRef.current.timeoutId);}activeTestRef.current = null;- }, []);+ clearActiveTest();+ }, [clearActiveTest]);+ // On unmount, only stop the timer — don't clear the store so the test+ // link survives SPA navigation. Polling resumes via rehydration on remount.useEffect(() => {- return cleanup;- }, [cleanup]);+ return () => {+ if (activeTestRef.current?.timeoutId) {+ clearTimeout(activeTestRef.current.timeoutId);+ activeTestRef.current.timeoutId = null;+ }+ };+ }, []);const poll = useCallback(async () => {const test = activeTestRef.current;@@ -167,6 +183,34 @@ function useBackgroundCredentialTest() {}}, [credentialGetter, queryClient, cleanup]);+ // Rehydrate polling from persisted store after a full page reload.+ // If a test was in progress before the reload, resume polling so the+ // link stays visible and the toast fires when the test finishes.+ const rehydratedRef = useRef(false);+ useEffect(() => {+ if (rehydratedRef.current) return;+ rehydratedRef.current = true;++ const stored = useCredentialTestStore.getState().activeTest;+ if (!stored || activeTestRef.current) return;++ // If the persisted test already exceeded the timeout, clean up+ if (Date.now() - stored.startTime > MAX_POLL_DURATION_MS) {+ clearActiveTest();+ return;+ }++ activeTestRef.current = {+ credentialId: stored.credentialId,+ workflowRunId: stored.workflowRunId,+ url: stored.url,+ startTime: stored.startTime,+ timeoutId: setTimeout(poll, POLL_INTERVAL_MS),+ errorCount: 0,+ };+ // eslint-disable-next-line react-hooks/exhaustive-deps -- intentionally runs once on mount+ }, []);+const startBackgroundTest = useCallback(async (credentialId: string, url: string, userContext?: string) => {// Clean up any previous test@@ -184,14 +228,21 @@ function useBackgroundCredentialTest() {);const data = response.data;+ const startTime = Date.now();activeTestRef.current = {credentialId,workflowRunId: data.workflow_run_id,url,- startTime: Date.now(),+ startTime,timeoutId: setTimeout(poll, POLL_INTERVAL_MS),errorCount: 0,};+ setActiveTest({+ credentialId,+ workflowRunId: data.workflow_run_id,+ url,+ startTime,+ });} catch {toast({title: "Failed to start browser profile test",@@ -201,7 +252,7 @@ function useBackgroundCredentialTest() {});}},- [credentialGetter, cleanup, poll],+ [credentialGetter, cleanup, poll, setActiveTest],);return { startBackgroundTest };diff --git a/skyvern-frontend/src/store/useCredentialTestStore.ts b/skyvern-frontend/src/store/useCredentialTestStore.tsnew file mode 100644index 000000000..1f9096558--- /dev/null+++ b/skyvern-frontend/src/store/useCredentialTestStore.ts@@ -0,0 +1,31 @@+import { create } from "zustand";+import { persist, createJSONStorage } from "zustand/middleware";++type ActiveCredentialTest = {+ credentialId: string;+ workflowRunId: string;+ url: string;+ startTime: number;+};++type CredentialTestStore = {+ activeTest: ActiveCredentialTest | null;+ setActiveTest: (test: ActiveCredentialTest) => void;+ clearActiveTest: () => void;+};++export type { ActiveCredentialTest };++export const useCredentialTestStore = create<CredentialTestStore>()(+ persist(+ (set) => ({+ activeTest: null,+ setActiveTest: (test) => set({ activeTest: test }),+ clearActiveTest: () => set({ activeTest: null }),+ }),+ {+ name: "credential-test",+ storage: createJSONStorage(() => sessionStorage),+ },+ ),+);