summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozrunner/tests/conftest.py
blob: 991fc376fb14f154fbad6476181348143c693373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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 http://mozilla.org/MPL/2.0/.

import os
import threading
from time import sleep

import mozrunner
import pytest
from moztest.selftest import fixtures


@pytest.fixture(scope="session")
def get_binary():
    if "BROWSER_PATH" in os.environ:
        os.environ["GECKO_BINARY_PATH"] = os.environ["BROWSER_PATH"]

    def inner(app):
        if app not in ("chrome", "chromium", "firefox"):
            pytest.xfail(reason="{} support not implemented".format(app))

        if app == "firefox":
            binary = fixtures.binary()
        elif app == "chrome":
            binary = os.environ.get("CHROME_BINARY_PATH")
        elif app == "chromium":
            binary = os.environ.get("CHROMIUM_BINARY_PATH")

        if not binary:
            pytest.skip("could not find a {} binary".format(app))
        return binary

    return inner


@pytest.fixture(params=["firefox", "chrome", "chromium"])
def runner(request, get_binary):
    app = request.param
    binary = get_binary(app)

    cmdargs = ["--headless"]
    if app in ["chrome", "chromium"]:
        # prevents headless chromium from exiting after loading the page
        cmdargs.append("--remote-debugging-port=9222")
        # only needed on Windows, but no harm in specifying it everywhere
        cmdargs.append("--disable-gpu")
    runner = mozrunner.runners[app](binary, cmdargs=cmdargs)
    runner.app = app
    yield runner
    runner.stop()


class RunnerThread(threading.Thread):
    def __init__(self, runner, start=False, timeout=1):
        threading.Thread.__init__(self)
        self.runner = runner
        self.timeout = timeout
        self.do_start = start

    def run(self):
        sleep(self.timeout)
        if self.do_start:
            self.runner.start()
        else:
            self.runner.stop()


@pytest.fixture
def create_thread():
    threads = []

    def inner(*args, **kwargs):
        thread = RunnerThread(*args, **kwargs)
        threads.append(thread)
        return thread

    yield inner

    for thread in threads:
        thread.join()