Skip to content

Commit 79727ea

Browse files
committed
Rebase and rewrite
1 parent 056e48d commit 79727ea

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

modules/selenium/testcontainers/selenium/__init__.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
13-
13+
from pathlib import Path
1414
from typing import Optional
1515

16+
from typing_extensions import Self
17+
1618
import urllib3
1719

1820
from selenium import webdriver
1921
from selenium.webdriver.common.options import ArgOptions
22+
23+
from core.network import Network
2024
from testcontainers.core.container import DockerContainer
2125
from testcontainers.core.waiting_utils import wait_container_is_ready
26+
from testcontainers.selenium.video import SeleniumVideoContainer
2227

23-
IMAGES = {"firefox": "selenium/standalone-firefox-debug:latest", "chrome": "selenium/standalone-chrome-debug:latest"}
28+
IMAGES = {"firefox": "selenium/standalone-firefox:latest", "chrome": "selenium/standalone-chrome:latest"}
2429

2530

2631
def get_image_name(capabilities: str) -> str:
@@ -51,6 +56,7 @@ def __init__(
5156
self.image = image or get_image_name(capabilities)
5257
self.port = port
5358
self.vnc_port = vnc_port
59+
self.video = None
5460
super().__init__(image=self.image, **kwargs)
5561
self.with_exposed_ports(self.port, self.vnc_port)
5662

@@ -72,3 +78,44 @@ def get_connection_url(self) -> str:
7278
ip = self.get_container_host_ip()
7379
port = self.get_exposed_port(self.port)
7480
return f"http://{ip}:{port}/wd/hub"
81+
82+
def with_video(self, image: Optional[str] = None, video_path: Optional[Path] = None) -> Self:
83+
video_path = video_path or Path.cwd()
84+
85+
self.video = SeleniumVideoContainer(image)
86+
video_folder_path = video_path.parent.resolve()
87+
self.video.set_videos_host_path(str(video_folder_path))
88+
89+
if video_path.name:
90+
self.video.set_video_name(video_path.name)
91+
92+
return self
93+
94+
def start(self) -> 'DockerContainer':
95+
if not self.video:
96+
super().start()
97+
return self
98+
99+
network = Network().__enter__()
100+
101+
self.with_kwargs(network=network.name)
102+
super().start()
103+
104+
self.video \
105+
.with_kwargs(network=network.name) \
106+
.set_selenium_container_host(self.get_wrapped_container().short_id) \
107+
.start()
108+
109+
return self
110+
111+
def stop(self, force=True, delete_volume=True) -> None:
112+
if self.video:
113+
# get_wrapped_container().stop -> stop the container
114+
# video.stop -> remove the container
115+
self.video.get_wrapped_container().stop()
116+
self.video.stop(force, delete_volume)
117+
118+
super().stop(force, delete_volume)
119+
120+
if self._network:
121+
self._network.remove()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
from typing import Optional
14+
15+
from testcontainers.core.container import DockerContainer
16+
17+
VIDEO_DEFAULT_IMAGE = "selenium/video:ffmpeg-6.1-20240402"
18+
19+
20+
class SeleniumVideoContainer(DockerContainer):
21+
"""
22+
Selenium video container.
23+
"""
24+
25+
def __init__(self, image: Optional[str] = None, **kwargs) -> None:
26+
self.image = image or VIDEO_DEFAULT_IMAGE
27+
super().__init__(image=self.image, **kwargs)
28+
29+
def set_video_name(self, video_name: str) -> 'DockerContainer':
30+
self.with_env("FILE_NAME", video_name)
31+
return self
32+
33+
def set_videos_host_path(self, host_path: str) -> 'DockerContainer':
34+
self.with_volume_mapping(host_path, "/videos", "rw")
35+
return self
36+
37+
def set_selenium_container_host(self, host: str) -> 'DockerContainer':
38+
self.with_env("DISPLAY_CONTAINER_NAME", host)
39+
return self

modules/selenium/tests/test_selenium.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import tempfile
2+
from pathlib import Path
3+
14
import pytest
25
from selenium.webdriver import DesiredCapabilities
36
from selenium.webdriver.common.by import By
@@ -23,3 +26,19 @@ def test_selenium_custom_image():
2326
chrome = BrowserWebDriverContainer(DesiredCapabilities.CHROME, image=image)
2427
assert "image" in dir(chrome), "`image` attribute was not instantialized."
2528
assert chrome.image == image, "`image` attribute was not set to the user provided value"
29+
30+
31+
@pytest.mark.parametrize("caps", [DesiredCapabilities.CHROME, DesiredCapabilities.FIREFOX])
32+
def test_selenium_video(caps, workdir):
33+
video_path = workdir / Path("video.mp4")
34+
with BrowserWebDriverContainer(caps).with_video(video_path=video_path) as chrome:
35+
chrome.get_driver().get("https://google.com")
36+
37+
assert video_path.exists(), "Selenium video file does not exists"
38+
39+
40+
@pytest.fixture
41+
def workdir() -> Path:
42+
tmpdir = tempfile.TemporaryDirectory()
43+
yield Path(tmpdir.name)
44+
tmpdir.cleanup()

0 commit comments

Comments
 (0)