-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[py] Add the ability to use Options classes on Safari
- Loading branch information
1 parent
0b2ab18
commit 4153f72
Showing
3 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# 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. | ||
from typing import Union | ||
import warnings | ||
from selenium.common.exceptions import InvalidArgumentException | ||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | ||
from selenium.webdriver.common.proxy import Proxy | ||
from selenium.webdriver.common.options import ArgOptions | ||
|
||
|
||
class Log(object): | ||
def __init__(self): | ||
self.level = None | ||
|
||
def to_capabilities(self) -> dict: | ||
if self.level: | ||
return {"log": {"level": self.level}} | ||
return {} | ||
|
||
|
||
class Options(ArgOptions): | ||
KEY = "webkit:safariOptions" | ||
|
||
def __init__(self): | ||
super(Options, self).__init__() | ||
self._binary_location = None | ||
self._preferences: dict = {} | ||
self._proxy = None | ||
self.log = Log() | ||
|
||
@property | ||
def binary_location(self): | ||
""" | ||
:Returns: The location of the browser binary otherwise an empty string | ||
""" | ||
return self._binary_location | ||
|
||
@binary_location.setter | ||
def binary_location(self, value): | ||
""" | ||
Allows you to set the browser binary to launch | ||
:Args: | ||
- value : path to the browser binary | ||
""" | ||
self._binary_location = value | ||
|
||
@property | ||
def accept_insecure_certs(self) -> bool: | ||
return self._caps.get('acceptInsecureCerts') | ||
|
||
@accept_insecure_certs.setter | ||
def accept_insecure_certs(self, value: bool): | ||
self._caps['acceptInsecureCerts'] = value | ||
|
||
@property | ||
def page_load_strategy(self) -> str: | ||
return self._caps["pageLoadStrategy"] | ||
|
||
@page_load_strategy.setter | ||
def page_load_strategy(self, strategy: str): | ||
if strategy in ["normal", "eager", "none"]: | ||
self.set_capability("pageLoadStrategy", strategy) | ||
else: | ||
raise ValueError("Strategy can only be one of the following: normal, eager, none") | ||
|
||
def to_capabilities(self) -> dict: | ||
"""Marshals the options to an desired capabilities object. | ||
""" | ||
# This intentionally looks at the internal properties | ||
# so if a binary or profile has _not_ been set, | ||
# it will defer to geckodriver to find the system Firefox | ||
# and generate a fresh profile. | ||
caps = self._caps | ||
opts = {} | ||
|
||
if self._arguments: | ||
opts["args"] = self._arguments | ||
if self._binary_location: | ||
opts["binary"] = self._binary_location | ||
opts.update(self.log.to_capabilities()) | ||
|
||
if opts: | ||
caps[Options.KEY] = opts | ||
|
||
return caps | ||
|
||
@property | ||
def default_capabilities(self) -> dict: | ||
return DesiredCapabilities.SAFARI.copy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
py/test/unit/selenium/webdriver/safari/safari_options_tests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# 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 pytest | ||
|
||
from selenium.webdriver.safari.options import Options | ||
|
||
|
||
@pytest.fixture | ||
def options(): | ||
return Options() | ||
|
||
|
||
def test_set_binary_location(options): | ||
options.binary_location = '/foo/bar' | ||
assert options._binary_location == '/foo/bar' | ||
|
||
|
||
def test_get_binary_location(options): | ||
options._binary_location = '/foo/bar' | ||
assert options.binary_location == '/foo/bar' | ||
|
||
|
||
def test_creates_capabilities(options): | ||
options._arguments = ['foo'] | ||
options._binary_location = '/bar' | ||
caps = options.to_capabilities() | ||
opts = caps.get(Options.KEY) | ||
assert opts | ||
assert 'foo' in opts['args'] | ||
assert opts['binary'] == '/bar' | ||
|
||
|
||
def test_starts_with_default_capabilities(options): | ||
from selenium.webdriver import DesiredCapabilities | ||
caps = DesiredCapabilities.SAFARI.copy() | ||
caps.update({"pageLoadStrategy": "normal"}) | ||
assert options._caps == caps | ||
|
||
|
||
def test_is_a_baseoptions(options): | ||
from selenium.webdriver.common.options import BaseOptions | ||
assert isinstance(options, BaseOptions) |