Repository: apify/crawlee-python
File: tests/unit/browsers/test_browser_pool.py (Python)

Code before:

    with pytest.raises(RuntimeError, match='BrowserPool is not active.'):
        await browser_pool.new_page()

    with pytest.raises(RuntimeError, match='BrowserPool is not active.'):
        await browser_pool.new_page_with_each_plugin()

    with pytest.raises(RuntimeError, match='BrowserPool is already active.'):
        async with browser_pool, browser_pool:
            pass

    async with browser_pool:
        assert browser_pool.active is True


async def test_with_plugin_contains_page_options(server_url: URL) -> None:
    plugin = PlaywrightBrowserPlugin(browser_new_context_options={'user_agent': 'My Best User-Agent'})
    async with BrowserPool(plugins=[plugin]) as browser_pool:
        test_page = await browser_pool.new_page()
        await test_page.page.goto(str(server_url / 'user-agent'))
        assert 'My Best User-Agent' in await test_page.page.content()
        await test_page.page.close()


@pytest.mark.parametrize(
    ('page_count', 'equal_browsers'),
    [
        pytest.param(2, True, id='two pages create same browser'),
        pytest.param(1, False, id='each page creates new browser'),
    ],
)
async def test_browser_pool_retire_browser_after_page_count(page_count: int, *, equal_browsers: bool) -> None:
    async with BrowserPool(retire_browser_after_page_count=page_count) as browser_pool:
        test_page = await browser_pool.new_page()
        first_browser = test_page.page.context
        await test_page.page.close()

        test_page = await browser_pool.new_page()
        second_browser = test_page.page.context

        await test_page.page.close()

        if equal_browsers:
            assert first_browser == second_browser
        else:
            assert first_browser != second_browser


Review comment (suggestion):
I'd rename the first parameter to `retire_after_page_count` and the second parameter to something like `expect_equal_browsers`. Also maybe you could use `is` and `is not` instead of equality operator.

Also the `id` values in `pytest.param` are kinda misleading.

Code after:

    with pytest.raises(RuntimeError, match='BrowserPool is not active.'):
        await browser_pool.new_page()

    with pytest.raises(RuntimeError, match='BrowserPool is not active.'):
        await browser_pool.new_page_with_each_plugin()

    with pytest.raises(RuntimeError, match='BrowserPool is already active.'):
        async with browser_pool, browser_pool:
            pass

    async with browser_pool:
        assert browser_pool.active is True


async def test_with_plugin_contains_page_options(server_url: URL) -> None:
    plugin = PlaywrightBrowserPlugin(browser_new_context_options={'user_agent': 'My Best User-Agent'})
    async with BrowserPool(plugins=[plugin]) as browser_pool:
        test_page = await browser_pool.new_page()
        await test_page.page.goto(str(server_url / 'user-agent'))
        assert 'My Best User-Agent' in await test_page.page.content()
        await test_page.page.close()


@pytest.mark.parametrize(
    ('retire_after_page_count', 'expect_equal_browsers'),
    [
        pytest.param(2, True, id='Two pages opened in the same browser'),
        pytest.param(1, False, id='Each page opened in a new browser.'),
    ],
)
async def test_browser_pool_retire_browser_after_page_count(
    retire_after_page_count: int, *, expect_equal_browsers: bool
) -> None:
    async with BrowserPool(retire_browser_after_page_count=retire_after_page_count) as browser_pool:
        test_page = await browser_pool.new_page()
        first_browser = test_page.page.context
        await test_page.page.close()

        test_page = await browser_pool.new_page()
        second_browser = test_page.page.context

        await test_page.page.close()

        if expect_equal_browsers:
            assert first_browser is second_browser
        else:
            assert first_browser is not second_browser