Skip to content

Commit

Permalink
Merge branch 'master' into clean-up-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyPirogov authored Aug 28, 2023
2 parents db3d7c5 + de0872f commit 03c445f
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 450 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

---

## 4.0.1
- Add correct os_type determination while on cygwin
- Up-to date README.md

---

## 4.0.0
- Refactor

---

## 3.9.1
- Fix new chromedriver path
- Rework Manager os_type to class - Os System Manager

---

## 3.8.6
- Officially support python 3.11 (3.11.1+)
- Add MyPy stubs
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pytest = "*"
bump2version = "*"
pytest-xdist = "*"
pybrowsers = "*"
mock = "*"

[packages]
requests = "*"
Expand Down
423 changes: 0 additions & 423 deletions Pipfile.lock

This file was deleted.

59 changes: 40 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
```
Expand All @@ -71,7 +71,7 @@ driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).i
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
```
Expand All @@ -82,7 +82,7 @@ driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_typ
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install())
```
Expand All @@ -92,7 +92,7 @@ driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).inst
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))
```
Expand Down Expand Up @@ -181,7 +181,7 @@ options.add_experimental_option('w3c', True)
driver = webdriver.Remote(webdriver_service.service_url, options=options)
```

If the Opera browser is installed in a location other than `C:/Program Files` or `C:/Program Files (x86)` on windows
If the Opera browser is installed in a location other than `C:/Program Files` or `C:/Program Files (x86)` on Windows
and `/usr/bin/opera` for all unix variants and mac, then use the below code,

```python
Expand All @@ -195,16 +195,26 @@ driver = webdriver.Remote(webdriver_service.service_url, options=options)
To get the version of the browser from the executable of the browser itself:

```python
from webdriver_manager.core.utils import read_version_from_cmd, PATTERN
from webdriver_manager.firefox import GeckoDriverManager

from webdriver_manager.core.utils import read_version_from_cmd
from webdriver_manager.core.os_manager import PATTERN

version = read_version_from_cmd("/usr/bin/firefox-bin --version", PATTERN["firefox"])
driver_binary = FirefoxDriverManager(version=version).install()
driver_binary = GeckoDriverManager(version=version).install()
```

#### Custom Cache and File manager
#### Custom Cache, File manager and OS Manager

```python
cache_manager = DriverCacheManager(file_manager=FileManager())
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.file_manager import FileManager
from webdriver_manager.core.driver_cache import DriverCacheManager
from webdriver_manager.core.os_manager import OperationSystemManager

cache_manager = DriverCacheManager(file_manager=FileManager(os_system_manager=OperationSystemManager()))
manager = ChromeDriverManager(cache_manager=cache_manager)
os_manager = OperationSystemManager(os_type="win64")
```

## Configuration
Expand All @@ -214,7 +224,7 @@ Any variable can be set using either .env file or via python directly

