Robot Framework SeleniumLibrary
alert.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 from selenium.common.exceptions import WebDriverException
18 from selenium.webdriver.support import expected_conditions as EC
19 from selenium.webdriver.support.ui import WebDriverWait
20 
21 from SeleniumLibrary.base import keyword, LibraryComponent
22 from SeleniumLibrary.utils import is_truthy, secs_to_timestr
23 
24 
26  ACCEPT = 'ACCEPT'
27  DISMISS = 'DISMISS'
28  LEAVE = 'LEAVE'
29 
32  _next_alert_action = ACCEPT
33 
34  @keyword
35 
45  def input_text_into_alert(self, text, action=ACCEPT, timeout=None):
46  alert = self._wait_alert_wait_alert(timeout)
47  alert.send_keys(text)
48  self._handle_alert_handle_alert(alert, action)
49 
50  @keyword
51 
65  def alert_should_be_present(self, text='', action=ACCEPT, timeout=None):
66  message = self.handle_alerthandle_alert(action, timeout)
67  if text and text != message:
68  raise AssertionError("Alert message should have been '%s' but it "
69  "was '%s'." % (text, message))
70 
71  @keyword
72 
86  def alert_should_not_be_present(self, action=ACCEPT, timeout=0):
87  try:
88  alert = self._wait_alert_wait_alert(timeout)
89  except AssertionError:
90  return
91  text = self._handle_alert_handle_alert(alert, action)
92  raise AssertionError("Alert with message '%s' present." % text)
93 
94  @keyword
95 
119  def handle_alert(self, action=ACCEPT, timeout=None):
120  alert = self._wait_alert_wait_alert(timeout)
121  return self._handle_alert_handle_alert(alert, action)
122 
123  def _handle_alert(self, alert, action):
124  action = action.upper()
125  text = ' '.join(alert.text.splitlines())
126  if action == self.ACCEPTACCEPT:
127  alert.accept()
128  elif action == self.DISMISSDISMISS:
129  alert.dismiss()
130  elif action != self.LEAVELEAVE:
131  raise ValueError("Invalid alert action '%s'." % action)
132  return text
133 
134  def _wait_alert(self, timeout=None):
135  timeout = self.get_timeoutget_timeout(timeout)
136  wait = WebDriverWait(self.driverdriverdriver, timeout)
137  try:
138  return wait.until(EC.alert_is_present())
139  except WebDriverException:
140  raise AssertionError('Alert not found in %s.'
141  % secs_to_timestr(timeout))
def input_text_into_alert(self, text, action=ACCEPT, timeout=None)
Types the given text into an input field in an alert.
Definition: alert.py:45
def handle_alert(self, action=ACCEPT, timeout=None)
Handles the current alert and returns its message.
Definition: alert.py:119
def _wait_alert(self, timeout=None)
Definition: alert.py:134
def alert_should_be_present(self, text='', action=ACCEPT, timeout=None)
Verifies that an alert is present and, by default, accepts it.
Definition: alert.py:65
def alert_should_not_be_present(self, action=ACCEPT, timeout=0)
Verifies that no alert is present.
Definition: alert.py:86
def _handle_alert(self, alert, action)
Definition: alert.py:123