Skip to content

Commit

Permalink
Use pytest fixtures to setup browser, split up chrome and firefox jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Jun 14, 2024
1 parent ac5bb1c commit 904267c
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 279 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
xvfb-run java -jar selenium-server.jar standalone > selenium-server.log 2>&1 &
timeout 60 bash -c 'while ! wget -O /dev/null -T 1 http://localhost:4444/readyz; do echo waiting for selenium server; sleep 1; done' || (cat selenium-server.log && exit 2)
echo "Selenium server is ready, running tests"
tox -e tests_selenium
tox -e tests_selenium_remote
tests_selenium:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -122,4 +122,5 @@ jobs:

- name: Run Selenium tests
run: |
tox -e tests_selenium -- -n 4;
tox -e tests_selenium_firefox -- -n 4;
tox -e tests_selenium_chrome -- -n 4;
38 changes: 38 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from tests.fake_webapp import start_flask_app
from tests.get_browser import get_browser

import splinter
from splinter import Browser
from splinter.config import Config


class Env:
def __init__(self):
Expand Down Expand Up @@ -75,3 +79,37 @@ def new_browser(browser_name):
return browser

return new_browser


def pytest_addoption(parser):
group = parser.getgroup(
"splinter",
"splinter integration",
)
group.addoption(
"--browser",
help="Name of the browser to use.",
type=str,
choices=list(splinter.browser._DRIVERS.keys()),
dest="browser_name",
)


@pytest.fixture(scope="session")
def browser_name(request) -> str:
return request.config.option.browser_name


@pytest.fixture(scope="session")
def browser_config():
return Config(headless=True)


@pytest.fixture(scope="session")
def browser_kwargs():
return {}