### `GH_TOKEN`
**webdriver_manager** downloading some webdrivers from their official GitHub repositories but GitHub has [limitations](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting) like 60 requests per hour for unauthenticated users.
In case not to face an error related to github credentials, you need to [create](https://help.github.com/articles/creating-an-access-token-for-command-line-use) github token and place it into your environment: (\*)
In case not to face an error related to GitHub credentials, you need to [create](https://help.github.com/articles/creating-an-access-token-for-command-line-use) GitHub token and place it into your environment: (\*)

Example:

Expand Down Expand Up @@ -260,30 +270,41 @@ import os
os.environ['WDM_SSL_VERIFY'] = '0'
```

### `path`
Set the directory where you want to download and save the webdriver. You can use relative and absolute paths.
### `version`
Specify the version of webdriver you need. And webdriver-manager will download it from sources for your os.
```python
from webdriver_manager.chrome import ChromeDriverManager

ChromeDriverManager(driver_version="2.26").install()
```

### `cache_valid_range`
Driver cache by default is valid for 1 day. You are able to change this value using constructor parameter:

```python
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.driver_cache import DriverCacheManager

ChromeDriverManager(path = r".\\Drivers").install()
ChromeDriverManager("2.26", cache_manager=DriverCacheManager(valid_range=1)).install()
```

### `version`
Specify the version of webdriver you need. And webdriver-manager will download it from sources for your os.
### `os_type`
For some reasons you may use custom OS/arch. You are able to change this value using constructor parameter:

```python
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import OperationSystemManager

ChromeDriverManager(version="2.26").install()
ChromeDriverManager(os_system_manager=OperationSystemManager(os_type="linux-mips64")).install()
```

### `cache_valid_range`
Driver cache by default is valid for 1 day. You are able to change this value using constructor parameter:
### `url
You may use any other repo with drivers and release URl. You are able to change this value using constructor parameters:

```python
from webdriver_manager.chrome import ChromeDriverManager

ChromeDriverManager("2.26", cache_valid_range=1).install()
ChromeDriverManager(url="https://custom-repo.url", latest_release_url="https://custom-repo.url/LATEST").install()
```

---
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 4.0.0
current_version = 4.0.1
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
long_description_content_type="text/markdown",
packages=setuptools.find_packages(include=['webdriver_manager*']),
include_package_data=True,
version='4.0.0',
version='4.0.1',
description='Library provides the way to automatically manage drivers for different browsers',
author='Sergey Pirogov',
author_email='automationremarks@gmail.com',
Expand Down
8 changes: 8 additions & 0 deletions tests/test_chrome_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import pytest
import browsers
from selenium import webdriver
from mock import patch

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.constants import ROOT_FOLDER_NAME
from selenium.webdriver.chrome.service import Service
from webdriver_manager.core.driver import Driver

from webdriver_manager.core.driver_cache import DriverCacheManager
from webdriver_manager.core.os_manager import OperationSystemManager
Expand All @@ -26,6 +28,12 @@ def test_chrome_manager_with_specific_version(delete_drivers_dir):
assert os.path.exists(driver_binary)


@patch.object(Driver, 'get_browser_version_from_os', return_value="112.0.5615.165")
def test_chrome_manager_with_old_detected_version(mock_version, delete_drivers_dir):
driver_binary = ChromeDriverManager().install()
assert os.path.exists(driver_binary)


def test_106_0_5249_61_chrome_version(delete_drivers_dir):
driver_binary = ChromeDriverManager("106.0.5249.61").install()
assert os.path.exists(driver_binary)
Expand Down
2 changes: 1 addition & 1 deletion webdriver_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.0.0"
__version__ = "4.0.1"
1 change: 1 addition & 0 deletions webdriver_manager/core/driver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def __get_metadata_key(self, driver: Driver):
browser_version = browser_version if browser_version else ""
self._metadata_key = f"{self.get_os_type()}_{driver.get_name()}_{driver_version}" \
f"_for_{browser_version}"
return self._metadata_key

def get_cache_key_driver_version(self, driver: Driver):
if self._cache_key_driver_version:
Expand Down
2 changes: 1 addition & 1 deletion webdriver_manager/core/os_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_os_name():
return OSType.LINUX
elif pl == "darwin":
return OSType.MAC
elif pl == "win32":
elif pl == "win32" or pl == "cygwin":
return OSType.WIN

@staticmethod
Expand Down
21 changes: 17 additions & 4 deletions webdriver_manager/drivers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def get_latest_release_version(self):
log(f"Get LATEST {self._name} version for {self._browser_type}")
if determined_browser_version is not None and version.parse(determined_browser_version) >= version.parse("113"):
return determined_browser_version

# Remove the build version (the last segment) from determined_browser_version for version < 113
determined_browser_version = ".".join(determined_browser_version.split(".")[:3])
latest_release_url = (
self._latest_release_url
if (determined_browser_version is None)
Expand All @@ -69,11 +70,23 @@ def get_url_for_version_and_platform(self, browser_version, platform):
response = self._http_client.get(url)
data = response.json()
versions = data["versions"]
for v in versions:
if v["version"] == browser_version:
downloads = v["downloads"]["chromedriver"]

if version.parse(browser_version) >= version.parse("115"):
short_version = ".".join(browser_version.split(".")[:3])
compatible_versions = [v for v in versions if short_version in v["version"]]
if compatible_versions:
latest_version = compatible_versions[-1]
log(f"WebDriver version {latest_version['version']} selected")
downloads = latest_version["downloads"]["chromedriver"]
for d in downloads:
if d["platform"] == platform:
return d["url"]
else:
for v in versions:
if v["version"] == browser_version:
downloads = v["downloads"]["chromedriver"]
for d in downloads:
if d["platform"] == platform:
return d["url"]

raise Exception(f"No such driver version {browser_version} for {platform}")

0 comments on commit 03c445f

Please sign in to comment.