Skip to content

Commit 7554958

Browse files
author
Sentience Dev
committed
fix tests
1 parent 626c5fb commit 7554958

File tree

1 file changed

+87
-16
lines changed

1 file changed

+87
-16
lines changed

tests/test_video_recording.py

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
import os
6-
import shutil
76
import tempfile
87
from pathlib import Path
98

@@ -17,7 +16,10 @@ def test_video_recording_basic():
1716
with tempfile.TemporaryDirectory() as temp_dir:
1817
video_dir = Path(temp_dir) / "recordings"
1918

20-
with SentienceBrowser(headless=True, record_video_dir=str(video_dir)) as browser:
19+
browser = SentienceBrowser(headless=True, record_video_dir=str(video_dir))
20+
browser.start()
21+
22+
try:
2123
browser.page.goto("https://example.com")
2224
browser.page.wait_for_load_state("networkidle")
2325

@@ -31,25 +33,34 @@ def test_video_recording_basic():
3133
# Verify file has content
3234
file_size = os.path.getsize(video_path)
3335
assert file_size > 0
36+
except Exception:
37+
browser.close()
38+
raise
3439

3540

3641
def test_video_recording_custom_resolution():
3742
"""Test video recording with custom resolution"""
3843
with tempfile.TemporaryDirectory() as temp_dir:
3944
video_dir = Path(temp_dir) / "recordings"
4045

41-
with SentienceBrowser(
46+
browser = SentienceBrowser(
4247
headless=True,
4348
record_video_dir=str(video_dir),
44-
record_video_size={"width": 1920, "height": 1080},
45-
) as browser:
49+
record_video_size={"width": 1920, "height": 1080}
50+
)
51+
browser.start()
52+
53+
try:
4654
browser.page.goto("https://example.com")
4755
browser.page.wait_for_load_state("networkidle")
4856

4957
video_path = browser.close()
5058

5159
assert video_path is not None
5260
assert os.path.exists(video_path)
61+
except Exception:
62+
browser.close()
63+
raise
5364

5465

5566
def test_video_recording_custom_output_path():
@@ -58,7 +69,10 @@ def test_video_recording_custom_output_path():
5869
video_dir = Path(temp_dir) / "recordings"
5970
custom_path = video_dir / "my_recording.webm"
6071

61-
with SentienceBrowser(headless=True, record_video_dir=str(video_dir)) as browser:
72+
browser = SentienceBrowser(headless=True, record_video_dir=str(video_dir))
73+
browser.start()
74+
75+
try:
6276
browser.page.goto("https://example.com")
6377
browser.page.wait_for_load_state("networkidle")
6478

@@ -67,6 +81,9 @@ def test_video_recording_custom_output_path():
6781
# Verify video was renamed to custom path
6882
assert video_path == str(custom_path)
6983
assert os.path.exists(custom_path)
84+
except Exception:
85+
browser.close()
86+
raise
7087

7188

7289
def test_video_recording_nested_output_path():
@@ -75,7 +92,10 @@ def test_video_recording_nested_output_path():
7592
video_dir = Path(temp_dir) / "recordings"
7693
nested_path = video_dir / "project" / "tutorials" / "video1.webm"
7794

78-
with SentienceBrowser(headless=True, record_video_dir=str(video_dir)) as browser:
95+
browser = SentienceBrowser(headless=True, record_video_dir=str(video_dir))
96+
browser.start()
97+
98+
try:
7999
browser.page.goto("https://example.com")
80100
browser.page.wait_for_load_state("networkidle")
81101

@@ -85,18 +105,27 @@ def test_video_recording_nested_output_path():
85105
assert video_path == str(nested_path)
86106
assert os.path.exists(nested_path)
87107
assert nested_path.parent.exists()
108+
except Exception:
109+
browser.close()
110+
raise
88111

89112

90113
def test_no_video_recording_when_disabled():
91114
"""Test that no video is created when recording is disabled"""
92-
with SentienceBrowser(headless=True) as browser:
115+
browser = SentienceBrowser(headless=True)
116+
browser.start()
117+
118+
try:
93119
browser.page.goto("https://example.com")
94120
browser.page.wait_for_load_state("networkidle")
95121

