Project

General

Profile

Download (3.91 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / profiles.py @ e1282a63

1
# SPDX-License-Identifier: GPL-3.0-or-later
2

    
3
"""
4
Browser profiles and Selenium driver initialization
5
"""
6

    
7
# This file is part of Haketilo.
8
#
9
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
10
#
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation, either version 3 of the License, or
14
# (at your option) any later version.
15
#
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
20
#
21
# You should have received a copy of the GNU General Public License
22
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
#
24
# I, Wojtek Kosior, thereby promise not to sue for violation of this file's
25
# license. Although I request that you do not make use this code in a
26
# proprietary program, I am not going to enforce this in court.
27

    
28
from selenium import webdriver
29
from selenium.webdriver.firefox.options import Options
30
import time
31

    
32
from .misc_constants import *
33

    
34
class HaketiloFirefox(webdriver.Firefox):
35
    """
36
    This wrapper class around selenium.webdriver.Firefox adds a `loaded_scripts`
37
    instance property that gets resetted to an empty array every time the
38
    `get()` method is called.
39
    """
40
    def __init__(self, *args, **kwargs):
41
        super().__init__(*args, **kwargs)
42
        self.reset_loaded_scripts()
43

    
44
    def reset_loaded_scripts(self):
45
        self.loaded_scripts = []
46

    
47
    def get(self, *args, **kwargs):
48
        self.reset_loaded_scripts()
49
        super().get(*args, **kwargs)
50

    
51
def set_profile_proxy(profile, proxy_host, proxy_port):
52
    """
53
    Create a Firefox profile that uses the specified HTTP proxy for all
54
    protocols.
55
    """
56
    # proxy type 1 designates "manual"
57
    profile.set_preference('network.proxy.type',                 1)
58
    profile.set_preference('network.proxy.no_proxies_on',        '')
59
    profile.set_preference('network.proxy.share_proxy_settings', True)
60

    
61
    for proto in ['http', 'ftp', 'socks', 'ssl']:
62
        profile.set_preference(f'network.proxy.{proto}',             proxy_host)
63
        profile.set_preference(f'network.proxy.{proto}_port',        proxy_port)
64
        profile.set_preference(f'network.proxy.backup.{proto}',      '')
65
        profile.set_preference(f'network.proxy.backup.{proto}_port', 0)
66

    
67
def set_profile_console_logging(profile):
68
    profile.set_preference('devtools.console.stdout.content', True)
69

    
70
def firefox_safe_mode(firefox_binary=default_firefox_binary,
71
                      proxy_host=default_proxy_host,
72
                      proxy_port=default_proxy_port):
73
    """
74
    Initialize a Firefox instance controlled by selenium. The instance is
75
    started in safe mode.
76
    """
77
    profile = webdriver.FirefoxProfile()
78
    set_profile_proxy(profile, proxy_host, proxy_port)
79
    set_profile_console_logging(profile)
80

    
81
    options = Options()
82
    options.add_argument('--safe-mode')
83

    
84
    return HaketiloFirefox(options=options, firefox_profile=profile,
85
                           firefox_binary=firefox_binary)
86

    
87
def firefox_with_profile(firefox_binary=default_firefox_binary,
88
                         profile_dir=default_clean_profile_dir,
89
                         proxy_host=default_proxy_host,
90
                         proxy_port=default_proxy_port):
91
    """
92
    Initialize a Firefox instance controlled by selenium. The instance is
93
    started using an empty profile (either the default one or the one passed to
94
    `configure` script). The empty profile is meant to make Firefox start with
95
    globally-installed extensions disabled.
96
    """
97
    profile = webdriver.FirefoxProfile(profile_dir)
98
    set_profile_proxy(profile, proxy_host, proxy_port)
99
    set_profile_console_logging(profile)
100

    
101
    return HaketiloFirefox(firefox_profile=profile,
102
                           firefox_binary=firefox_binary)
(4-4/8)