question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

selenium-wire >=4 breaks with pytest-asyncio

See original GitHub issue

It looks like selenium-wire 4.0.0 or later (tested with 4.0.0 and 4.0.2) breaks when used with pytest-asyncio in an async test, apparently because there’s some conflict over the running loops.

To reproduce, run pip install selenium-wire pytest-asyncio in a new virtual env, and then run pytest on the following test file:

import pytest
from seleniumwire import webdriver


@pytest.fixture
def driver():
    options = webdriver.ChromeOptions()
    options.add_argument("headless")
    options.add_argument("no-sandbox")
    options.add_argument("disable-extensions")
    options.add_argument("incognito")
    driver = webdriver.Chrome(options=options)
    try:
        yield driver
    finally:
        driver.quit()


@pytest.mark.asyncio
async def test_selenium(driver) -> None:
    driver.get("https://www.google.com/")

I get the following output

============================= test session starts ==============================
platform linux -- Python 3.9.1+, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/eagle/tmp
plugins: asyncio-0.14.0
collected 1 item                                                               

test_selenium.py FException in thread Selenium Wire Proxy Server:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
E                                                      [100%]    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/eagle/dvl/venvs/selenium/lib/python3.9/site-packages/seleniumwire/server.py", line 86, in serve_forever
    self._master.run_loop(self._event_loop.run_forever)

  File "/home/eagle/dvl/venvs/selenium/lib/python3.9/site-packages/seleniumwire/thirdparty/mitmproxy/master.py", line 84, in run_loop

==================================== ERRORS ====================================
______________________ ERROR at teardown of test_selenium ______________________

    loop = asyncio.get_event_loop()
request = <SubRequest 'event_loop' for <Function test_selenium>>
  File "/usr/lib/python3.9/asyncio/events.py", line 642, in get_event_loop

    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Selenium Wire Proxy Server'.
    @pytest.fixture
    def event_loop(request):
        """Create an instance of the default event loop for each test case."""
        loop = asyncio.get_event_loop_policy().new_event_loop()
        yield loop
>       loop.close()

../dvl/venvs/selenium/lib/python3.9/site-packages/pytest_asyncio/plugin.py:210: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.9/asyncio/unix_events.py:58: in close
    super().close()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=False debug=False>

    def close(self):
        if self.is_running():
>           raise RuntimeError("Cannot close a running event loop")
E           RuntimeError: Cannot close a running event loop

/usr/lib/python3.9/asyncio/selector_events.py:89: RuntimeError
---------------------------- Captured log teardown -----------------------------
WARNING  urllib3.connectionpool:connectionpool.py:304 Connection pool is full, discarding connection: 127.0.0.1
=================================== FAILURES ===================================
________________________________ test_selenium _________________________________

kwargs = {'driver': <seleniumwire.webdriver.Chrome (session="cc8ada3562d7d6b901fd27ec469845ba")>}
coro = <coroutine object test_selenium at 0x7f59a0916b40>
task = <Task pending name='Task-2' coro=<test_selenium() running at /home/eagle/tmp/test_selenium.py:23>>

    @functools.wraps(func)
    def inner(**kwargs):
        coro = func(**kwargs)
        if coro is not None:
            task = asyncio.ensure_future(coro, loop=_loop)
            try:
>               _loop.run_until_complete(task)

../dvl/venvs/selenium/lib/python3.9/site-packages/pytest_asyncio/plugin.py:179: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.9/asyncio/base_events.py:618: in run_until_complete
    self._check_running()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=True closed=False debug=False>

    def _check_running(self):
        if self.is_running():
>           raise RuntimeError('This event loop is already running')
E           RuntimeError: This event loop is already running

/usr/lib/python3.9/asyncio/base_events.py:578: RuntimeError
=========================== short test summary info ============================
FAILED test_selenium.py::test_selenium - RuntimeError: This event loop is alr...
ERROR test_selenium.py::test_selenium - RuntimeError: Cannot close a running ...
========================== 1 failed, 1 error in 1.60s ==========================

Running pip install 'selenium-wire<4' and then re-running the test shows no errors.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rracommented, Feb 24, 2021

Confirmed. Everything looks great now – thank you so much!

1reaction
rracommented, Feb 8, 2021

Here’s a new minimal test case that shows the above issue. It appears to be caused by a combination of having another async fixture and running selenium-wire with session scope. (You’ll need to install aiohttp in the virtual env to run this test case; that was the thing I had on hand that made sense as an async fixture.) Removing the session scope makes the problem go away.

import aiohttp
import pytest
from seleniumwire import webdriver


@pytest.fixture(scope="session")
def driver():
    options = webdriver.ChromeOptions()
    options.add_argument("headless")
    options.add_argument("no-sandbox")
    options.add_argument("disable-extensions")
    options.add_argument("incognito")
    driver = webdriver.Chrome(options=options)
    try:
        yield driver
    finally:
        driver.quit()


@pytest.fixture
async def session():
    async with aiohttp.ClientSession() as session:
        yield session 


@pytest.mark.asyncio
async def test_selenium(driver, session) -> None:
    driver.get("https://www.google.com/")
Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest-asyncio breaks on pytest 5.4.2 when subclassing from ...
I am using pytest with pytest-asyncio. I am running the following test: import unittest import pytest class SimpleTest(unittest.TestCase): @ ...
Read more >
Python with Selenium 4 Tutorial: A Complete Guide with ...
A complete tutorial on using Python with Selenium 4 with examples and code. A guide to running web automation testing using Python with ......
Read more >
async function with selenium wait - python - Stack Overflow
I select item on drop-down A, wait for few seconds, so that drop-down B will be populated based on item's value in drop-down...
Read more >
latest PDF - pytest Documentation
pytest discovers all tests following its Conventions for Python test discovery, so it finds both test_ prefixed functions.
Read more >
Intro Testing with Pytest, Selenium and Django - YouTube
In this Pytest, Selenium tutorial we consolidate knowledge learnt from the Pytest and Selenium tutorial series and in introduce working with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found