File: alert.py

package info (click to toggle)
python-selenium 3.14.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,024 kB
  • sloc: python: 5,165; makefile: 7
file content (105 lines) | stat: -rw-r--r-- 3,099 bytes parent folder | download | duplicates (2)
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
# 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.

"""
The Alert implementation.
"""

from selenium.webdriver.common.utils import keys_to_typing
from selenium.webdriver.remote.command import Command


class Alert(object):
    """
    Allows to work with alerts.

    Use this class to interact with alert prompts.  It contains methods for dismissing,
    accepting, inputting, and getting text from alert prompts.

    Accepting / Dismissing alert prompts::

        Alert(driver).accept()
        Alert(driver).dismiss()

    Inputting a value into an alert prompt:

        name_prompt = Alert(driver)
        name_prompt.send_keys("Willian Shakesphere")
        name_prompt.accept()


    Reading a the text of a prompt for verification:

        alert_text = Alert(driver).text
        self.assertEqual("Do you wish to quit?", alert_text)

    """

    def __init__(self, driver):
        """
        Creates a new Alert.

        :Args:
         - driver: The WebDriver instance which performs user actions.
        """
        self.driver = driver

    @property
    def text(self):
        """
        Gets the text of the Alert.
        """
        if self.driver.w3c:
            return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
        else:
            return self.driver.execute(Command.GET_ALERT_TEXT)["value"]

    def dismiss(self):
        """
        Dismisses the alert available.
        """
        if self.driver.w3c:
            self.driver.execute(Command.W3C_DISMISS_ALERT)
        else:
            self.driver.execute(Command.DISMISS_ALERT)

    def accept(self):
        """
        Accepts the alert available.

        Usage::
        Alert(driver).accept() # Confirm a alert dialog.
        """
        if self.driver.w3c:
            self.driver.execute(Command.W3C_ACCEPT_ALERT)
        else:
            self.driver.execute(Command.ACCEPT_ALERT)

    def send_keys(self, keysToSend):
        """
        Send Keys to the Alert.

        :Args:
         - keysToSend: The text to be sent to Alert.


        """
        if self.driver.w3c:
            self.driver.execute(Command.W3C_SET_ALERT_VALUE, {'value': keys_to_typing(keysToSend),
                                                              'text': keysToSend})
        else:
            self.driver.execute(Command.SET_ALERT_VALUE, {'text': keysToSend})