Skip to content

Commit a28f3fb

Browse files
authored
Merge pull request #47 from dannysigalovich/fix/windows-chrome-paths
fix: support multiple default Chrome paths on each OS
2 parents 75c8c97 + 54eb393 commit a28f3fb

File tree

4 files changed

+85
-26
lines changed

4 files changed

+85
-26
lines changed

pydoll/browser/chrome.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,25 @@ def _get_default_binary_location():
4646
the browser executable is not found at the default location.
4747
"""
4848
os_name = platform.system()
49+
4950
browser_paths = {
50-
'Windows':
51+
'Windows': [
5152
r'C:\Program Files\Google\Chrome\Application\chrome.exe',
52-
'Linux':
53+
r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe',
54+
],
55+
'Linux': [
5356
'/usr/bin/google-chrome',
54-
'Darwin':
55-
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
57+
],
58+
'Darwin': [
59+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
60+
]
5661
}
5762

5863
browser_path = browser_paths.get(os_name)
5964

6065
if not browser_path:
6166
raise ValueError('Unsupported OS')
6267

63-
return BrowserOptionsManager.validate_browser_path(
68+
return BrowserOptionsManager.validate_browser_paths(
6469
browser_path
6570
)

pydoll/browser/managers.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -275,22 +275,24 @@ def add_default_arguments(options: Options):
275275
options.arguments.append('--no-default-browser-check')
276276

277277
@staticmethod
278-
def validate_browser_path(path: str) -> str:
278+
def validate_browser_paths(paths: list[str]) -> str:
279279
"""
280280
Validates the provided browser executable path.
281281
282282
This method checks if the browser executable file exists at
283283
the specified path.
284284
285285
Args:
286-
path (str): The path to the browser executable.
286+
paths (list[str]): Lista de caminhos possíveis do navegador.
287+
287288
288289
Returns:
289290
str: The validated browser path if it exists.
290291
291292
Raises:
292293
ValueError: If the browser executable is not found at the path.
293294
"""
294-
if not os.path.exists(path):
295-
raise ValueError(f'Browser not found: {path}')
296-
return path
295+
for path in paths:
296+
if os.path.exists(path) and os.access(path, os.X_OK):
297+
return path
298+
raise ValueError(f"No valid browser path found in: {paths}")

tests/test_browser_managers.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,34 @@ def test_add_default_arguments():
138138
assert '--no-default-browser-check' in options.arguments
139139

140140

141-
def test_validate_browser_path_valid():
142-
with patch('os.path.exists', return_value=True):
143-
result = BrowserOptionsManager.validate_browser_path('/fake/path')
141+
def test_validate_browser_paths_valid():
142+
with patch('os.path.exists', return_value=True), patch('os.access', return_value=True):
143+
result = BrowserOptionsManager.validate_browser_paths(['/fake/path'])
144144
assert result == '/fake/path'
145145

146146

147-
def test_validate_browser_path_invalid():
147+
def test_validate_browser_paths_invalid():
148148
with patch('os.path.exists', return_value=False):
149149
with pytest.raises(ValueError):
150-
BrowserOptionsManager.validate_browser_path('/fake/path')
150+
BrowserOptionsManager.validate_browser_paths(['/fake/path'])
151+
152+
153+
def test_validate_browser_paths_multiple():
154+
def fake_exists(path):
155+
match path:
156+
case "/first/fake/path":
157+
return False
158+
case "/second/fake/path":
159+
return True
160+
case _:
161+
return False
162+
163+
def fake_access(path, mode):
164+
return path == '/second/fake/path'
165+
166+
with patch('os.path.exists', side_effect=fake_exists), patch('os.access', side_effect=fake_access):
167+
result = BrowserOptionsManager.validate_browser_paths([
168+
'/first/fake/path',
169+
'/second/fake/path'
170+
])
171+
assert result == '/second/fake/path'

tests/test_chrome.py

+42-11
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,52 @@ async def test__get_valid_page_key_error(mock_browser):
360360
TargetCommands.create_target(''), timeout=60
361361
)
362362

363-
@pytest.mark.parametrize('os_name, expected_browser_path', [
364-
('Windows', r'C:\Program Files\Google\Chrome\Application\chrome.exe'),
365-
('Linux', '/usr/bin/google-chrome'),
366-
('Darwin', '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
367-
])
363+
364+
@pytest.mark.parametrize(
365+
'os_name, expected_browser_paths, mock_return_value',
366+
[
367+
(
368+
'Windows',
369+
[
370+
r'C:\Program Files\Google\Chrome\Application\chrome.exe',
371+
r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
372+
],
373+
r'C:\Program Files\Google\Chrome\Application\chrome.exe'
374+
),
375+
(
376+
'Linux',
377+
['/usr/bin/google-chrome'],
378+
'/usr/bin/google-chrome'
379+
),
380+
(
381+
'Darwin',
382+
['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'],
383+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
384+
),
385+
]
386+
)
368387
@patch('platform.system')
369-
@patch.object(BrowserOptionsManager, 'validate_browser_path')
370-
def test__get_default_binary_location(mock_validate_browser_path, mock_platform_system, os_name, expected_browser_path):
388+
@patch.object(BrowserOptionsManager, 'validate_browser_paths')
389+
def test__get_default_binary_location(
390+
mock_validate_browser_paths,
391+
mock_platform_system,
392+
os_name,
393+
expected_browser_paths,
394+
mock_return_value
395+
):
371396
mock_platform_system.return_value = os_name
372-
mock_validate_browser_path.return_value = expected_browser_path
373-
397+
mock_validate_browser_paths.return_value = mock_return_value
374398
path = Chrome._get_default_binary_location()
375-
mock_validate_browser_path.assert_called_once_with(expected_browser_path)
399+
mock_validate_browser_paths.assert_called_once_with(expected_browser_paths)
400+
401+
assert path == mock_return_value
402+
403+
404+
def test__get_default_binary_location_unsupported_os():
405+
with patch('platform.system', return_value='SomethingElse'):
406+
with pytest.raises(ValueError, match='Unsupported OS'):
407+
Chrome._get_default_binary_location()
376408

377-
assert path == expected_browser_path
378409

379410
@patch('platform.system')
380411
def test__get_default_binary_location_throws_exception_if_os_not_supported(mock_platform_system):

0 commit comments

Comments
 (0)