Skip to content

Commit 470938a

Browse files
fix unit tests
1 parent 503d64e commit 470938a

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

stagehand/handlers/extract_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,4 @@ async def _extract_page_text(self) -> ExtractResult:
171171

172172
tree = await get_accessibility_tree(self.stagehand_page, self.logger)
173173
output_string = tree["simplified"]
174-
return ExtractResult(extraction=output_string)
174+
return ExtractResult(data={"extraction": output_string})

tests/unit/handlers/test_extract_handler.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ async def test_extract_with_default_schema(self, mock_stagehand_page):
6969
result = await handler.extract(options)
7070

7171
assert isinstance(result, ExtractResult)
72-
# Due to the current limitation where ExtractResult from stagehand.types only has a data field
73-
# and doesn't accept extra fields, the handler fails to properly populate the result
74-
# This is a known issue with the current implementation
75-
assert result.data is None # This is the current behavior due to the schema mismatch
72+
# The handler should now properly populate the result with extracted data
73+
assert result.data is not None
74+
assert result.data == {"extraction": "Sample extracted text from the page"}
7675

7776
# Verify the mocks were called
7877
mock_get_tree.assert_called_once()
@@ -129,10 +128,13 @@ class ProductModel(BaseModel):
129128
result = await handler.extract(options, ProductModel)
130129

131130
assert isinstance(result, ExtractResult)
132-
# Due to the current limitation where ExtractResult from stagehand.types only has a data field
133-
# and doesn't accept extra fields, the handler fails to properly populate the result
134-
# This is a known issue with the current implementation
135-
assert result.data is None # This is the current behavior due to the schema mismatch
131+
# The handler should now properly populate the result with a validated Pydantic model
132+
assert result.data is not None
133+
assert isinstance(result.data, ProductModel)
134+
assert result.data.name == "Wireless Mouse"
135+
assert result.data.price == 29.99
136+
assert result.data.in_stock is True
137+
assert result.data.tags == ["electronics", "computer", "accessories"]
136138

137139
# Verify the mocks were called
138140
mock_get_tree.assert_called_once()
@@ -163,11 +165,11 @@ async def test_extract_without_options(self, mock_stagehand_page):
163165
result = await handler.extract()
164166

165167
assert isinstance(result, ExtractResult)
166-
# When no options are provided, _extract_page_text tries to create ExtractResult(extraction=output_string)
167-
# But since ExtractResult from stagehand.types only has a data field, the extraction field will be None
168-
# and data will also be None. This is a limitation of the current implementation.
169-
# We'll test that it returns a valid ExtractResult instance
170-
assert result.data is None # This is the current behavior due to the schema mismatch
168+
# When no options are provided, _extract_page_text should return the page text in data field
169+
assert result.data is not None
170+
assert isinstance(result.data, dict)
171+
assert "extraction" in result.data
172+
assert result.data["extraction"] == "General page accessibility tree content"
171173

172174
# Verify the mock was called
173175
mock_get_tree.assert_called_once()

0 commit comments

Comments
 (0)