|
| 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