#!/usr/bin/env python3 # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. import os import random import subprocess import tempfile import time from basic_tests import SnapTestsBase from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.select import Select class QATests(SnapTestsBase): def __init__(self): self._dir = "qa_tests" self._http_server = None self._http_tmpdir = None super().__init__( exp=os.path.join( self._dir, f"qa_expectations_{self._distro_release()}.json" ) ) def _distro_release(self): with open("/etc/lsb-release") as lsb_release: f = list( filter( lambda x: x.startswith("DISTRIB_RELEASE"), lsb_release.read().split(), ) ) return f[0].split("=")[1].replace(".", "") def _start_local_http_server(self, port=45678): """Start a local HTTP server with a 1MB random file for download tests.""" # Create temporary directory tmpdir = tempfile.mkdtemp(prefix="snap-test-http-") # Generate 1MB random file random_file = os.path.join(tmpdir, "testfile.iso") with open(random_file, "wb") as f: f.write(os.urandom(1024 * 1024)) # 1MB of random bytes # Create simple HTML page with download link html_file = os.path.join(tmpdir, "index.html") with open(html_file, "w") as f: f.write( """ Test Download Page

Download Test

Download 1MB File """ ) # Start http.server as background process server_process = subprocess.Popen( ["python3", "-m", "http.server", str(port)], cwd=tmpdir, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) # Give server time to start time.sleep(0.5) self._logger.info(f"Started local HTTP server on port {port} in {tmpdir}") return server_process, tmpdir def _stop_local_http_server(self, server_process, tmpdir): """Stop the local HTTP server and clean up.""" if server_process: server_process.terminate() server_process.wait() self._logger.info("Stopped local HTTP server") # Clean up temporary directory if tmpdir and os.path.exists(tmpdir): import shutil shutil.rmtree(tmpdir) def _test_audio_playback( self, url, iframe_selector=None, click_to_play=False, video_selector=None ): self._logger.info(f"open url {url}") if url: self.open_tab(url) if iframe_selector: self._logger.info("find iframe") iframe = self._wait.until( EC.visibility_of_element_located((By.CSS_SELECTOR, iframe_selector)) ) self._driver.switch_to.frame(iframe) self._logger.info("find video") video = self._wait.until( EC.visibility_of_element_located(( By.CSS_SELECTOR, video_selector or "video", )) ) self._wait.until(lambda d: type(video.get_property("duration")) is float) assert video.get_property("duration") > 0.0, "