@pytest.fixture(scope="function")
def browser(browser_name, browser_config, browser_kwargs):
return Browser(browser_name, config=browser_config, **browser_kwargs)
4 changes: 2 additions & 2 deletions tests/test_webdriver_chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class TestChromeBrowser(WebDriverTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
config = Config(fullscreen=False)
config = Config(fullscreen=False, headless=True)
request.cls.browser = get_browser("chrome", config=config)
request.addfinalizer(request.cls.browser.quit)

Expand All @@ -26,7 +26,7 @@ def visit_example_app(self, request):
class TestChromeBrowserFullscreen(WebDriverTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
config = Config(fullscreen=True)
config = Config(fullscreen=True, headless=True)
request.cls.browser = get_browser("chrome", config=config)
request.addfinalizer(request.cls.browser.quit)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_webdriver_edge_chromium.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class TestEdgeChromiumBrowser(WebDriverTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
config = Config(fullscreen=False)
config = Config(fullscreen=False, headless=True)
request.cls.browser = get_browser("edge", config=config)
request.addfinalizer(request.cls.browser.quit)

Expand All @@ -26,7 +26,7 @@ def visit_example_app(self, request):
class TestEdgeChromiumBrowserFullscreen(WebDriverTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
config = Config(fullscreen=True)
config = Config(fullscreen=True, headless=True)
request.cls.browser = get_browser("edge", config=config)
request.addfinalizer(request.cls.browser.quit)

Expand Down
63 changes: 6 additions & 57 deletions tests/test_webdriver_firefox.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# Copyright 2013 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import os

import pytest

from .base import get_browser
from .base import WebDriverTests
from .fake_webapp import EXAMPLE_APP
from tests.base import get_browser
from tests.base import WebDriverTests
from tests.fake_webapp import EXAMPLE_APP

from splinter.config import Config


class TestFirefoxBrowser(WebDriverTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
config = Config(fullscreen=False)
config = Config(fullscreen=False, headless=True)
request.cls.browser = get_browser("firefox", config=config)
request.addfinalizer(request.cls.browser.quit)

Expand All @@ -27,59 +25,10 @@ def visit_example_app(self, request):
class TestFirefoxBrowserFullScreen(WebDriverTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
config = Config(fullscreen=True)
config = Config(fullscreen=True, headless=True)
request.cls.browser = get_browser("firefox", config=config)
request.addfinalizer(request.cls.browser.quit)

@pytest.fixture(autouse=True)
def visit_example_app(self):
def visit_example_app(self, request):
self.browser.visit(EXAMPLE_APP)


def test_firefox_create_instance_with_extension(request):
"""Test: Load an extension via selenium.
The dummy extension should add a red border to any web page.
"""
extension_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"dummy_extension",
"borderify-1.0-an+fx.xpi",
)

config = Config(extensions=[extension_path])
browser = get_browser("firefox", config=config)
request.addfinalizer(browser.quit)

browser.visit(EXAMPLE_APP)

elem = browser.find_by_css("body")
elem.is_visible(wait_time=20)
style = elem._element.get_attribute("style")

assert "border: 5px solid red;" == style


def test_preference_set(request):
preferences = {
"dom.max_script_run_time": 213,
"devtools.inspector.enabled": True,
}
browser = get_browser("firefox", profile_preferences=preferences)
request.addfinalizer(browser.quit)

# Rip the preferences out of firefox's config page
browser.visit("about:config")
browser.find_by_id("warningButton").click()
browser.find_by_id("about-config-search").fill("dom.max_script_run_time")
elem = browser.find_by_xpath("//table[@id='prefs']/tr[1]/td[1]/span/span")
assert elem.value == "213"


def test_capabilities_set(request):
browser = get_browser("firefox", capabilities={"pageLoadStrategy": "eager"})
request.addfinalizer(browser.quit)

capabilities = browser.driver.capabilities
assert "pageLoadStrategy" in capabilities
assert "eager" == capabilities.get("pageLoadStrategy")
14 changes: 14 additions & 0 deletions tests/tests_firefox_webdriver/test_firefox_capabilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest


@pytest.fixture(scope="session")
def browser_kwargs():
return {"capabilities": {"pageLoadStrategy": "eager"}}


def test_capabilities_set(request, browser):
request.addfinalizer(browser.quit)

capabilities = browser.driver.capabilities
assert "pageLoadStrategy" in capabilities
assert "eager" == capabilities.get("pageLoadStrategy")
36 changes: 36 additions & 0 deletions tests/tests_firefox_webdriver/test_firefox_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import pathlib

import pytest

from tests.fake_webapp import EXAMPLE_APP

from splinter.config import Config


@pytest.fixture(scope="session")
def browser_config():
extension_path = pathlib.Path(
os.getcwd(), # NOQA PTH109
"tests",
"dummy_extension",
"borderify-1.0-an+fx.xpi",
)

return Config(extensions=[str(extension_path)], headless=True)


def test_firefox_create_instance_with_extension(request, browser):
"""Test: Load an extension via selenium.
The dummy extension should add a red border to any web page.
"""
request.addfinalizer(browser.quit)

browser.visit(EXAMPLE_APP)

elem = browser.find_by_css("body")
elem.is_visible(wait_time=20)

style = elem._element.get_attribute("style")
assert "border: 5px solid red;" == style
23 changes: 23 additions & 0 deletions tests/tests_firefox_webdriver/test_firefox_preferences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest


@pytest.fixture(scope="session")
def browser_kwargs():
prefs = {
"profile_preferences": {
"dom.max_script_run_time": 213,
"devtools.inspector.enabled": True,
},
}
return prefs


def test_preference_set(request, browser):
request.addfinalizer(browser.quit)

# Rip the preferences out of firefox's config page
browser.visit("about:config")
browser.find_by_id("warningButton").click()
browser.find_by_id("about-config-search").fill("dom.max_script_run_time")
elem = browser.find_by_xpath("//table[@id='prefs']/tr[1]/td[1]/span/span")
assert elem.value == "213"
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import pytest
from tests.fake_webapp import EXAMPLE_APP

from .base import supported_browsers
from .fake_webapp import EXAMPLE_APP


@pytest.mark.parametrize("browser_name", supported_browsers)
def test_find_by_css_should_found_an_async_element(get_new_browser, browser_name):
browser = get_new_browser(browser_name)
def test_find_by_css_should_found_an_async_element(browser):
browser.visit(EXAMPLE_APP)

browser.find_by_css(".add-async-element").click()
Expand All @@ -18,9 +13,7 @@ def test_find_by_css_should_found_an_async_element(get_new_browser, browser_name
assert 1 == len(elements)


@pytest.mark.parametrize("browser_name", supported_browsers)
def test_find_by_xpath_should_found_an_async_element(get_new_browser, browser_name):
browser = get_new_browser(browser_name)
def test_find_by_xpath_should_found_an_async_element(browser):
browser.visit(EXAMPLE_APP)

browser.find_by_css(".add-async-element").click()
Expand All @@ -29,9 +22,7 @@ def test_find_by_xpath_should_found_an_async_element(get_new_browser, browser_na
assert 1 == len(elements)


@pytest.mark.parametrize("browser_name", supported_browsers)
def test_find_by_tag_should_found_an_async_element(get_new_browser, browser_name):
browser = get_new_browser(browser_name)
def test_find_by_tag_should_found_an_async_element(browser):
browser.visit(EXAMPLE_APP)

browser.find_by_css(".add-async-element").click()
Expand All @@ -40,9 +31,7 @@ def test_find_by_tag_should_found_an_async_element(get_new_browser, browser_name
assert 1 == len(elements)


@pytest.mark.parametrize("browser_name", supported_browsers)
def test_find_by_id_should_found_an_async_element(get_new_browser, browser_name):
browser = get_new_browser(browser_name)
def test_find_by_id_should_found_an_async_element(browser):
browser.visit(EXAMPLE_APP)

browser.find_by_css(".add-async-element").click()
Expand All @@ -51,9 +40,7 @@ def test_find_by_id_should_found_an_async_element(get_new_browser, browser_name)
assert 1 == len(elements)


@pytest.mark.parametrize("browser_name", supported_browsers)
def test_find_by_name_should_found_an_async_element(get_new_browser, browser_name):
browser = get_new_browser(browser_name)
def test_find_by_name_should_found_an_async_element(browser):
browser.visit(EXAMPLE_APP)

browser.find_by_css(".add-async-element").click()
Expand All @@ -62,9 +49,7 @@ def test_find_by_name_should_found_an_async_element(get_new_browser, browser_nam
assert 1 == len(elements)


@pytest.mark.parametrize("browser_name", supported_browsers)
def test_find_by_value_should_found_an_async_element(get_new_browser, browser_name):
browser = get_new_browser(browser_name)
def test_find_by_value_should_found_an_async_element(browser):
browser.visit(EXAMPLE_APP)

browser.find_by_css(".add-async-element").click()
Expand Down
Loading

0 comments on commit 904267c

Please sign in to comment.