Path: blob/trunk/py/test/unit/selenium/webdriver/remote/new_session_tests.py
4093 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from importlib import import_module1819import pytest2021from selenium.webdriver.chrome.options import Options as ChromeOptions22from selenium.webdriver.common.options import ArgOptions, PageLoadStrategy23from selenium.webdriver.common.proxy import Proxy, ProxyType24from selenium.webdriver.remote import webdriver25from selenium.webdriver.remote.command import Command26from selenium.webdriver.remote.webdriver import WebDriver272829def test_converts_proxy_type_value_to_lowercase_for_w3c(mocker):30mock = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute")31w3c_caps = {"pageLoadStrategy": "normal", "proxy": {"proxyType": "manual", "httpProxy": "foo"}}32options = ArgOptions()33proxy = Proxy({"proxyType": ProxyType.MANUAL, "httpProxy": "foo"})34options.proxy = proxy35WebDriver(options=options)36expected_params = {"capabilities": {"firstMatch": [{}], "alwaysMatch": w3c_caps}}37mock.assert_called_with(Command.NEW_SESSION, expected_params)383940def test_works_as_context_manager(mocker):41mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.execute")42quit_ = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.quit")4344with WebDriver(options=ChromeOptions()) as driver:45assert isinstance(driver, WebDriver)4647assert quit_.call_count == 1484950@pytest.mark.parametrize("browser_name", ["firefox", "chrome", "ie"])51def test_acepts_options_to_remote_driver(mocker, browser_name):52options = import_module(f"selenium.webdriver.{browser_name}.options")53mock = mocker.patch("selenium.webdriver.remote.webdriver.WebDriver.start_session")5455opts = options.Options()56opts.add_argument("foo")5758WebDriver(options=opts)5960expected_caps = opts.to_capabilities()61mock.assert_called_with(expected_caps)626364def test_always_match_if_2_of_the_same_options():65from selenium.webdriver.chrome.options import Options as ChromeOptions66from selenium.webdriver.chrome.options import Options as ChromeOptions26768co1 = ChromeOptions()69co1.add_argument("foo")70co2 = ChromeOptions2()71co2.add_argument("bar")7273expected = {74"capabilities": {75"alwaysMatch": {76"browserName": "chrome",77"pageLoadStrategy": PageLoadStrategy.normal,78},79"firstMatch": [80{"goog:chromeOptions": {"args": ["foo"], "extensions": []}},81{"goog:chromeOptions": {"args": ["bar"], "extensions": []}},82],83}84}85result = webdriver.create_matches([co1, co2])86assert expected == result878889def test_first_match_when_2_different_option_types():90from selenium.webdriver.chrome.options import Options as ChromeOptions91from selenium.webdriver.firefox.options import Options as FirefoxOptions9293expected = {94"capabilities": {95"alwaysMatch": {"pageLoadStrategy": PageLoadStrategy.normal},96"firstMatch": [97{"browserName": "chrome", "goog:chromeOptions": {"extensions": [], "args": []}},98{99"browserName": "firefox",100"acceptInsecureCerts": True,101"moz:debuggerAddress": True,102"moz:firefoxOptions": {"args": ["foo"], "prefs": {"remote.active-protocols": 1}},103},104],105}106}107108firefox_options = FirefoxOptions()109firefox_options.add_argument("foo")110result = webdriver.create_matches([ChromeOptions(), firefox_options])111assert expected == result112113114