Skip to content

Commit

Permalink
Add retrying of failed driver initialization (#249)
Browse files Browse the repository at this point in the history
Fixes: #219
  • Loading branch information
BeyondEvil committed Aug 5, 2020
1 parent 8b47104 commit 23f7f78
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ tox = "*"
flake8 = "*"
black = "*"
pre-commit = "*"
pytest-localserver = "*"
pytest-xdist = "*"
pytest-mock = "*"

[packages]
pytest-selenium = {editable = true,extras = ["appium"],path = "."}
2 changes: 2 additions & 0 deletions docs/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Release Notes

* Drop `PhantomJS` support

* Add driver initialization retry


1.17.0 (2019-07-13)
-------------------
Expand Down
17 changes: 16 additions & 1 deletion pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.event_firing_webdriver import EventFiringWebDriver
from tenacity import Retrying, stop_after_attempt, wait_exponential

from .utils import CaseInsensitiveDict
from . import drivers
Expand Down Expand Up @@ -195,7 +196,16 @@ def driver_path(request):
@pytest.fixture
def driver(request, driver_class, driver_kwargs):
"""Returns a WebDriver instance based on options and capabilities"""
driver = driver_class(**driver_kwargs)

retries = int(request.config.getini("max_driver_init_attempts"))
for retry in Retrying(
stop=stop_after_attempt(retries), wait=wait_exponential(), reraise=True
):
with retry:
LOGGER.info(
f"Driver init, attempt {retry.retry_state.attempt_number}/{retries}"
)
driver = driver_class(**driver_kwargs)

event_listener = request.config.getoption("event_listener")
if event_listener is not None:
Expand Down Expand Up @@ -409,6 +419,11 @@ def pytest_addoption(parser):
default=os.getenv("SAUCELABS_JOB_AUTH", "none"),
)

parser.addini(
"max_driver_init_attempts",
help="Maximum number of driver initialization attempts",
default=3,
)
group = parser.getgroup("selenium", "selenium")
group._addoption(
"--driver",
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"pytest-variables>=1.5.0",
"selenium>=3.0.0",
"requests",
"tenacity>=6,<7",
],
entry_points={
"pytest11": [
Expand Down
58 changes: 58 additions & 0 deletions testing/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

from functools import partial

import pytest_selenium
import pytest

from pytest_selenium.utils import CaseInsensitiveDict

pytestmark = pytest.mark.nondestructive


Expand Down Expand Up @@ -180,3 +183,58 @@ def test_pass(driver_kwargs):
"""
)
testdir.quick_qa("--driver", "Firefox", file_test, passed=1)


def test_driver_retry_pass(testdir, mocker):
mocker.patch.dict(
pytest_selenium.SUPPORTED_DRIVERS,
CaseInsensitiveDict({"Firefox": mocker.MagicMock()}),
)
mock_retrying = mocker.spy(pytest_selenium.pytest_selenium, "Retrying")

file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver):
assert driver
"""
)

testdir.quick_qa("--driver", "Firefox", file_test, passed=1)
assert mock_retrying.spy_return.statistics["attempt_number"] == 1


@pytest.mark.parametrize("max_num_attempts", [None, 2])
def test_driver_retry_fail(testdir, mocker, max_num_attempts):
mocker.patch(
"pytest_selenium.webdriver.firefox.webdriver.FirefoxRemoteConnection",
side_effect=Exception("Connection Error"),
)
mocker.patch("pytest_selenium.webdriver.firefox.webdriver.Service")
mocker.patch("pytest_selenium.pytest_selenium.wait_exponential", return_value=0)
mock_retrying = mocker.spy(pytest_selenium.pytest_selenium, "Retrying")

default_attempts = 3
if max_num_attempts is not None:
testdir.makeini(
f"""
[pytest]
max_driver_init_attempts = {max_num_attempts}
"""
)

file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver):
assert True # catch if this starts to pass
"""
)

testdir.quick_qa("--driver", "Firefox", file_test, failed=1)
expected_attempts = max_num_attempts or default_attempts
assert mock_retrying.spy_return.statistics["attempt_number"] == expected_attempts
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ setenv =
deps =
pytest-localserver
pytest-xdist
pytest-mock
commands = pytest -n auto -v -r a --color=yes --html={envlogdir}/report.html --self-contained-html {posargs}

[testenv:docs]
Expand Down

0 comments on commit 23f7f78

Please sign in to comment.