Skip to content

Commit aca1eb3

Browse files
author
Sentience Dev
committed
examples & tests
1 parent 439cd55 commit aca1eb3

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
Advanced Video Recording Demo
3+
4+
Demonstrates advanced video recording features:
5+
- Custom resolution (1080p)
6+
- Custom output filename
7+
- Multiple recordings in one session
8+
"""
9+
10+
from sentience import SentienceBrowser
11+
from pathlib import Path
12+
from datetime import datetime
13+
14+
15+
def main():
16+
print("\n" + "=" * 60)
17+
print("Advanced Video Recording Demo")
18+
print("=" * 60 + "\n")
19+
20+
video_dir = Path("./recordings")
21+
video_dir.mkdir(exist_ok=True)
22+
23+
# Example 1: Custom Resolution (1080p)
24+
print("📹 Example 1: Recording in 1080p (Full HD)\n")
25+
26+
with SentienceBrowser(
27+
record_video_dir=str(video_dir),
28+
record_video_size={"width": 1920, "height": 1080} # 1080p resolution
29+
) as browser:
30+
print(" Resolution: 1920x1080")
31+
browser.page.goto("https://example.com")
32+
browser.page.wait_for_load_state("networkidle")
33+
browser.page.wait_for_timeout(2000)
34+
35+
# Close with custom filename
36+
video_path = browser.close(output_path=video_dir / "example_1080p.webm")
37+
print(f" ✅ Saved: {video_path}\n")
38+
39+
# Example 2: Custom Filename with Timestamp
40+
print("📹 Example 2: Recording with timestamp filename\n")
41+
42+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
43+
custom_filename = f"recording_{timestamp}.webm"
44+
45+
with SentienceBrowser(record_video_dir=str(video_dir)) as browser:
46+
browser.page.goto("https://example.com")
47+
browser.page.click("text=More information")
48+
browser.page.wait_for_timeout(2000)
49+
50+
video_path = browser.close(output_path=video_dir / custom_filename)
51+
print(f" ✅ Saved: {video_path}\n")
52+
53+
# Example 3: Organized by Project
54+
print("📹 Example 3: Organized directory structure\n")
55+
56+
project_dir = Path("./recordings/my_project/tutorials")
57+
58+
with SentienceBrowser(record_video_dir=str(project_dir)) as browser:
59+
print(f" Saving to: {project_dir}")
60+
browser.page.goto("https://example.com")
61+
browser.page.wait_for_timeout(2000)
62+
63+
video_path = browser.close(output_path=project_dir / "tutorial_01.webm")
64+
print(f" ✅ Saved: {video_path}\n")
65+
66+
# Example 4: Multiple videos with descriptive names
67+
print("📹 Example 4: Tutorial series with descriptive names\n")
68+
69+
tutorials = [
70+
("intro", "https://example.com"),
71+
("navigation", "https://example.com"),
72+
("features", "https://example.com"),
73+
]
74+
75+
for name, url in tutorials:
76+
with SentienceBrowser(record_video_dir=str(video_dir)) as browser:
77+
browser.page.goto(url)
78+
browser.page.wait_for_timeout(1000)
79+
80+
video_path = browser.close(output_path=video_dir / f"{name}.webm")
81+
print(f" ✅ {name}: {video_path}")
82+
83+
print("\n" + "=" * 60)
84+
print("All recordings completed!")
85+
print(f"Check {video_dir.absolute()} for all videos")
86+
print("=" * 60 + "\n")
87+
88+
89+
if __name__ == "__main__":
90+
main()

tests/test_video_recording.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
"""
2+
Tests for video recording functionality
3+
"""
4+
5+
import os
6+
import pytest
7+
from pathlib import Path
8+
import tempfile
9+
import shutil
10+
11+
from sentience import SentienceBrowser
12+
13+
14+
def test_video_recording_basic():
15+
"""Test basic video recording functionality"""
16+
with tempfile.TemporaryDirectory() as temp_dir:
17+
video_dir = Path(temp_dir) / "recordings"
18+
19+
with SentienceBrowser(
20+
headless=True,
21+
record_video_dir=str(video_dir)
22+
) as browser:
23+
browser.page.goto("https://example.com")
24+
browser.page.wait_for_load_state("networkidle")
25+
26+
video_path = browser.close()
27+
28+
# Verify video was created
29+
assert video_path is not None
30+
assert os.path.exists(video_path)
31+
assert video_path.endswith(".webm")
32+
33+
# Verify file has content
34+
file_size = os.path.getsize(video_path)
35+
assert file_size > 0
36+
37+
38+
def test_video_recording_custom_resolution():
39+
"""Test video recording with custom resolution"""
40+
with tempfile.TemporaryDirectory() as temp_dir:
41+
video_dir = Path(temp_dir) / "recordings"
42+
43+
with SentienceBrowser(
44+
headless=True,
45+
record_video_dir=str(video_dir),
46+
record_video_size={"width": 1920, "height": 1080}
47+
) as browser:
48+
browser.page.goto("https://example.com")
49+
browser.page.wait_for_load_state("networkidle")
50+
51+
video_path = browser.close()
52+
53+
assert video_path is not None
54+
assert os.path.exists(video_path)
55+
56+
57+
def test_video_recording_custom_output_path():
58+
"""Test video recording with custom output path"""
59+
with tempfile.TemporaryDirectory() as temp_dir:
60+
video_dir = Path(temp_dir) / "recordings"
61+
custom_path = video_dir / "my_recording.webm"
62+
63+
with SentienceBrowser(
64+
headless=True,
65+
record_video_dir=str(video_dir)
66+
) as browser:
67+
browser.page.goto("https://example.com")
68+
browser.page.wait_for_load_state("networkidle")
69+
70+
video_path = browser.close(output_path=str(custom_path))
71+
72+
# Verify video was renamed to custom path
73+
assert video_path == str(custom_path)
74+
assert os.path.exists(custom_path)
75+
76+
77+
def test_video_recording_nested_output_path():
78+
"""Test video recording with nested directory in output path"""
79+
with tempfile.TemporaryDirectory() as temp_dir:
80+
video_dir = Path(temp_dir) / "recordings"
81+
nested_path = video_dir / "project" / "tutorials" / "video1.webm"
82+
83+
with SentienceBrowser(
84+
headless=True,
85+
record_video_dir=str(video_dir)
86+
) as browser:
87+
browser.page.goto("https://example.com")
88+
browser.page.wait_for_load_state("networkidle")
89+
90+
video_path = browser.close(output_path=str(nested_path))
91+
92+
# Verify nested directories were created
93+
assert video_path == str(nested_path)
94+
assert os.path.exists(nested_path)
95+
assert nested_path.parent.exists()
96+
97+
98+
def test_no_video_recording_when_disabled():
99+
"""Test that no video is created when recording is disabled"""
100+
with SentienceBrowser(headless=True) as browser:
101+
browser.page.goto("https://example.com")
102+
browser.page.wait_for_load_state("networkidle")
103+
104+
video_path = browser.close()
105+
106+
# Should return None when recording is disabled
107+
assert video_path is None
108+
109+
110+
def test_video_recording_directory_auto_created():
111+
"""Test that video directory is automatically created"""
112+
with tempfile.TemporaryDirectory() as temp_dir:
113+
# Use a non-existent directory
114+
video_dir = Path(temp_dir) / "new_recordings" / "subdir"
115+
116+
with SentienceBrowser(
117+
headless=True,
118+
record_video_dir=str(video_dir)
119+
) as browser:
120+
browser.page.goto("https://example.com")
121+
browser.page.wait_for_load_state("networkidle")
122+
123+
video_path = browser.close()
124+
125+
# Verify directory was created
126+
assert video_dir.exists()
127+
assert video_path is not None
128+
assert os.path.exists(video_path)
129+
130+
131+
def test_video_recording_with_pathlib():
132+
"""Test video recording using pathlib.Path objects"""
133+
with tempfile.TemporaryDirectory() as temp_dir:
134+
video_dir = Path(temp_dir) / "recordings"
135+
output_path = video_dir / "test_video.webm"
136+
137+
with SentienceBrowser(
138+
headless=True,
139+
record_video_dir=video_dir # Pass Path object
140+
) as browser:
141+
browser.page.goto("https://example.com")
142+
browser.page.wait_for_load_state("networkidle")
143+
144+
video_path = browser.close(output_path=output_path) # Pass Path object
145+
146+
assert os.path.exists(output_path)
147+
assert video_path == str(output_path)
148+
149+
150+
def test_video_recording_multiple_sessions():
151+
"""Test creating multiple video recordings in sequence"""
152+
with tempfile.TemporaryDirectory() as temp_dir:
153+
video_dir = Path(temp_dir) / "recordings"
154+
155+
video_paths = []
156+
157+
# Create 3 video recordings
158+
for i in range(3):
159+
with SentienceBrowser(
160+
headless=True,
161+
record_video_dir=str(video_dir)
162+
) as browser:
163+
browser.page.goto("https://example.com")
164+
browser.page.wait_for_load_state("networkidle")
165+
166+
output_path = video_dir / f"video_{i}.webm"
167+
video_path = browser.close(output_path=str(output_path))
168+
video_paths.append(video_path)
169+
170+
# Verify all videos were created
171+
for video_path in video_paths:
172+
assert os.path.exists(video_path)
173+
174+
175+
def test_video_recording_default_resolution():
176+
"""Test that default resolution is 1280x800"""
177+
with tempfile.TemporaryDirectory() as temp_dir:
178+
video_dir = Path(temp_dir) / "recordings"
179+
180+
browser = SentienceBrowser(
181+
headless=True,
182+
record_video_dir=str(video_dir)
183+
)
184+
185+
# Verify default resolution
186+
assert browser.record_video_size == {"width": 1280, "height": 800}
187+
188+
browser.start()
189+
browser.page.goto("https://example.com")
190+
browser.page.wait_for_load_state("networkidle")
191+
browser.close()

0 commit comments

Comments
 (0)