1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
import platform
import socket
import subprocess
import time
from test.selenium.webdriver.common.network import get_lan_ip
from test.selenium.webdriver.common.webserver import SimpleWebServer
from urllib.request import urlopen
import pytest
from selenium import webdriver
drivers = (
"chrome",
"edge",
"firefox",
"ie",
"remote",
"safari",
"webkitgtk",
"wpewebkit",
)
def pytest_addoption(parser):
parser.addoption(
"--driver",
action="append",
choices=drivers,
dest="drivers",
metavar="DRIVER",
help="driver to run tests against ({})".format(", ".join(drivers)),
)
parser.addoption(
"--browser-binary",
action="store",
dest="binary",
help="location of the browser binary",
)
parser.addoption(
"--driver-binary",
action="store",
dest="executable",
help="location of the service executable binary",
)
parser.addoption(
"--browser-args",
action="store",
dest="args",
help="arguments to start the browser with",
)
parser.addoption(
"--headless",
action="store",
dest="headless",
help="Allow tests to run in headless",
)
parser.addoption(
"--use-lan-ip",
action="store_true",
dest="use_lan_ip",
help="Whether to start test server with lan ip instead of localhost",
)
parser.addoption(
"--bidi",
action="store",
dest="bidi",
metavar="BIDI",
help="Whether to enable BiDi support",
)
def pytest_ignore_collect(path, config):
drivers_opt = config.getoption("drivers")
_drivers = set(drivers).difference(drivers_opt or drivers)
if drivers_opt:
_drivers.add("unit")
parts = path.dirname.split(os.path.sep)
return len([d for d in _drivers if d.lower() in parts]) > 0
driver_instance = None
@pytest.fixture(scope="function")
def driver(request):
kwargs = {}
# browser can be changed with `--driver=firefox` as an argument or to addopts in pytest.ini
driver_class = getattr(request, "param", "Chrome").capitalize()
# skip tests if not available on the platform
_platform = platform.system()
if driver_class == "Safari" and _platform != "Darwin":
pytest.skip("Safari tests can only run on an Apple OS")
if (driver_class == "Ie") and _platform != "Windows":
pytest.skip("IE and EdgeHTML Tests can only run on Windows")
if "WebKit" in driver_class and _platform != "Linux":
pytest.skip("Webkit tests can only run on Linux")
# conditionally mark tests as expected to fail based on driver
marker = request.node.get_closest_marker(f"xfail_{driver_class.lower()}")
if marker is not None:
if "run" in marker.kwargs:
if marker.kwargs["run"] is False:
pytest.skip()
yield
return
if "raises" in marker.kwargs:
marker.kwargs.pop("raises")
pytest.xfail(**marker.kwargs)
def fin():
global driver_instance
if driver_instance is not None:
driver_instance.quit()
driver_instance = None
request.addfinalizer(fin)
driver_path = request.config.option.executable
options = None
global driver_instance
if driver_instance is None:
if driver_class == "Firefox":
options = get_options(driver_class, request.config)
if driver_class == "Chrome":
options = get_options(driver_class, request.config)
if driver_class == "Remote":
options = get_options("Firefox", request.config) or webdriver.FirefoxOptions()
options.set_capability("moz:firefoxOptions", {})
options.enable_downloads = True
if driver_class == "WebKitGTK":
options = get_options(driver_class, request.config)
if driver_class == "Edge":
options = get_options(driver_class, request.config)
if driver_class.lower() == "wpewebkit":
driver_class = "WPEWebKit"
options = get_options(driver_class, request.config)
if driver_path is not None:
kwargs["service"] = get_service(driver_class, driver_path)
if options is not None:
kwargs["options"] = options
driver_instance = getattr(webdriver, driver_class)(**kwargs)
yield driver_instance
# Close the browser after BiDi tests. Those make event subscriptions
# and doesn't seems to be stable enough, causing the flakiness of the
# subsequent tests.
# Remove this when BiDi implementation and API is stable.
if bool(request.config.option.bidi):
def fin():
global driver_instance
if driver_instance is not None:
driver_instance.quit()
driver_instance = None
request.addfinalizer(fin)
if request.node.get_closest_marker("no_driver_after_test"):
driver_instance = None
def get_options(driver_class, config):
browser_path = config.option.binary
browser_args = config.option.args
headless = bool(config.option.headless)
bidi = bool(config.option.bidi)
options = None
if browser_path or browser_args:
if not options:
options = getattr(webdriver, f"{driver_class}Options")()
if driver_class == "WebKitGTK":
options.overlay_scrollbars_enabled = False
if browser_path is not None:
options.binary_location = browser_path.strip("'")
if browser_args is not None:
for arg in browser_args.split():
options.add_argument(arg)
if headless:
if not options:
options = getattr(webdriver, f"{driver_class}Options")()
if driver_class == "Chrome" or driver_class == "Edge":
options.add_argument("--headless=new")
if driver_class == "Firefox":
options.add_argument("-headless")
if bidi:
if not options:
options = getattr(webdriver, f"{driver_class}Options")()
options.web_socket_url = True
options.unhandled_prompt_behavior = "ignore"
return options
def get_service(driver_class, executable):
# Let the default behaviour be used if we don't set the driver executable
if not executable:
return None
module = getattr(webdriver, driver_class.lower())
service = module.service.Service(executable_path=executable)
return service
@pytest.fixture(scope="session", autouse=True)
def stop_driver(request):
def fin():
global driver_instance
if driver_instance is not None:
driver_instance.quit()
driver_instance = None
request.addfinalizer(fin)
def pytest_exception_interact(node, call, report):
if report.failed:
global driver_instance
if driver_instance is not None:
driver_instance.quit()
driver_instance = None
@pytest.fixture
def pages(driver, webserver):
class Pages:
def url(self, name, localhost=False):
return webserver.where_is(name, localhost)
def load(self, name):
driver.get(self.url(name))
return Pages()
@pytest.fixture(autouse=True, scope="session")
def server(request):
drivers = request.config.getoption("drivers")
if drivers is None or "remote" not in drivers:
yield None
return
_host = "localhost"
_port = 4444
_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"java/src/org/openqa/selenium/grid/selenium_server_deploy.jar",
)
def wait_for_server(url, timeout):
start = time.time()
while time.time() - start < timeout:
try:
urlopen(url)
return 1
except OSError:
time.sleep(0.2)
return 0
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
url = f"http://{_host}:{_port}/status"
try:
_socket.connect((_host, _port))
print(
"The remote driver server is already running or something else"
"is using port {}, continuing...".format(_port)
)
except Exception:
print("Starting the Selenium server")
process = subprocess.Popen(
[
"java",
"-jar",
_path,
"standalone",
"--port",
"4444",
"--selenium-manager",
"true",
"--enable-managed-downloads",
"true",
]
)
print(f"Selenium server running as process: {process.pid}")
assert wait_for_server(url, 10), f"Timed out waiting for Selenium server at {url}"
print("Selenium server is ready")
yield process
process.terminate()
process.wait()
print("Selenium server has been terminated")
@pytest.fixture(autouse=True, scope="session")
def webserver(request):
host = get_lan_ip() if request.config.getoption("use_lan_ip") else None
webserver = SimpleWebServer(host=host)
webserver.start()
yield webserver
webserver.stop()
@pytest.fixture
def edge_service():
from selenium.webdriver.edge.service import Service as EdgeService
return EdgeService
@pytest.fixture(scope="function")
def driver_executable(request):
return request.config.option.executable
@pytest.fixture(scope="function")
def clean_service(request):
try:
driver_class = request.config.option.drivers[0].capitalize()
except AttributeError:
raise Exception("This test requires a --driver to be specified.")
yield get_service(driver_class, request.config.option.executable)
@pytest.fixture(scope="function")
def clean_driver(request):
try:
driver_class = request.config.option.drivers[0].capitalize()
except AttributeError:
raise Exception("This test requires a --driver to be specified.")
driver_reference = getattr(webdriver, driver_class)
yield driver_reference
if request.node.get_closest_marker("no_driver_after_test"):
driver_reference = None
|