Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions dotnet/test/testing.nunit/Environment/EnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public class EnvironmentManager
{
private static readonly Lazy<EnvironmentManager> instance = new(() => new EnvironmentManager());

private static readonly Dictionary<Browser, string> driverPathEnvironmentVariables = new()
{
[Browser.Chrome] = "SE_CHROMEDRIVER",
[Browser.Edge] = "SE_EDGEDRIVER",
[Browser.Firefox] = "SE_GECKODRIVER",
[Browser.IE] = "SE_IEDRIVER",
[Browser.Safari] = "SE_SAFARIDRIVER",
};

private EnvironmentManager()
{
string dataFilePath;
Expand Down Expand Up @@ -73,6 +82,13 @@ private EnvironmentManager()
Browser = driverConfig.BrowserValue;
RemoteCapabilities = driverConfig.RemoteCapabilities;

// Services the tests build themselves then resolve the pinned driver instead of invoking Selenium Manager.
if (!string.IsNullOrEmpty(driverServiceLocation)
&& driverPathEnvironmentVariables.TryGetValue(Browser, out string driverVariable))
{
System.Environment.SetEnvironmentVariable(driverVariable, driverServiceLocation);
}

WebServer = new AppServer();
WebServer.StartAsync().GetAwaiter().GetResult();

Expand Down
6 changes: 5 additions & 1 deletion dotnet/test/webdriver/Firefox/FirefoxCommandContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Testing.NUnit.Environment;

namespace OpenQA.Selenium.Tests.Firefox;

Expand All @@ -34,7 +35,10 @@ public void ShouldGetAndSetContext()
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.AllowSystemAccess = true;

FirefoxDriver driver = new FirefoxDriver(service);
FirefoxOptions options = new FirefoxOptions();
options.BinaryLocation = EnvironmentManager.Instance.BrowserLocation;

FirefoxDriver driver = new FirefoxDriver(service, options);
try
{
Assert.That(driver.GetContext(), Is.EqualTo(FirefoxCommandContext.Content));
Expand Down
3 changes: 3 additions & 0 deletions dotnet/test/webdriver/Firefox/FirefoxDriverServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.IO;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Internal.Logging;
using OpenQA.Selenium.Testing.NUnit.Environment;

namespace OpenQA.Selenium.Tests.Firefox;

Expand Down Expand Up @@ -56,6 +57,7 @@ public void TearDown()
public void ShouldRedirectGeckoDriverLogsToFile()
{
FirefoxOptions options = new FirefoxOptions();
options.BinaryLocation = EnvironmentManager.Instance.BrowserLocation;
string logPath = Path.GetTempFileName();
options.LogLevel = FirefoxDriverLogLevel.Info;

Expand All @@ -82,6 +84,7 @@ public void ShouldRedirectGeckoDriverLogsToConsole()
{
Log.SetLevel(LogEventLevel.Trace).Handlers.Add(testLogHandler);
FirefoxOptions options = new FirefoxOptions();
options.BinaryLocation = EnvironmentManager.Instance.BrowserLocation;
options.LogLevel = FirefoxDriverLogLevel.Info;

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
Expand Down
18 changes: 18 additions & 0 deletions py/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ def _pinned_grid_args(config):
]


# Maps the test driver name to the env var its Service consults before invoking Selenium Manager.
_DRIVER_PATH_ENV_KEYS = {
"chrome": "SE_CHROMEDRIVER",
"edge": "SE_EDGEDRIVER",
"firefox": "SE_GECKODRIVER",
"ie": "SE_IEDRIVER",
"safari": "SE_SAFARIDRIVER",
}


def pytest_configure(config):
"""Export the pinned driver so services the tests build without a path skip Selenium Manager."""
executable = config.option.executable
driver = next((d for d in config.option.drivers or [] if d.lower() in _DRIVER_PATH_ENV_KEYS), None)
if driver and executable:
os.environ[_DRIVER_PATH_ENV_KEYS[driver.lower()]] = _resolve_bazel_path(executable).strip("'")
Comment thread
qodo-code-review[bot] marked this conversation as resolved.


def get_extensions_location():
"""Locate the test extensions directory.

Expand Down
6 changes: 2 additions & 4 deletions py/test/selenium/webdriver/chrome/chrome_service_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ class TestChromeDriverService:
service_path = "/path/to/chromedriver"

@pytest.fixture(autouse=True)
def setup_and_teardown(self):
os.environ["SE_CHROMEDRIVER"] = self.service_path
yield
os.environ.pop("SE_CHROMEDRIVER", None)
def pin_driver_env(self, monkeypatch):
monkeypatch.setenv("SE_CHROMEDRIVER", self.service_path)

def test_uses_path_from_env_variable(self, service):
assert "chromedriver" in service.path
Expand Down
6 changes: 2 additions & 4 deletions py/test/selenium/webdriver/edge/edge_service_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ class TestEdgeDriverService:
service_path = "/path/to/msedgedriver"

@pytest.fixture(autouse=True)
def setup_and_teardown(self):
os.environ["SE_EDGEDRIVER"] = self.service_path
yield
os.environ.pop("SE_EDGEDRIVER", None)
def pin_driver_env(self, monkeypatch):
monkeypatch.setenv("SE_EDGEDRIVER", self.service_path)

def test_uses_path_from_env_variable(self, service):
assert "msedgedriver" in service.path
Expand Down
6 changes: 2 additions & 4 deletions py/test/selenium/webdriver/firefox/firefox_context_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@
import pytest

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service


@pytest.fixture
def driver(request):
def driver(request, clean_options):
Comment thread
titusfortner marked this conversation as resolved.
if request.config.getoption("remote"):
pytest.skip("system access cannot be granted per-session on Grid")
options = Options()
service = Service(service_args=["--allow-system-access"])
driver = Firefox(options=options, service=service)
driver = Firefox(options=clean_options, service=service)
yield driver
driver.quit()

Expand Down
26 changes: 11 additions & 15 deletions py/test/selenium/webdriver/firefox/firefox_service_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from selenium.common.exceptions import SessionNotCreatedException
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service


Expand All @@ -34,11 +33,11 @@ def _skip_on_grid(request):
pytest.skip("driver-service behaviour is only exercised with a local driver")


def test_log_output_as_filename(recwarn) -> None:
def test_log_output_as_filename(recwarn, clean_options) -> None:
log_file = "geckodriver.log"
service = Service(log_output=log_file)
try:
driver = Firefox(service=service)
driver = Firefox(service=service, options=clean_options)
assert len(recwarn) == 0
with open(log_file) as fp:
assert "geckodriver\tINFO\tListening" in fp.readline()
Expand All @@ -47,12 +46,12 @@ def test_log_output_as_filename(recwarn) -> None:
os.remove(log_file)


def test_log_output_as_file() -> None:
def test_log_output_as_file(clean_options) -> None:
log_name = "geckodriver.log"
log_file = open(log_name, "w", encoding="utf-8")
service = Service(log_output=log_file)
try:
driver = Firefox(service=service)
driver = Firefox(service=service, options=clean_options)
with open(log_name) as fp:
assert "geckodriver\tINFO\tListening" in fp.readline()
finally:
Expand All @@ -61,21 +60,20 @@ def test_log_output_as_file() -> None:
os.remove(log_name)


def test_log_output_as_stdout(capfd) -> None:
def test_log_output_as_stdout(capfd, clean_options) -> None:
service = Service(log_output=subprocess.STDOUT)
driver = Firefox(service=service)
driver = Firefox(service=service, options=clean_options)

out, err = capfd.readouterr()
assert "geckodriver\tINFO\tListening" in out
driver.quit()


def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None:
options = Options()
options.add_argument("-profile=/no/such/location")
def test_driver_is_stopped_if_browser_cant_start(clean_driver, clean_options) -> None:
clean_options.add_argument("-profile=/no/such/location")
service = Service()
with pytest.raises(SessionNotCreatedException):
clean_driver(options=options, service=service)
clean_driver(options=clean_options, service=service)
assert not service.is_connectable()
assert service.process.poll() is not None

Expand Down Expand Up @@ -109,10 +107,8 @@ class TestGeckoDriverService:
service_path = "/path/to/geckodriver"

@pytest.fixture(autouse=True)
def setup_and_teardown(self):
os.environ["SE_GECKODRIVER"] = self.service_path
yield
os.environ.pop("SE_GECKODRIVER", None)
def pin_driver_env(self, monkeypatch):
monkeypatch.setenv("SE_GECKODRIVER", self.service_path)

def test_uses_path_from_env_variable(self, service):
assert "geckodriver" in service.path
Expand Down
7 changes: 2 additions & 5 deletions py/test/selenium/webdriver/safari/safari_service_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# specific language governing permissions and limitations
# under the License.

import os
import sys
from unittest.mock import patch

Expand All @@ -34,10 +33,8 @@ class TestSafariDriverService:
service_path = "/path/to/safaridriver"

@pytest.fixture(autouse=True)
def setup_and_teardown(self):
os.environ["SE_SAFARIDRIVER"] = self.service_path
yield
os.environ.pop("SE_SAFARIDRIVER", None)
def pin_driver_env(self, monkeypatch):
monkeypatch.setenv("SE_SAFARIDRIVER", self.service_path)

def test_uses_path_from_env_variable(self, service):
assert "safaridriver" in service.path
Expand Down
6 changes: 0 additions & 6 deletions rb/spec/integration/selenium/webdriver/chrome/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ module Chrome

after { service_manager.stop }

it 'auto uses chromedriver' do
service.executable_path = DriverFinder.new(nil, described_class.new).driver_path

expect(service_manager.uri).to be_a(URI)
end

it 'can be started outside driver' do
expect(service_manager.uri).to be_a(URI)
end
Expand Down
10 changes: 7 additions & 3 deletions rb/spec/integration/selenium/webdriver/driver_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ module WebDriver
expect { Platform.assert_executable(driver_finder.driver_path) }.not_to raise_error
end

it 'resolves an executable browser path' do
it 'resolves an executable browser path',
skip_if: {pinned: true, reason: 'pinned runs supply the browser through options'} do
expect { Platform.assert_executable(driver_finder.browser_path) }.not_to raise_error
end

it 'downloads the driver into the Selenium cache',
pending_if: {browser: :safari, reason: 'driver ships with OS'} do
pending_if: {browser: :safari, reason: 'driver ships with OS'},
skip_if: {pinned: true, reason: 'pinned runs do not use Selenium Manager'} do
Dir.mktmpdir('se-cache') do |cache_dir|
originals = {'SE_CACHE_PATH' => ENV.fetch('SE_CACHE_PATH', nil),
'SE_SKIP_DRIVER_IN_PATH' => ENV.fetch('SE_SKIP_DRIVER_IN_PATH', nil)}
Expand All @@ -54,7 +56,8 @@ module WebDriver
it 'downloads the browser into the Selenium cache',
pending_if: [{browser: :safari, reason: 'browser ships with OS'},
{browser: :edge, platform: :windows,
reason: 'Edge MSI installer always writes to system path'}] do
reason: 'Edge MSI installer always writes to system path'}],
skip_if: {pinned: true, reason: 'pinned runs do not use Selenium Manager'} do
Dir.mktmpdir('se-cache') do |cache_dir|
originals = {'SE_CACHE_PATH' => ENV.fetch('SE_CACHE_PATH', nil),
'SE_FORCE_BROWSER_DOWNLOAD' => ENV.fetch('SE_FORCE_BROWSER_DOWNLOAD', nil)}
Expand All @@ -68,6 +71,7 @@ module WebDriver
end

it 'resolves the browser to its system install location',
skip_if: {pinned: true, reason: 'pinned runs do not use Selenium Manager'},
skip_unless: [{browser: :safari},
{browser: :edge, platform: :windows}] do
Dir.mktmpdir('se-cache') do |cache_dir|
Expand Down
6 changes: 0 additions & 6 deletions rb/spec/integration/selenium/webdriver/edge/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ module Edge

after { service_manager.stop }

it 'auto uses edgedriver' do
service.executable_path = DriverFinder.new(nil, described_class.new).driver_path

expect(service_manager.uri).to be_a(URI)
end

it 'can be started outside driver' do
expect(service_manager.uri).to be_a(URI)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ module Firefox

after { service_manager.stop }

it 'auto uses geckodriver' do
service.executable_path = DriverFinder.new(nil, described_class.new).driver_path

expect(service_manager.uri).to be_a(URI)
end

it 'can be started outside driver' do
expect(service_manager.uri).to be_a(URI)
end
Expand Down
6 changes: 0 additions & 6 deletions rb/spec/integration/selenium/webdriver/ie/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ module IE

after { service_manager.stop }

it 'auto uses iedriver' do
service.executable_path = DriverFinder.new(nil, described_class.new).driver_path

expect(service_manager.uri).to be_a(URI)
end

it 'can be started outside driver' do
expect(service_manager.uri).to be_a(URI)
end
Expand Down
6 changes: 0 additions & 6 deletions rb/spec/integration/selenium/webdriver/safari/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ module Safari

after { service_manager.stop }

it 'auto uses safaridriver' do
service.executable_path = DriverFinder.new(nil, described_class.new).driver_path

expect(service_manager.uri).to be_a(URI)
end

it 'can be started outside driver' do
expect(service_manager.uri).to be_a(URI)
end
Expand Down
1 change: 1 addition & 0 deletions rb/spec/integration/selenium/webdriver/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def create_guards(example)
guards.add_condition(:headless, !ENV['HEADLESS'].nil?)
guards.add_condition(:bidi, !ENV['WEBDRIVER_BIDI'].nil?)
guards.add_condition(:rbe, GlobalTestEnv.rbe?)
guards.add_condition(:pinned, !GlobalTestEnv.driver_path.nil?)
guards.add_condition(:version, GlobalTestEnv.browser_version)
guards
end
Expand Down
Loading
Loading