1
1
import os
2
- from selenium .common .exceptions import WebDriverException
3
2
from keywordgroup import KeywordGroup
4
3
5
- class _JavaScriptKeywords (KeywordGroup ):
6
4
7
- def __init__ (self ):
8
- self ._cancel_on_next_confirmation = False
5
+ class _JavaScriptKeywords (KeywordGroup ):
9
6
10
7
# Public
11
8
12
- def alert_should_be_present (self , text = '' ):
13
- """Verifies an alert is present and dismisses it.
14
-
15
- If `text` is a non-empty string, then it is also verified that the
16
- message of the alert equals to `text`.
17
-
18
- Will fail if no alert is present. Note that following keywords
19
- will fail unless the alert is dismissed by this
20
- keyword or another like `Get Alert Message`.
21
- """
22
- alert_text = self .get_alert_message ()
23
- if text and alert_text != text :
24
- raise AssertionError ("Alert text should have been '%s' but was '%s'"
25
- % (text , alert_text ))
26
-
27
- def choose_cancel_on_next_confirmation (self ):
28
- """Cancel will be selected the next time `Confirm Action` is used."""
29
- self ._cancel_on_next_confirmation = True
30
-
31
- def choose_ok_on_next_confirmation (self ):
32
- """Undo the effect of using keywords `Choose Cancel On Next Confirmation`. Note
33
- that Selenium's overridden window.confirm() function will normally automatically
34
- return true, as if the user had manually clicked OK, so you shouldn't
35
- need to use this command unless for some reason you need to change
36
- your mind prior to the next confirmation. After any confirmation, Selenium will resume using the
37
- default behavior for future confirmations, automatically returning
38
- true (OK) unless/until you explicitly use `Choose Cancel On Next Confirmation` for each
39
- confirmation.
40
-
41
- Note that every time a confirmation comes up, you must
42
- consume it by using a keywords such as `Get Alert Message`, or else
43
- the following selenium operations will fail.
44
- """
45
- self ._cancel_on_next_confirmation = False
46
-
47
- def confirm_action (self ):
48
- """Dismisses currently shown confirmation dialog and returns it's message.
49
-
50
- By default, this keyword chooses 'OK' option from the dialog. If
51
- 'Cancel' needs to be chosen, keyword `Choose Cancel On Next
52
- Confirmation` must be called before the action that causes the
53
- confirmation dialog to be shown.
54
-
55
- Examples:
56
- | Click Button | Send | # Shows a confirmation dialog |
57
- | ${message}= | Confirm Action | # Chooses Ok |
58
- | Should Be Equal | ${message} | Are your sure? |
59
- | | | |
60
- | Choose Cancel On Next Confirmation | | |
61
- | Click Button | Send | # Shows a confirmation dialog |
62
- | Confirm Action | | # Chooses Cancel |
63
- """
64
- text = self ._close_alert (not self ._cancel_on_next_confirmation )
65
- self ._cancel_on_next_confirmation = False
66
- return text
67
-
68
9
def execute_javascript (self , * code ):
69
10
"""Executes the given JavaScript code.
70
11
@@ -120,59 +61,8 @@ def execute_async_javascript(self, *code):
120
61
self ._info ("Executing Asynchronous JavaScript:\n %s" % js )
121
62
return self ._current_browser ().execute_async_script (js )
122
63
123
- def get_alert_message (self , dismiss = True ):
124
- """Returns the text of current JavaScript alert.
125
-
126
- By default the current JavaScript alert will be dismissed.
127
- This keyword will fail if no alert is present. Note that
128
- following keywords will fail unless the alert is
129
- dismissed by this keyword or another like `Get Alert Message`.
130
- """
131
- if dismiss :
132
- return self ._close_alert ()
133
- else :
134
- return self ._read_alert ()
135
-
136
- def dismiss_alert (self , accept = True ):
137
- """ Returns true if alert was confirmed, false if it was dismissed
138
-
139
- This keyword will fail if no alert is present. Note that
140
- following keywords will fail unless the alert is
141
- dismissed by this keyword or another like `Get Alert Message`.
142
- """
143
- return self ._handle_alert (accept )
144
-
145
64
# Private
146
65
147
- def _close_alert (self , confirm = True ):
148
- try :
149
- text = self ._read_alert ()
150
- alert = self ._handle_alert (confirm )
151
- return text
152
- except WebDriverException :
153
- raise RuntimeError ('There were no alerts' )
154
-
155
- def _read_alert (self ):
156
- alert = None
157
- try :
158
- alert = self ._current_browser ().switch_to_alert ()
159
- text = ' ' .join (alert .text .splitlines ()) # collapse new lines chars
160
- return text
161
- except WebDriverException :
162
- raise RuntimeError ('There were no alerts' )
163
-
164
- def _handle_alert (self , confirm = True ):
165
- try :
166
- alert = self ._current_browser ().switch_to_alert ()
167
- if not confirm :
168
- alert .dismiss ()
169
- return False
170
- else :
171
- alert .accept ()
172
- return True
173
- except WebDriverException :
174
- raise RuntimeError ('There were no alerts' )
175
-
176
66
def _get_javascript_to_execute (self , code ):
177
67
codepath = code .replace ('/' , os .sep )
178
68
if not (os .path .isabs (codepath ) and os .path .isfile (codepath )):
@@ -183,4 +73,4 @@ def _get_javascript_to_execute(self, code):
183
73
try :
184
74
return codefile .read ().strip ()
185
75
finally :
186
- codefile .close ()
76
+ codefile .close ()
0 commit comments