File: mn_options_tests.py

package info (click to toggle)
python-selenium 4.24.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,404 kB
  • sloc: python: 14,901; javascript: 2,347; makefile: 124; sh: 52
file content (132 lines) | stat: -rw-r--r-- 4,823 bytes parent folder | download
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
# 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.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.options import PageLoadStrategy
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.options import Log
from selenium.webdriver.firefox.options import Options


@pytest.fixture
def driver_kwargs(driver_kwargs):
    driver_kwargs["options"] = Options()
    return driver_kwargs


class TestIntegration:
    def test_we_can_pass_options(self, driver, pages):
        pages.load("formPage.html")
        driver.find_element(By.ID, "cheese")


class TestUnit:
    def test_ctor(self):
        opts = Options()
        assert opts._binary is None
        assert not opts._preferences
        assert opts._profile is None
        assert not opts._arguments
        assert isinstance(opts.log, Log)

    def test_binary(self):
        opts = Options()
        assert opts.binary is None

        other_binary = FirefoxBinary()
        assert other_binary != opts.binary
        opts.binary = other_binary
        assert other_binary == opts.binary

        path = "/path/to/binary"
        opts.binary = path
        assert isinstance(opts.binary, FirefoxBinary)
        assert opts.binary._start_cmd == path

    def test_prefs(self):
        opts = Options()
        assert len(opts.preferences) == 0
        assert isinstance(opts.preferences, dict)

        opts.set_preference("spam", "ham")
        assert len(opts.preferences) == 1
        opts.set_preference("eggs", True)
        assert len(opts.preferences) == 2
        opts.set_preference("spam", "spam")
        assert len(opts.preferences) == 2
        assert opts.preferences == {"spam": "spam", "eggs": True}

    def test_profile(self, tmpdir_factory):
        opts = Options()
        assert opts.profile is None

        other_profile = FirefoxProfile()
        assert other_profile != opts.profile
        opts.profile = other_profile
        assert other_profile == opts.profile

        opts.profile = str(tmpdir_factory.mktemp("profile"))
        assert isinstance(opts.profile, FirefoxProfile)

    def test_arguments(self):
        opts = Options()
        assert len(opts.arguments) == 0

        opts.add_argument("--foo")
        assert len(opts.arguments) == 1
        opts.arguments.append("--bar")
        assert len(opts.arguments) == 2
        assert opts.arguments == ["--foo", "--bar"]

    def test_to_capabilities(self):
        opts = Options()
        firefox_caps = DesiredCapabilities.FIREFOX.copy()
        firefox_caps.update({"pageLoadStrategy": PageLoadStrategy.normal})
        assert opts.to_capabilities() == firefox_caps

        profile = FirefoxProfile()
        opts.profile = profile
        caps = opts.to_capabilities()
        assert "moz:firefoxOptions" in caps
        assert "profile" in caps["moz:firefoxOptions"]
        assert isinstance(caps["moz:firefoxOptions"]["profile"], str)
        assert caps["moz:firefoxOptions"]["profile"] == profile.encoded

        opts.add_argument("--foo")
        caps = opts.to_capabilities()
        assert "moz:firefoxOptions" in caps
        assert "args" in caps["moz:firefoxOptions"]
        assert caps["moz:firefoxOptions"]["args"] == ["--foo"]

        binary = FirefoxBinary()
        opts.binary = binary
        caps = opts.to_capabilities()
        assert "moz:firefoxOptions" in caps
        assert "binary" in caps["moz:firefoxOptions"]
        assert isinstance(caps["moz:firefoxOptions"]["binary"], str)
        assert caps["moz:firefoxOptions"]["binary"] == binary._start_cmd

        opts.set_preference("spam", "ham")
        caps = opts.to_capabilities()
        assert "moz:firefoxOptions" in caps
        assert "prefs" in caps["moz:firefoxOptions"]
        assert isinstance(caps["moz:firefoxOptions"]["prefs"], dict)
        assert caps["moz:firefoxOptions"]["prefs"]["spam"] == "ham"