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
16+ from typing_extensions import Self
17+
1618import urllib3
1719
1820from selenium import webdriver
1921from selenium .webdriver .common .options import ArgOptions
22+
23+ from core .network import Network
2024from testcontainers .core .container import DockerContainer
2125from 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
2631def 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 ()
0 commit comments