96122
video_path = browser.close()
97123

98124
# Should return None when recording is disabled
99125
assert video_path is None
126+
except Exception:
127+
browser.close()
128+
raise
100129

101130

102131
def test_video_recording_directory_auto_created():
@@ -105,7 +134,10 @@ def test_video_recording_directory_auto_created():
105134
# Use a non-existent directory
106135
video_dir = Path(temp_dir) / "new_recordings" / "subdir"
107136

108-
with SentienceBrowser(headless=True, record_video_dir=str(video_dir)) as browser:
137+
browser = SentienceBrowser(headless=True, record_video_dir=str(video_dir))
138+
browser.start()
139+
140+
try:
109141
browser.page.goto("https://example.com")
110142
browser.page.wait_for_load_state("networkidle")
111143

@@ -115,6 +147,9 @@ def test_video_recording_directory_auto_created():
115147
assert video_dir.exists()
116148
assert video_path is not None
117149
assert os.path.exists(video_path)
150+
except Exception:
151+
browser.close()
152+
raise
118153

119154

120155
def test_video_recording_with_pathlib():
@@ -123,16 +158,23 @@ def test_video_recording_with_pathlib():
123158
video_dir = Path(temp_dir) / "recordings"
124159
output_path = video_dir / "test_video.webm"
125160

126-
with SentienceBrowser(
127-
headless=True, record_video_dir=video_dir # Pass Path object
128-
) as browser:
161+
browser = SentienceBrowser(
162+
headless=True,
163+
record_video_dir=video_dir # Pass Path object
164+
)
165+
browser.start()
166+
167+
try:
129168
browser.page.goto("https://example.com")
130169
browser.page.wait_for_load_state("networkidle")
131170

132171
video_path = browser.close(output_path=output_path) # Pass Path object
133172

134173
assert os.path.exists(output_path)
135174
assert video_path == str(output_path)
175+
except Exception:
176+
browser.close()
177+
raise
136178

137179

138180
def test_video_recording_multiple_sessions():
@@ -144,13 +186,19 @@ def test_video_recording_multiple_sessions():
144186

145187
# Create 3 video recordings
146188
for i in range(3):
147-
with SentienceBrowser(headless=True, record_video_dir=str(video_dir)) as browser:
189+
browser = SentienceBrowser(headless=True, record_video_dir=str(video_dir))
190+
browser.start()
191+
192+
try:
148193
browser.page.goto("https://example.com")
149194
browser.page.wait_for_load_state("networkidle")
150195

151196
output_path = video_dir / f"video_{i}.webm"
152197
video_path = browser.close(output_path=str(output_path))
153198
video_paths.append(video_path)
199+
except Exception:
200+
browser.close()
201+
raise
154202

155203
# Verify all videos were created
156204
for video_path in video_paths:
@@ -168,6 +216,29 @@ def test_video_recording_default_resolution():
168216
assert browser.record_video_size == {"width": 1280, "height": 800}
169217

170218
browser.start()
171-
browser.page.goto("https://example.com")
172-
browser.page.wait_for_load_state("networkidle")
173-
browser.close()
219+
220+
try:
221+
browser.page.goto("https://example.com")
222+
browser.page.wait_for_load_state("networkidle")
223+
browser.close()
224+
except Exception:
225+
browser.close()
226+
raise
227+
228+
229+
def test_video_recording_with_context_manager():
230+
"""Test that context manager works when NOT calling close() manually"""
231+
with tempfile.TemporaryDirectory() as temp_dir:
232+
video_dir = Path(temp_dir) / "recordings"
233+
234+
# Use context manager WITHOUT calling close() manually
235+
with SentienceBrowser(headless=True, record_video_dir=str(video_dir)) as browser:
236+
browser.page.goto("https://example.com")
237+
browser.page.wait_for_load_state("networkidle")
238+
# Don't call browser.close() - let context manager handle it
239+
240+
# Verify video was created after context manager exits
241+
# Find the .webm file in the directory
242+
webm_files = list(video_dir.glob("*.webm"))
243+
assert len(webm_files) > 0
244+
assert os.path.exists(webm_files[0])

0 commit comments

Comments
 (0)