2020-02-12 10:56:34 +00:00
|
|
|
# Copyright (c) 2020, Matthew Waters <matthew@centricular.com>
|
2018-12-17 11:34:10 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
|
2020-02-12 10:56:34 +00:00
|
|
|
import logging
|
2018-12-17 11:34:10 +00:00
|
|
|
from selenium import webdriver
|
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
|
|
|
|
from selenium.webdriver.chrome.options import Options as COptions
|
|
|
|
|
2020-02-12 10:56:34 +00:00
|
|
|
l = logging.getLogger(__name__)
|
|
|
|
|
2018-12-17 11:34:10 +00:00
|
|
|
def create_firefox_driver():
|
|
|
|
capabilities = webdriver.DesiredCapabilities().FIREFOX.copy()
|
|
|
|
capabilities['acceptSslCerts'] = True
|
|
|
|
profile = FirefoxProfile()
|
|
|
|
profile.set_preference ('media.navigator.streams.fake', True)
|
|
|
|
profile.set_preference ('media.navigator.permission.disabled', True)
|
|
|
|
|
|
|
|
return webdriver.Firefox(firefox_profile=profile, capabilities=capabilities)
|
|
|
|
|
|
|
|
def create_chrome_driver():
|
|
|
|
capabilities = webdriver.DesiredCapabilities().CHROME.copy()
|
|
|
|
capabilities['acceptSslCerts'] = True
|
|
|
|
copts = COptions()
|
|
|
|
copts.add_argument('--allow-file-access-from-files')
|
|
|
|
copts.add_argument('--use-fake-ui-for-media-stream')
|
|
|
|
copts.add_argument('--use-fake-device-for-media-stream')
|
|
|
|
copts.add_argument('--enable-blink-features=RTCUnifiedPlanByDefault')
|
2020-02-12 10:56:34 +00:00
|
|
|
# XXX: until libnice can deal with mdns candidates
|
|
|
|
local_state = {"enabled_labs_experiments" : ["enable-webrtc-hide-local-ips-with-mdns@2"] }
|
|
|
|
copts.add_experimental_option("localState", {"browser" : local_state})
|
2018-12-17 11:34:10 +00:00
|
|
|
|
|
|
|
return webdriver.Chrome(options=copts, desired_capabilities=capabilities)
|
|
|
|
|
|
|
|
def create_driver(name):
|
|
|
|
if name == 'firefox':
|
|
|
|
return create_firefox_driver()
|
|
|
|
elif name == 'chrome':
|
|
|
|
return create_chrome_driver()
|
|
|
|
else:
|
2020-02-12 10:56:34 +00:00
|
|
|
raise ValueError("Unknown browser name \'" + name + "\'")
|
2018-12-17 11:34:10 +00:00
|
|
|
|
|
|
|
def valid_int(n):
|
|
|
|
if isinstance(n, int):
|
|
|
|
return True
|
|
|
|
if isinstance(n, str):
|
|
|
|
try:
|
|
|
|
num = int(n)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
class Browser(object):
|
2020-02-12 10:56:34 +00:00
|
|
|
"""
|
|
|
|
A browser as connected through selenium.
|
|
|
|
"""
|
|
|
|
def __init__(self, driver):
|
|
|
|
l.info('Using driver \'' + driver.name + '\' with capabilities ' + str(driver.capabilities))
|
2018-12-17 11:34:10 +00:00
|
|
|
self.driver = driver
|
2020-02-12 10:56:34 +00:00
|
|
|
|
|
|
|
def open(self, html_source):
|
|
|
|
self.driver.get(html_source)
|
2018-12-17 11:34:10 +00:00
|
|
|
|
|
|
|
def get_peer_id(self):
|
2020-02-12 10:56:34 +00:00
|
|
|
peer_id = WebDriverWait(self.driver, 5).until(
|
2018-12-17 11:34:10 +00:00
|
|
|
lambda x: x.find_element_by_id('peer-id'),
|
|
|
|
message='Peer-id element was never seen'
|
|
|
|
)
|
2020-02-12 10:56:34 +00:00
|
|
|
WebDriverWait (self.driver, 5).until(
|
2018-12-17 11:34:10 +00:00
|
|
|
lambda x: valid_int(peer_id.text),
|
|
|
|
message='Peer-id never became a number'
|
|
|
|
)
|
2020-02-12 10:56:34 +00:00
|
|
|
return int(peer_id.text)
|