Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix browser tab not showing the most recent screenshot (#8389)

Xingyao Wang507c02e518e6
diff --git a/frontend/__tests__/services/observations.test.tsx b/frontend/__tests__/services/observations.test.tsx
new file mode 100644
index 0000000000..1a6d630dca
--- /dev/null
+++ b/frontend/__tests__/services/observations.test.tsx
@@ -0,0 +1,62 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
+import { handleObservationMessage } from "#/services/observations";
+import { setScreenshotSrc, setUrl } from "#/state/browser-slice";
+import ObservationType from "#/types/observation-type";
+import store from "#/store";
+
+// Mock the store module
+vi.mock("#/store", () => ({
+ default: {
+ dispatch: vi.fn(),
+ },
+}));
+
+describe("handleObservationMessage", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ afterEach(() => {
+ vi.resetAllMocks();
+ });
+
+ it("updates browser state when receiving a browse observation", () => {
+ const message = {
+ id: "test-id",
+ cause: "test-cause",
+ observation: ObservationType.BROWSE,
+ content: "test content",
+ message: "test message",
+ extras: {
+ url: "https://example.com",
+ screenshot: "base64-screenshot-data",
+ },
+ };
+
+ handleObservationMessage(message);
+
+ // Check that setScreenshotSrc and setUrl were called with the correct values
+ expect(store.dispatch).toHaveBeenCalledWith(setScreenshotSrc("base64-screenshot-data"));
+ expect(store.dispatch).toHaveBeenCalledWith(setUrl("https://example.com"));
+ });
+
+ it("updates browser state when receiving a browse_interactive observation", () => {
+ const message = {
+ id: "test-id",
+ cause: "test-cause",
+ observation: ObservationType.BROWSE_INTERACTIVE,
+ content: "test content",
+ message: "test message",
+ extras: {
+ url: "https://example.com",
+ screenshot: "base64-screenshot-data",
+ },
+ };
+
+ handleObservationMessage(message);
+
+ // Check that setScreenshotSrc and setUrl were called with the correct values
+ expect(store.dispatch).toHaveBeenCalledWith(setScreenshotSrc("base64-screenshot-data"));
+ expect(store.dispatch).toHaveBeenCalledWith(setUrl("https://example.com"));
+ });
+});
\ No newline at end of file
diff --git a/frontend/src/services/observations.ts b/frontend/src/services/observations.ts
index 73643599dd..6dd0d3ae73 100644
--- a/frontend/src/services/observations.ts
+++ b/frontend/src/services/observations.ts
@@ -158,6 +158,13 @@ export function handleObservationMessage(message: ObservationMessage) {
);
break;
case "browse":
+ if (message.extras?.screenshot) {
+ store.dispatch(setScreenshotSrc(message.extras.screenshot));
+ }
+ if (message.extras?.url) {
+ store.dispatch(setUrl(message.extras.url));
+ }
+
store.dispatch(
addAssistantObservation({
...baseObservation,
@@ -198,6 +205,13 @@ export function handleObservationMessage(message: ObservationMessage) {
);
break;
case "browse_interactive":
+ if (message.extras?.screenshot) {
+ store.dispatch(setScreenshotSrc(message.extras.screenshot));
+ }
+ if (message.extras?.url) {
+ store.dispatch(setUrl(message.extras.url));
+ }
+
store.dispatch(
addAssistantObservation({
...baseObservation,