Project

General

Profile

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

haketilo / test / profiles.py @ 201fcfad

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
def set_profile_proxy(profile, proxy_host, proxy_port):
35
    # proxy type 1 designates "manual"
36
    profile.set_preference('network.proxy.type',                 1)
37
    profile.set_preference('network.proxy.no_proxies_on',        '')
38
    profile.set_preference('network.proxy.share_proxy_settings', True)
39

    
40
    for proto in ['http', 'ftp', 'socks', 'ssl']:
41
        profile.set_preference(f'network.proxy.{proto}',             proxy_host)
42
        profile.set_preference(f'network.proxy.{proto}_port',        proxy_port)
43
        profile.set_preference(f'network.proxy.backup.{proto}',      '')
44
        profile.set_preference(f'network.proxy.backup.{proto}_port', 0)
45

    
46
def set_profile_console_logging(profile):
47
    profile.set_preference('devtools.console.stdout.content', True)
48

    
49
def firefox_safe_mode(firefox_binary=default_firefox_binary,
50
                      proxy_host=default_proxy_host,
51
                      proxy_port=default_proxy_port):
52
    profile = webdriver.FirefoxProfile()
53
    set_profile_proxy(profile, proxy_host, proxy_port)
54
    set_profile_console_logging(profile)
55

    
56
    options = Options()
57
    options.add_argument('--safe-mode')
58

    
59
    return webdriver.Firefox(options=options, firefox_profile=profile,
60
                             firefox_binary=firefox_binary)
61

    
62
def firefox_with_profile(firefox_binary=default_firefox_binary,
63
                         profile_dir=default_clean_profile_dir,
64
                         proxy_host=default_proxy_host,
65
                         proxy_port=default_proxy_port):
66
    profile = webdriver.FirefoxProfile(profile_dir)
67
    set_profile_proxy(profile, proxy_host, proxy_port)
68
    set_profile_console_logging(profile)
69

    
70
    return webdriver.Firefox(firefox_profile=profile,
71
                             firefox_binary=firefox_binary)
(4-4/8)