Skip to content

Commit 1376ae8

Browse files
committed
Add New Feature: SeleniumBase Dialog Boxes
1 parent 6a5cde0 commit 1376ae8

File tree

3 files changed

+670
-0
lines changed

3 files changed

+670
-0
lines changed

seleniumbase/core/jqc_helper.py

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
"""
2+
This module contains methods for opening jquery-confirm boxes.
3+
These helper methods SHOULD NOT be called directly from tests.
4+
"""
5+
from seleniumbase.fixtures import constants
6+
from seleniumbase.fixtures import js_utils
7+
8+
9+
form_code = (
10+
"""'<form align="center" action="" class="jqc_form">' +
11+
'<div class="form-group">' +
12+
'<input style="font-size:20px; background-color: #f8fdfd; ' +
13+
' width: 84%%; border: 1px solid blue; ' +
14+
' box-shadow:inset 0 0 2px 2px #f4fafa;"' +
15+
' type="text" class="jqc_input" />' +
16+
'</div>' +
17+
'</form>'"""
18+
)
19+
20+
21+
def jquery_confirm_button_dialog(driver, message, buttons, options=None):
22+
js_utils.activate_jquery_confirm(driver)
23+
# These defaults will be overwritten later if set
24+
theme = constants.JqueryConfirm.DEFAULT_THEME
25+
border_color = constants.JqueryConfirm.DEFAULT_COLOR
26+
width = constants.JqueryConfirm.DEFAULT_WIDTH
27+
if options:
28+
for option in options:
29+
if option[0].lower() == "theme":
30+
theme = option[1]
31+
elif option[0].lower() == "color":
32+
border_color = option[1]
33+
elif option[0].lower() == "width":
34+
width = option[1]
35+
else:
36+
raise Exception('Unknown option: "%s"' % option[0])
37+
if not message:
38+
message = ""
39+
key_row = ""
40+
if len(buttons) == 1: # There's only one button as an option
41+
key_row = "keys: ['enter', 'y', '1']," # Shortcut: "Enter","Y","1"
42+
b_html = (
43+
"""button_%s: {
44+
btnClass: 'btn-%s',
45+
text: '<b>%s</b>',
46+
%s
47+
action: function(){
48+
jqc_status = '%s';
49+
$jqc_status = jqc_status;
50+
jconfirm.lastButtonText = jqc_status;
51+
}
52+
},"""
53+
)
54+
all_buttons = ""
55+
btn_count = 0
56+
for button in buttons:
57+
btn_count += 1
58+
text = button[0]
59+
text = js_utils.escape_quotes_if_needed(text)
60+
if len(buttons) > 1 and text.lower() == "yes":
61+
key_row = "keys: ['y'],"
62+
if btn_count < 10:
63+
key_row = "keys: ['y', '%s']," % btn_count
64+
elif len(buttons) > 1 and text.lower() == "no":
65+
key_row = "keys: ['n'],"
66+
if btn_count < 10:
67+
key_row = "keys: ['n', '%s']," % btn_count
68+
elif len(buttons) > 1:
69+
if btn_count < 10:
70+
key_row = "keys: ['%s']," % btn_count
71+
color = button[1]
72+
if not color:
73+
color = "blue"
74+
new_button = b_html % (btn_count, color, text, key_row, text)
75+
all_buttons += new_button
76+
77+
content = (
78+
'<div></div><font color="#0066ee">%s</font>'
79+
"" % (message)
80+
)
81+
content = js_utils.escape_quotes_if_needed(content)
82+
overlay_opacity = "0.32"
83+
if theme.lower() == "supervan":
84+
overlay_opacity = "0.56"
85+
if theme.lower() == "bootstrap":
86+
overlay_opacity = "0.64"
87+
if theme.lower() == "modern":
88+
overlay_opacity = "0.5"
89+
if theme.lower() == "material":
90+
overlay_opacity = "0.4"
91+
jqcd = (
92+
"""jconfirm({
93+
boxWidth: '%s',
94+
useBootstrap: false,
95+
containerFluid: true,
96+
bgOpacity: %s,
97+
type: '%s',
98+
theme: '%s',
99+
animationBounce: 1,
100+
typeAnimated: true,
101+
animation: 'scale',
102+
draggable: true,
103+
dragWindowGap: 1,
104+
container: 'body',
105+
title: '%s',
106+
content: '<div></div>',
107+
buttons: {
108+
%s
109+
}
110+
});"""
111+
% (
112+
width,
113+
overlay_opacity,
114+
border_color,
115+
theme,
116+
content,
117+
all_buttons
118+
)
119+
)
120+
driver.execute_script(jqcd)
121+
122+
123+
def jquery_confirm_text_dialog(driver, message, button=None, options=None):
124+
js_utils.activate_jquery_confirm(driver)
125+
# These defaults will be overwritten later if set
126+
theme = constants.JqueryConfirm.DEFAULT_THEME
127+
border_color = constants.JqueryConfirm.DEFAULT_COLOR
128+
width = constants.JqueryConfirm.DEFAULT_WIDTH
129+
130+
if not message:
131+
message = ""
132+
if button:
133+
if not type(button) is list and not type(button) is tuple:
134+
raise Exception('"button" should be a (text, color) tuple!')
135+
if len(button) != 2:
136+
raise Exception('"button" should be a (text, color) tuple!')
137+
else:
138+
button = ("Submit", "blue")
139+
if options:
140+
for option in options:
141+
if option[0].lower() == "theme":
142+
theme = option[1]
143+
elif option[0].lower() == "color":
144+
border_color = option[1]
145+
elif option[0].lower() == "width":
146+
width = option[1]
147+
else:
148+
raise Exception('Unknown option: "%s"' % option[0])
149+
btn_text = button[0]
150+
btn_color = button[1]
151+
if not btn_color:
152+
btn_color = "blue"
153+
content = (
154+
'<div></div><font color="#0066ee">%s</font>'
155+
"" % (message)
156+
)
157+
content = js_utils.escape_quotes_if_needed(content)
158+
overlay_opacity = "0.32"
159+
if theme.lower() == "supervan":
160+
overlay_opacity = "0.56"
161+
if theme.lower() == "bootstrap":
162+
overlay_opacity = "0.64"
163+
if theme.lower() == "modern":
164+
overlay_opacity = "0.5"
165+
if theme.lower() == "material":
166+
overlay_opacity = "0.4"
167+
jqcd = (
168+
"""jconfirm({
169+
boxWidth: '%s',
170+
useBootstrap: false,
171+
containerFluid: true,
172+
bgOpacity: %s,
173+
type: '%s',
174+
theme: '%s',
175+
animationBounce: 1,
176+
typeAnimated: true,
177+
animation: 'scale',
178+
draggable: true,
179+
dragWindowGap: 1,
180+
container: 'body',
181+
title: '%s',
182+
content: '<div></div>' +
183+
%s,
184+
buttons: {
185+
formSubmit: {
186+
btnClass: 'btn-%s',
187+
text: '%s',
188+
action: function () {
189+
jqc_input = this.$content.find('.jqc_input').val();
190+
$jqc_input = this.$content.find('.jqc_input').val();
191+
jconfirm.lastInputText = jqc_input;
192+
$jqc_status = '%s'; // There is only one button
193+
},
194+
},
195+
},
196+
onContentReady: function () {
197+
var jc = this;
198+
this.$content.find('form.jqc_form').on('submit', function (e) {
199+
// User submits the form by pressing "Enter" in the field
200+
e.preventDefault();
201+
jc.$$formSubmit.trigger('click'); // Click the button
202+
});
203+
}
204+
});"""
205+
% (
206+
width,
207+
overlay_opacity,
208+
border_color,
209+
theme,
210+
content,
211+
form_code,
212+
btn_color,
213+
btn_text,
214+
btn_text
215+
)
216+
)
217+
driver.execute_script(jqcd)
218+
219+
220+
def jquery_confirm_full_dialog(driver, message, buttons, options=None):
221+
js_utils.activate_jquery_confirm(driver)
222+
# These defaults will be overwritten later if set
223+
theme = constants.JqueryConfirm.DEFAULT_THEME
224+
border_color = constants.JqueryConfirm.DEFAULT_COLOR
225+
width = constants.JqueryConfirm.DEFAULT_WIDTH
226+
227+
if not message:
228+
message = ""
229+
btn_count = 0
230+
b_html = (
231+
"""button_%s: {
232+
btnClass: 'btn-%s',
233+
text: '%s',
234+
action: function(){
235+
jqc_input = this.$content.find('.jqc_input').val();
236+
$jqc_input = this.$content.find('.jqc_input').val();
237+
jconfirm.lastInputText = jqc_input;
238+
$jqc_status = '%s';
239+
}
240+
},"""
241+
)
242+
b1_html = (
243+
"""formSubmit: {
244+
btnClass: 'btn-%s',
245+
text: '%s',
246+
action: function(){
247+
jqc_input = this.$content.find('.jqc_input').val();
248+
$jqc_input = this.$content.find('.jqc_input').val();
249+
jconfirm.lastInputText = jqc_input;
250+
jqc_status = '%s';
251+
$jqc_status = jqc_status;
252+
jconfirm.lastButtonText = jqc_status;
253+
}
254+
},"""
255+
)
256+
one_button_trigger = ""
257+
if len(buttons) == 1:
258+
# If there's only one button, allow form submit with "Enter/Return"
259+
one_button_trigger = "jc.$$formSubmit.trigger('click');"
260+
all_buttons = ""
261+
for button in buttons:
262+
text = button[0]
263+
text = js_utils.escape_quotes_if_needed(text)
264+
color = button[1]
265+
if not color:
266+
color = "blue"
267+
btn_count += 1
268+
if len(buttons) == 1:
269+
new_button = b1_html % (color, text, text)
270+
else:
271+
new_button = b_html % (btn_count, color, text, text)
272+
all_buttons += new_button
273+
if options:
274+
for option in options:
275+
if option[0].lower() == "theme":
276+
theme = option[1]
277+
elif option[0].lower() == "color":
278+
border_color = option[1]
279+
elif option[0].lower() == "width":
280+
width = option[1]
281+
else:
282+
raise Exception('Unknown option: "%s"' % option[0])
283+
284+
content = (
285+
'<div></div><font color="#0066ee">%s</font>'
286+
"" % (message)
287+
)
288+
content = js_utils.escape_quotes_if_needed(content)
289+
overlay_opacity = "0.32"
290+
if theme.lower() == "supervan":
291+
overlay_opacity = "0.56"
292+
if theme.lower() == "bootstrap":
293+
overlay_opacity = "0.64"
294+
if theme.lower() == "modern":
295+
overlay_opacity = "0.5"
296+
if theme.lower() == "material":
297+
overlay_opacity = "0.4"
298+
jqcd = (
299+
"""jconfirm({
300+
boxWidth: '%s',
301+
useBootstrap: false,
302+
containerFluid: true,
303+
bgOpacity: %s,
304+
type: '%s',
305+
theme: '%s',
306+
animationBounce: 1,
307+
typeAnimated: true,
308+
animation: 'scale',
309+
draggable: true,
310+
dragWindowGap: 1,
311+
container: 'body',
312+
title: '%s',
313+
content: '<div></div>' +
314+
%s,
315+
buttons: {
316+
%s
317+
},
318+
onContentReady: function () {
319+
var jc = this;
320+
this.$content.find('form.jqc_form').on('submit', function (e) {
321+
// User submits the form by pressing "Enter" in the field
322+
e.preventDefault();
323+
%s
324+
});
325+
}
326+
});"""
327+
% (
328+
width,
329+
overlay_opacity,
330+
border_color,
331+
theme,
332+
content,
333+
form_code,
334+
all_buttons,
335+
one_button_trigger
336+
)
337+
)
338+
driver.execute_script(jqcd)

0 commit comments

Comments
 (0)