This is a test page served by Nginx.
# Web and Testing Containers Containers for web services, browser automation, and testing infrastructure including Nginx, Selenium WebDriver, and specialized testing utilities for comprehensive web application testing. ## Capabilities ### Browser WebDriver Container Selenium browser container for Chrome and Firefox automation with VNC support and video recording capabilities. ```python { .api } class BrowserWebDriverContainer: def __init__( self, capabilities: dict, options: Optional[Any] = None, image: Optional[str] = None, port: int = 4444, vnc_port: int = 5900, **kwargs: Any ): """ Initialize browser WebDriver container. Args: capabilities: Selenium capabilities dictionary options: Browser-specific options image: Docker image (auto-selected if None) port: Selenium Grid port (default 4444) vnc_port: VNC port for remote viewing (default 5900) **kwargs: Additional container options """ def get_driver(self): """ Get configured WebDriver instance. Returns: Selenium WebDriver instance """ def get_connection_url(self) -> str: """ Get Selenium Grid connection URL. Returns: Selenium Grid URL string """ def with_options(self, options: Any) -> "BrowserWebDriverContainer": """ Set browser-specific options. Args: options: Chrome/Firefox options object Returns: Self for method chaining """ def with_video(self, image: Optional[str] = None, video_path: Optional[str] = None) -> "BrowserWebDriverContainer": """ Enable video recording of browser session. Args: image: Video recorder image video_path: Host path to save videos Returns: Self for method chaining """ ``` ### Nginx Container Nginx web server container for serving static content, reverse proxy testing, and web server functionality. ```python { .api } class NginxContainer: def __init__( self, image: str = "nginx:alpine", port: int = 80, **kwargs: Any ): """ Initialize Nginx container. Args: image: Nginx Docker image port: HTTP port (default 80) **kwargs: Additional container options """ def get_url(self) -> str: """ Get Nginx server URL. Returns: Nginx server URL string """ ``` ### Testing Utility Containers Specialized containers for testing scenarios and development utilities. ```python { .api } class MailpitContainer: def __init__( self, image: str = "axllent/mailpit:latest", smtp_port: int = 1025, web_port: int = 8025, **kwargs: Any ): """ Initialize Mailpit email testing container. Args: image: Mailpit Docker image smtp_port: SMTP server port (default 1025) web_port: Web interface port (default 8025) **kwargs: Additional container options """ def get_smtp_connection_url(self) -> str: """ Get SMTP connection URL. Returns: SMTP connection URL string """ def get_web_url(self) -> str: """ Get web interface URL. Returns: Web interface URL string """ class SftpContainer: def __init__( self, image: str = "atmoz/sftp:latest", port: int = 22, username: str = "testuser", password: str = "testpass", **kwargs: Any ): """ Initialize SFTP server container. Args: image: SFTP Docker image port: SFTP port (default 22) username: SFTP username password: SFTP password **kwargs: Additional container options """ def get_connection_url(self) -> str: """ Get SFTP connection URL. Returns: SFTP connection URL string """ ``` ## Usage Examples ### Selenium Browser Automation ```python from testcontainers.selenium import BrowserWebDriverContainer from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Chrome browser automation chrome_capabilities = { "browserName": "chrome", "browserVersion": "latest" } with BrowserWebDriverContainer(chrome_capabilities) as chrome: # Get WebDriver instance driver = chrome.get_driver() try: # Navigate to a website driver.get("https://example.com") # Wait for page to load wait = WebDriverWait(driver, 10) title_element = wait.until( EC.presence_of_element_located((By.TAG_NAME, "h1")) ) # Interact with page print(f"Page title: {driver.title}") print(f"H1 text: {title_element.text}") # Take screenshot driver.save_screenshot("example_page.png") # Find and click elements links = driver.find_elements(By.TAG_NAME, "a") print(f"Found {len(links)} links on the page") finally: driver.quit() ``` ### Firefox with Custom Options ```python from testcontainers.selenium import BrowserWebDriverContainer from selenium.webdriver.firefox.options import Options # Configure Firefox options firefox_options = Options() firefox_options.add_argument("--headless") # Run in background firefox_options.set_preference("network.http.pipelining", True) firefox_capabilities = { "browserName": "firefox", "browserVersion": "latest" } with BrowserWebDriverContainer(firefox_capabilities) as firefox: firefox.with_options(firefox_options) driver = firefox.get_driver() try: # Test JavaScript execution driver.get("data:text/html,
This is a test page served by Nginx.
This is an HTML test email.
Click here """ html_part = MIMEText(html_content, "html") html_msg.attach(html_part) server.send_message(html_msg) # Check emails via web API import time time.sleep(1) # Wait for emails to be processed # Get emails via Mailpit API api_response = requests.get(f"{web_url}/api/v1/messages") emails = api_response.json() print(f"Received {len(emails['messages'])} emails") for email in emails["messages"]: print(f"- Subject: {email['Subject']}") print(f" From: {email['From']['Address']}") print(f" To: {email['To'][0]['Address']}") ``` ### SFTP File Transfer Testing ```python from testcontainers.sftp import SftpContainer import paramiko import io with SftpContainer() as sftp: connection_url = sftp.get_connection_url() # Parse connection details host = connection_url.split("://")[1].split("@")[1].split(":")[0] port = int(connection_url.split(":")[3]) username = "testuser" password = "testpass" # Create SSH client ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # Connect to SFTP server ssh.connect(hostname=host, port=port, username=username, password=password) sftp_client = ssh.open_sftp() # Upload file test_content = "Hello, SFTP!\nThis is a test file." file_buffer = io.StringIO(test_content) with sftp_client.open("test_upload.txt", "w") as remote_file: remote_file.write(test_content) # List files files = sftp_client.listdir(".") print(f"Files on SFTP server: {files}") # Download file with sftp_client.open("test_upload.txt", "r") as remote_file: downloaded_content = remote_file.read() print(f"Downloaded content: {downloaded_content}") # Create directory and upload multiple files sftp_client.mkdir("test_directory") for i in range(3): filename = f"test_directory/file_{i}.txt" content = f"Content of file {i}" with sftp_client.open(filename, "w") as remote_file: remote_file.write(content) # List directory contents dir_files = sftp_client.listdir("test_directory") print(f"Files in test_directory: {dir_files}") finally: sftp_client.close() ssh.close() ``` ### Complete Web Application Testing Stack ```python from testcontainers.selenium import BrowserWebDriverContainer from testcontainers.nginx import NginxContainer from testcontainers.mailpit import MailpitContainer from testcontainers.postgres import PostgresContainer from testcontainers.core.network import Network import tempfile import os # Create test web application app_html = """