-
Notifications
You must be signed in to change notification settings - Fork 349
fix: Add selenium video support #6 #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexanderankin
merged 3 commits into
testcontainers:main
from
JosefShenhav:selenium-with-video
May 14, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,17 +10,20 @@ | |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| import urllib3 | ||
| from typing_extensions import Self | ||
|
|
||
| from selenium import webdriver | ||
| from selenium.webdriver.common.options import ArgOptions | ||
| from testcontainers.core.container import DockerContainer | ||
| from testcontainers.core.network import Network | ||
| from testcontainers.core.waiting_utils import wait_container_is_ready | ||
| from testcontainers.selenium.video import SeleniumVideoContainer | ||
|
|
||
| IMAGES = {"firefox": "selenium/standalone-firefox-debug:latest", "chrome": "selenium/standalone-chrome-debug:latest"} | ||
| IMAGES = {"firefox": "selenium/standalone-firefox:latest", "chrome": "selenium/standalone-chrome:latest"} | ||
|
|
||
|
|
||
| def get_image_name(capabilities: str) -> str: | ||
|
|
@@ -51,6 +54,8 @@ def __init__( | |
| self.image = image or get_image_name(capabilities) | ||
| self.port = port | ||
| self.vnc_port = vnc_port | ||
| self.video = None | ||
| self.__video_network = None | ||
| super().__init__(image=self.image, **kwargs) | ||
| self.with_exposed_ports(self.port, self.vnc_port) | ||
|
|
||
|
|
@@ -72,3 +77,44 @@ def get_connection_url(self) -> str: | |
| ip = self.get_container_host_ip() | ||
| port = self.get_exposed_port(self.port) | ||
| return f"http://{ip}:{port}/wd/hub" | ||
|
|
||
| def with_video(self, image: Optional[str] = None, video_path: Optional[Path] = None) -> Self: | ||
| video_path = video_path or Path.cwd() | ||
|
|
||
| self.video = SeleniumVideoContainer(image) | ||
|
|
||
| video_folder_path = video_path.parent if video_path.suffix else video_path | ||
| self.video.set_videos_host_path(str(video_folder_path.resolve())) | ||
|
|
||
| if video_path.name: | ||
| self.video.set_video_name(video_path.name) | ||
|
|
||
| return self | ||
|
|
||
| def start(self) -> "DockerContainer": | ||
| if not self.video: | ||
| super().start() | ||
| return self | ||
|
|
||
| self.__video_network = Network().__enter__() | ||
|
|
||
| self.with_kwargs(network=self.__video_network.name) | ||
| super().start() | ||
|
|
||
| self.video.with_kwargs(network=self.__video_network.name).set_selenium_container_host( | ||
| self.get_wrapped_container().short_id | ||
| ).start() | ||
|
|
||
| return self | ||
|
|
||
| def stop(self, force=True, delete_volume=True) -> None: | ||
| if self.video: | ||
| # get_wrapped_container().stop -> stop the container | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should renamed video.stop to video.remove? |
||
| # video.stop -> remove the container | ||
| self.video.get_wrapped_container().stop() | ||
| self.video.stop(force, delete_volume) | ||
|
|
||
| super().stop(force, delete_volume) | ||
|
|
||
| if self.__video_network: | ||
| self.__video_network.remove() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
| from typing import Optional | ||
|
|
||
| from testcontainers.core.container import DockerContainer | ||
|
|
||
| VIDEO_DEFAULT_IMAGE = "selenium/video:ffmpeg-6.1-20240402" | ||
|
|
||
|
|
||
| class SeleniumVideoContainer(DockerContainer): | ||
| """ | ||
| Selenium video container. | ||
| """ | ||
|
|
||
| def __init__(self, image: Optional[str] = None, **kwargs) -> None: | ||
| self.image = image or VIDEO_DEFAULT_IMAGE | ||
| super().__init__(image=self.image, **kwargs) | ||
|
|
||
| def set_video_name(self, video_name: str) -> "DockerContainer": | ||
| self.with_env("FILE_NAME", video_name) | ||
| return self | ||
|
|
||
| def set_videos_host_path(self, host_path: str) -> "DockerContainer": | ||
| self.with_volume_mapping(host_path, "/videos", "rw") | ||
| return self | ||
|
|
||
| def set_selenium_container_host(self, host: str) -> "DockerContainer": | ||
| self.with_env("DISPLAY_CONTAINER_NAME", host) | ||
| return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.