| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import os |
| 6 |
import threading |
| 7 |
from time import sleep |
| 8 |
|
| 9 |
import mozrunner |
| 10 |
import pytest |
| 11 |
from moztest.selftest import fixtures |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture(scope="session") |
| 15 |
def get_binary(): |
| 16 |
if "BROWSER_PATH" in os.environ: |
| 17 |
os.environ["GECKO_BINARY_PATH"] = os.environ["BROWSER_PATH"] |
| 18 |
|
| 19 |
def inner(app): |
| 20 |
if app not in ("chrome", "chromium", "firefox"): |
| 21 |
pytest.xfail(reason=f"{app} support not implemented") |
| 22 |
|
| 23 |
if app == "firefox": |
| 24 |
binary = fixtures.binary() |
| 25 |
elif app == "chrome": |
| 26 |
binary = os.environ.get("CHROME_BINARY_PATH") |
| 27 |
elif app == "chromium": |
| 28 |
binary = os.environ.get("CHROMIUM_BINARY_PATH") |
| 29 |
|
| 30 |
if not binary: |
| 31 |
pytest.skip(f"could not find a {app} binary") |
| 32 |
return binary |
| 33 |
|
| 34 |
return inner |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture(params=["firefox", "chrome", "chromium"]) |
| 38 |
def runner(request, get_binary): |
| 39 |
app = request.param |
| 40 |
binary = get_binary(app) |
| 41 |
|
| 42 |
cmdargs = ["--headless"] |
| 43 |
if app in ["chrome", "chromium"]: |
| 44 |
# prevents headless chromium from exiting after loading the page |
| 45 |
cmdargs.append("--remote-debugging-port=9222") |
| 46 |
# only needed on Windows, but no harm in specifying it everywhere |
| 47 |
cmdargs.append("--disable-gpu") |
| 48 |
runner = mozrunner.runners[app](binary, cmdargs=cmdargs) |
| 49 |
runner.app = app |
| 50 |
yield runner |
| 51 |
runner.stop() |
| 52 |
|
| 53 |
|
| 54 |
class RunnerThread(threading.Thread): |
| 55 |
def __init__(self, runner, start=False, timeout=1): |
| 56 |
threading.Thread.__init__(self) |
| 57 |
self.runner = runner |
| 58 |
self.timeout = timeout |
| 59 |
self.do_start = start |
| 60 |
|
| 61 |
def run(self): |
| 62 |
sleep(self.timeout) |
| 63 |
if self.do_start: |
| 64 |
self.runner.start() |
| 65 |
else: |
| 66 |
self.runner.stop() |
| 67 |
|
| 68 |
|
| 69 |
@pytest.fixture |
| 70 |
def create_thread(): |
| 71 |
threads = [] |
| 72 |
|
| 73 |
def inner(*args, **kwargs): |
| 74 |
thread = RunnerThread(*args, **kwargs) |
| 75 |
threads.append(thread) |
| 76 |
return thread |
| 77 |
|
| 78 |
yield inner |
| 79 |
|
| 80 |
for thread in threads: |
| 81 |
thread.join() |