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
1414from typing import Optional
1515
1616import urllib3
17+ from typing_extensions import Self
1718
1819from selenium import webdriver
1920from selenium .webdriver .common .options import ArgOptions
2021from testcontainers .core .container import DockerContainer
22+ from testcontainers .core .network import Network
2123from testcontainers .core .waiting_utils import wait_container_is_ready
24+ from testcontainers .selenium .video import SeleniumVideoContainer
2225
23- IMAGES = {"firefox" : "selenium/standalone-firefox-debug :latest" , "chrome" : "selenium/standalone-chrome-debug :latest" }
26+ IMAGES = {"firefox" : "selenium/standalone-firefox:latest" , "chrome" : "selenium/standalone-chrome:latest" }
2427
2528
2629def get_image_name (capabilities : str ) -> str :
@@ -51,6 +54,8 @@ def __init__(
5154 self .image = image or get_image_name (capabilities )
5255 self .port = port
5356 self .vnc_port = vnc_port
57+ self .video = None
58+ self .__video_network = None
5459 super ().__init__ (image = self .image , ** kwargs )
5560 self .with_exposed_ports (self .port , self .vnc_port )
5661
@@ -72,3 +77,43 @@ def get_connection_url(self) -> str:
7277 ip = self .get_container_host_ip ()
7378 port = self .get_exposed_port (self .port )
7479 return f"http://{ ip } :{ port } /wd/hub"
80+
81+ def with_video (self , image : Optional [str ] = None , video_path : Optional [Path ] = None ) -> Self :
82+ video_path = video_path or Path .cwd ()
83+
84+ self .video = SeleniumVideoContainer (image )
85+ video_folder_path = video_path .parent .resolve ()
86+ self .video .set_videos_host_path (str (video_folder_path ))
87+
88+ if video_path .name :
89+ self .video .set_video_name (video_path .name )
90+
91+ return self
92+
93+ def start (self ) -> "DockerContainer" :
94+ if not self .video :
95+ super ().start ()
96+ return self
97+
98+ self .__video_network = Network ().__enter__ ()
99+
100+ self .with_kwargs (network = self .__video_network .name )
101+ super ().start ()
102+
103+ self .video .with_kwargs (network = self .__video_network .name ).set_selenium_container_host (
104+ self .get_wrapped_container ().short_id
105+ ).start ()
106+
107+ return self
108+
109+ def stop (self , force = True , delete_volume = True ) -> None :
110+ if self .video :
111+ # get_wrapped_container().stop -> stop the container
112+ # video.stop -> remove the container
113+ self .video .get_wrapped_container ().stop ()
114+ self .video .stop (force , delete_volume )
115+
116+ super ().stop (force , delete_volume )
117+
118+ if self .__video_network :
119+ self .__video_network .remove ()
0 commit comments