2012-09-03 13:52:08 -03:00
|
|
|
import webbrowser
|
|
|
|
import unittest
|
2018-07-08 04:22:32 -03:00
|
|
|
import os
|
|
|
|
import sys
|
2012-09-03 13:52:08 -03:00
|
|
|
import subprocess
|
|
|
|
from unittest import mock
|
|
|
|
from test import support
|
2024-03-28 04:59:33 -03:00
|
|
|
from test.support import is_apple_mobile
|
2020-08-03 13:49:18 -03:00
|
|
|
from test.support import import_helper
|
2020-08-06 08:51:29 -03:00
|
|
|
from test.support import os_helper
|
2024-03-28 04:59:33 -03:00
|
|
|
from test.support import requires_subprocess
|
|
|
|
from test.support import threading_helper
|
2012-09-03 13:52:08 -03:00
|
|
|
|
2024-03-28 04:59:33 -03:00
|
|
|
# The webbrowser module uses threading locks
|
|
|
|
threading_helper.requires_working_threading(module=True)
|
2012-09-03 13:52:08 -03:00
|
|
|
|
2023-03-31 12:02:47 -03:00
|
|
|
URL = 'https://www.example.com'
|
2012-09-03 13:52:08 -03:00
|
|
|
CMD_NAME = 'test'
|
|
|
|
|
|
|
|
|
|
|
|
class PopenMock(mock.MagicMock):
|
|
|
|
|
|
|
|
def poll(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def wait(self, seconds=None):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2024-03-28 04:59:33 -03:00
|
|
|
@requires_subprocess()
|
2012-09-03 13:52:08 -03:00
|
|
|
class CommandTestMixin:
|
|
|
|
|
|
|
|
def _test(self, meth, *, args=[URL], kw={}, options, arguments):
|
|
|
|
"""Given a web browser instance method name along with arguments and
|
|
|
|
keywords for same (which defaults to the single argument URL), creates
|
|
|
|
a browser instance from the class pointed to by self.browser, calls the
|
|
|
|
indicated instance method with the indicated arguments, and compares
|
|
|
|
the resulting options and arguments passed to Popen by the browser
|
|
|
|
instance against the 'options' and 'args' lists. Options are compared
|
|
|
|
in a position independent fashion, and the arguments are compared in
|
|
|
|
sequence order to whatever is left over after removing the options.
|
|
|
|
|
|
|
|
"""
|
|
|
|
popen = PopenMock()
|
|
|
|
support.patch(self, subprocess, 'Popen', popen)
|
|
|
|
browser = self.browser_class(name=CMD_NAME)
|
|
|
|
getattr(browser, meth)(*args, **kw)
|
|
|
|
popen_args = subprocess.Popen.call_args[0][0]
|
|
|
|
self.assertEqual(popen_args[0], CMD_NAME)
|
|
|
|
popen_args.pop(0)
|
|
|
|
for option in options:
|
|
|
|
self.assertIn(option, popen_args)
|
|
|
|
popen_args.pop(popen_args.index(option))
|
|
|
|
self.assertEqual(popen_args, arguments)
|
|
|
|
|
|
|
|
|
|
|
|
class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.GenericBrowser
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
|
|
|
|
class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.BackgroundBrowser
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
|
|
|
|
class ChromeCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.Chrome
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open', kw=dict(autoraise=False),
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new',
|
|
|
|
options=['--new-window'],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new_tab(self):
|
|
|
|
self._test('open_new_tab',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
|
2023-04-04 13:00:03 -03:00
|
|
|
class EdgeCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.Edge
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open', kw=dict(autoraise=False),
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new',
|
|
|
|
options=['--new-window'],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new_tab(self):
|
|
|
|
self._test('open_new_tab',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
|
2012-09-03 13:52:08 -03:00
|
|
|
class MozillaCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.Mozilla
|
|
|
|
|
2016-10-30 14:16:33 -03:00
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open', kw=dict(autoraise=False),
|
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new',
|
|
|
|
options=[],
|
|
|
|
arguments=['-new-window', URL])
|
|
|
|
|
|
|
|
def test_open_new_tab(self):
|
|
|
|
self._test('open_new_tab',
|
|
|
|
options=[],
|
|
|
|
arguments=['-new-tab', URL])
|
|
|
|
|
|
|
|
|
2023-03-31 12:02:47 -03:00
|
|
|
class EpiphanyCommandTest(CommandTestMixin, unittest.TestCase):
|
2016-10-30 14:16:33 -03:00
|
|
|
|
2023-03-31 12:02:47 -03:00
|
|
|
browser_class = webbrowser.Epiphany
|
2012-09-03 13:52:08 -03:00
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
|
|
|
options=['-n'],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open', kw=dict(autoraise=False),
|
|
|
|
options=['-noraise', '-n'],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new',
|
|
|
|
options=['-w'],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
def test_open_new_tab(self):
|
|
|
|
self._test('open_new_tab',
|
|
|
|
options=['-w'],
|
|
|
|
arguments=[URL])
|
|
|
|
|
|
|
|
|
|
|
|
class OperaCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.Opera
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open',
|
2018-07-03 08:30:06 -03:00
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
2012-09-03 13:52:08 -03:00
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open', kw=dict(autoraise=False),
|
2018-07-03 08:30:06 -03:00
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
2012-09-03 13:52:08 -03:00
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new',
|
2018-07-03 08:30:06 -03:00
|
|
|
options=['--new-window'],
|
|
|
|
arguments=[URL])
|
2012-09-03 13:52:08 -03:00
|
|
|
|
2013-09-29 11:48:19 -03:00
|
|
|
def test_open_new_tab(self):
|
2012-09-03 13:52:08 -03:00
|
|
|
self._test('open_new_tab',
|
2018-07-03 08:30:06 -03:00
|
|
|
options=[],
|
|
|
|
arguments=[URL])
|
2012-09-03 13:52:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ELinksCommandTest(CommandTestMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
browser_class = webbrowser.Elinks
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open', options=['-remote'],
|
|
|
|
arguments=['openURL({})'.format(URL)])
|
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open',
|
|
|
|
options=['-remote'],
|
|
|
|
arguments=['openURL({})'.format(URL)])
|
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new',
|
|
|
|
options=['-remote'],
|
|
|
|
arguments=['openURL({},new-window)'.format(URL)])
|
|
|
|
|
|
|
|
def test_open_new_tab(self):
|
|
|
|
self._test('open_new_tab',
|
|
|
|
options=['-remote'],
|
|
|
|
arguments=['openURL({},new-tab)'.format(URL)])
|
|
|
|
|
|
|
|
|
2024-03-28 04:59:33 -03:00
|
|
|
@unittest.skipUnless(sys.platform == "ios", "Test only applicable to iOS")
|
|
|
|
class IOSBrowserTest(unittest.TestCase):
|
|
|
|
def _obj_ref(self, *args):
|
|
|
|
# Construct a string representation of the arguments that can be used
|
|
|
|
# as a proxy for object instance references
|
|
|
|
return "|".join(str(a) for a in args)
|
|
|
|
|
|
|
|
@unittest.skipIf(getattr(webbrowser, "objc", None) is None,
|
|
|
|
"iOS Webbrowser tests require ctypes")
|
|
|
|
def setUp(self):
|
|
|
|
# Intercept the the objc library. Wrap the calls to get the
|
|
|
|
# references to classes and selectors to return strings, and
|
|
|
|
# wrap msgSend to return stringified object references
|
|
|
|
self.orig_objc = webbrowser.objc
|
|
|
|
|
|
|
|
webbrowser.objc = mock.Mock()
|
|
|
|
webbrowser.objc.objc_getClass = lambda cls: f"C#{cls.decode()}"
|
|
|
|
webbrowser.objc.sel_registerName = lambda sel: f"S#{sel.decode()}"
|
|
|
|
webbrowser.objc.objc_msgSend.side_effect = self._obj_ref
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
webbrowser.objc = self.orig_objc
|
|
|
|
|
|
|
|
def _test(self, meth, **kwargs):
|
|
|
|
# The browser always gets focus, there's no concept of separate browser
|
|
|
|
# windows, and there's no API-level control over creating a new tab.
|
|
|
|
# Therefore, all calls to webbrowser are effectively the same.
|
|
|
|
getattr(webbrowser, meth)(URL, **kwargs)
|
|
|
|
|
|
|
|
# The ObjC String version of the URL is created with UTF-8 encoding
|
|
|
|
url_string_args = [
|
|
|
|
"C#NSString",
|
|
|
|
"S#stringWithCString:encoding:",
|
|
|
|
b'https://www.example.com',
|
|
|
|
4,
|
|
|
|
]
|
|
|
|
# The NSURL version of the URL is created from that string
|
|
|
|
url_obj_args = [
|
|
|
|
"C#NSURL",
|
|
|
|
"S#URLWithString:",
|
|
|
|
self._obj_ref(*url_string_args),
|
|
|
|
]
|
|
|
|
# The openURL call is invoked on the shared application
|
|
|
|
shared_app_args = ["C#UIApplication", "S#sharedApplication"]
|
|
|
|
|
|
|
|
# Verify that the last call is the one that opens the URL.
|
|
|
|
webbrowser.objc.objc_msgSend.assert_called_with(
|
|
|
|
self._obj_ref(*shared_app_args),
|
|
|
|
"S#openURL:options:completionHandler:",
|
|
|
|
self._obj_ref(*url_obj_args),
|
|
|
|
None,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_open(self):
|
|
|
|
self._test('open')
|
|
|
|
|
|
|
|
def test_open_with_autoraise_false(self):
|
|
|
|
self._test('open', autoraise=False)
|
|
|
|
|
|
|
|
def test_open_new(self):
|
|
|
|
self._test('open_new')
|
|
|
|
|
|
|
|
def test_open_new_tab(self):
|
|
|
|
self._test('open_new_tab')
|
|
|
|
|
|
|
|
|
2017-02-25 04:14:07 -04:00
|
|
|
class BrowserRegistrationTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# Ensure we don't alter the real registered browser details
|
|
|
|
self._saved_tryorder = webbrowser._tryorder
|
|
|
|
webbrowser._tryorder = []
|
|
|
|
self._saved_browsers = webbrowser._browsers
|
|
|
|
webbrowser._browsers = {}
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
webbrowser._tryorder = self._saved_tryorder
|
|
|
|
webbrowser._browsers = self._saved_browsers
|
|
|
|
|
|
|
|
def _check_registration(self, preferred):
|
|
|
|
class ExampleBrowser:
|
|
|
|
pass
|
|
|
|
|
|
|
|
expected_tryorder = []
|
|
|
|
expected_browsers = {}
|
|
|
|
|
|
|
|
self.assertEqual(webbrowser._tryorder, expected_tryorder)
|
|
|
|
self.assertEqual(webbrowser._browsers, expected_browsers)
|
|
|
|
|
|
|
|
webbrowser.register('Example1', ExampleBrowser)
|
|
|
|
expected_tryorder = ['Example1']
|
|
|
|
expected_browsers['example1'] = [ExampleBrowser, None]
|
|
|
|
self.assertEqual(webbrowser._tryorder, expected_tryorder)
|
|
|
|
self.assertEqual(webbrowser._browsers, expected_browsers)
|
|
|
|
|
|
|
|
instance = ExampleBrowser()
|
|
|
|
if preferred is not None:
|
|
|
|
webbrowser.register('example2', ExampleBrowser, instance,
|
|
|
|
preferred=preferred)
|
|
|
|
else:
|
|
|
|
webbrowser.register('example2', ExampleBrowser, instance)
|
|
|
|
if preferred:
|
|
|
|
expected_tryorder = ['example2', 'Example1']
|
|
|
|
else:
|
|
|
|
expected_tryorder = ['Example1', 'example2']
|
|
|
|
expected_browsers['example2'] = [ExampleBrowser, instance]
|
|
|
|
self.assertEqual(webbrowser._tryorder, expected_tryorder)
|
|
|
|
self.assertEqual(webbrowser._browsers, expected_browsers)
|
|
|
|
|
|
|
|
def test_register(self):
|
|
|
|
self._check_registration(preferred=False)
|
|
|
|
|
|
|
|
def test_register_default(self):
|
|
|
|
self._check_registration(preferred=None)
|
|
|
|
|
|
|
|
def test_register_preferred(self):
|
|
|
|
self._check_registration(preferred=True)
|
|
|
|
|
|
|
|
|
2023-12-21 09:43:38 -04:00
|
|
|
@unittest.skipUnless(sys.platform == "darwin", "macOS specific test")
|
|
|
|
def test_no_xdg_settings_on_macOS(self):
|
|
|
|
# On macOS webbrowser should not use xdg-settings to
|
|
|
|
# look for X11 based browsers (for those users with
|
|
|
|
# XQuartz installed)
|
|
|
|
with mock.patch("subprocess.check_output") as ck_o:
|
|
|
|
webbrowser.register_standard_browsers()
|
|
|
|
|
|
|
|
ck_o.assert_not_called()
|
|
|
|
|
|
|
|
|
2017-03-08 11:15:54 -04:00
|
|
|
class ImportTest(unittest.TestCase):
|
|
|
|
def test_register(self):
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2017-03-08 11:15:54 -04:00
|
|
|
self.assertIsNone(webbrowser._tryorder)
|
|
|
|
self.assertFalse(webbrowser._browsers)
|
|
|
|
|
|
|
|
class ExampleBrowser:
|
|
|
|
pass
|
|
|
|
webbrowser.register('Example1', ExampleBrowser)
|
|
|
|
self.assertTrue(webbrowser._tryorder)
|
|
|
|
self.assertEqual(webbrowser._tryorder[-1], 'Example1')
|
|
|
|
self.assertTrue(webbrowser._browsers)
|
|
|
|
self.assertIn('example1', webbrowser._browsers)
|
|
|
|
self.assertEqual(webbrowser._browsers['example1'], [ExampleBrowser, None])
|
|
|
|
|
|
|
|
def test_get(self):
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2017-03-08 11:15:54 -04:00
|
|
|
self.assertIsNone(webbrowser._tryorder)
|
|
|
|
self.assertFalse(webbrowser._browsers)
|
|
|
|
|
|
|
|
with self.assertRaises(webbrowser.Error):
|
|
|
|
webbrowser.get('fakebrowser')
|
|
|
|
self.assertIsNotNone(webbrowser._tryorder)
|
|
|
|
|
2024-01-24 20:38:34 -04:00
|
|
|
@unittest.skipIf(" " in sys.executable, "test assumes no space in path (GH-114452)")
|
2018-07-08 04:22:32 -03:00
|
|
|
def test_synthesize(self):
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2018-07-08 04:22:32 -03:00
|
|
|
name = os.path.basename(sys.executable).lower()
|
|
|
|
webbrowser.register(name, None, webbrowser.GenericBrowser(name))
|
|
|
|
webbrowser.get(sys.executable)
|
|
|
|
|
2024-03-28 04:59:33 -03:00
|
|
|
@unittest.skipIf(
|
|
|
|
is_apple_mobile,
|
|
|
|
"Apple mobile doesn't allow modifying browser with environment"
|
|
|
|
)
|
2018-07-08 04:22:32 -03:00
|
|
|
def test_environment(self):
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2018-07-08 04:22:32 -03:00
|
|
|
try:
|
|
|
|
browser = webbrowser.get().name
|
2021-12-29 21:30:13 -04:00
|
|
|
except webbrowser.Error as err:
|
2018-07-08 04:22:32 -03:00
|
|
|
self.skipTest(str(err))
|
2020-08-06 08:51:29 -03:00
|
|
|
with os_helper.EnvironmentVarGuard() as env:
|
2018-07-08 04:22:32 -03:00
|
|
|
env["BROWSER"] = browser
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2018-07-08 04:22:32 -03:00
|
|
|
webbrowser.get()
|
|
|
|
|
2024-03-28 04:59:33 -03:00
|
|
|
@unittest.skipIf(
|
|
|
|
is_apple_mobile,
|
|
|
|
"Apple mobile doesn't allow modifying browser with environment"
|
|
|
|
)
|
2018-11-26 17:29:45 -04:00
|
|
|
def test_environment_preferred(self):
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2018-11-26 17:29:45 -04:00
|
|
|
try:
|
|
|
|
webbrowser.get()
|
|
|
|
least_preferred_browser = webbrowser.get(webbrowser._tryorder[-1]).name
|
2021-12-29 21:30:13 -04:00
|
|
|
except (webbrowser.Error, IndexError) as err:
|
2018-11-26 17:29:45 -04:00
|
|
|
self.skipTest(str(err))
|
|
|
|
|
2020-08-06 08:51:29 -03:00
|
|
|
with os_helper.EnvironmentVarGuard() as env:
|
2018-11-26 17:29:45 -04:00
|
|
|
env["BROWSER"] = least_preferred_browser
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2018-11-26 17:29:45 -04:00
|
|
|
self.assertEqual(webbrowser.get().name, least_preferred_browser)
|
|
|
|
|
2020-08-06 08:51:29 -03:00
|
|
|
with os_helper.EnvironmentVarGuard() as env:
|
2018-11-26 17:29:45 -04:00
|
|
|
env["BROWSER"] = sys.executable
|
2020-08-03 13:49:18 -03:00
|
|
|
webbrowser = import_helper.import_fresh_module('webbrowser')
|
2018-11-26 17:29:45 -04:00
|
|
|
self.assertEqual(webbrowser.get().name, sys.executable)
|
|
|
|
|
2017-03-08 11:15:54 -04:00
|
|
|
|
2012-09-03 13:52:08 -03:00
|
|
|
if __name__=='__main__':
|
|
|
|
unittest.main()
|