Skip to content

Commit aad7d24

Browse files
committed
Update website tour code
1 parent 65bcb94 commit aad7d24

File tree

2 files changed

+104
-37
lines changed

2 files changed

+104
-37
lines changed

seleniumbase/core/style_sheet.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@
9797
}
9898
</style>'''
9999

100+
# Bootstrap Tour Backdrop Style
101+
bt_backdrop_style = (
102+
'''
103+
.tour-tour-element {
104+
box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.20);
105+
pointer-events: none !important;
106+
z-index: 9999;
107+
}
108+
109+
:not(.tour-tour-element) .orphan.tour-tour {
110+
box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.20);
111+
}
112+
113+
.tour-tour {
114+
pointer-events: auto;
115+
z-index: 9999;
116+
}
117+
''')
118+
100119
sh_style_test = (
101120
'''
102121
let test_tour = new Shepherd.Tour({
@@ -107,16 +126,17 @@
107126
});
108127
''')
109128

129+
# Shepherd Backdrop Style
110130
sh_backdrop_style = (
111131
'''
112132
body.shepherd-active .shepherd-target.shepherd-enabled {
113-
box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.22);
133+
box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.20);
114134
pointer-events: none !important;
115135
z-index: 9999;
116136
}
117137
118138
body.shepherd-active .shepherd-orphan {
119-
box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.16);
139+
box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.20);
120140
pointer-events: auto;
121141
z-index: 9999;
122142
}

seleniumbase/fixtures/base_case.py

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,13 @@ def __activate_bootstrap(self):
827827
bootstrap_tour_css = constants.BootstrapTour.MIN_CSS
828828
bootstrap_tour_js = constants.BootstrapTour.MIN_JS
829829

830-
verify_script = ("""// Instance the tour
830+
verify_script = ("""// Verify Bootstrap Tour activated
831831
var tour2 = new Tour({
832832
});""")
833833

834+
backdrop_style = style_sheet.bt_backdrop_style
835+
self.add_css_style(backdrop_style)
836+
self.wait_for_ready_state_complete()
834837
for x in range(4):
835838
self.activate_jquery()
836839
self.add_css_link(bootstrap_tour_css)
@@ -852,7 +855,7 @@ def __activate_bootstrap(self):
852855
'''directive. ''' % self.driver.current_url)
853856

854857
def __is_bootstrap_activated(self):
855-
verify_script = ("""// Instance the tour
858+
verify_script = ("""// Verify Bootstrap Tour activated
856859
var tour2 = new Tour({
857860
});""")
858861
try:
@@ -969,44 +972,75 @@ def __is_shepherd_activated(self):
969972
return False
970973

971974
def create_tour(self, name=None, theme=None):
972-
""" Creates a tour for a website.
975+
""" Creates a tour for a website. By default, the Shepherd Javascript
976+
Library is used with the Shepherd "Light" / "Arrows" theme.
973977
@Params
974978
name - If creating multiple tours, use this to select the
975979
tour you wish to add steps to.
976980
theme - Sets the default theme for the tour.
977981
Choose from "light"/"arrows", "dark", "default", "square",
978982
and "square-dark". ("arrows" is used if None is selected.)
983+
Alternatively, you may use a different Javascript Library
984+
as the theme. Those include "IntroJS" & "Bootstrap".
979985
"""
980986
if not name:
981987
name = "default"
982988

983-
shepherd_theme = "shepherd-theme-arrows"
984989
if theme:
985990
if theme.lower() == "bootstrap":
986991
self.create_bootstrap_tour(name)
987992
return
988-
elif theme == "default":
993+
elif theme.lower() == "intro":
994+
self.create_introjs_tour(name)
995+
return
996+
elif theme.lower() == "introjs":
997+
self.create_introjs_tour(name)
998+
return
999+
elif theme.lower() == "shepherd":
1000+
self.create_shepherd_tour(name, theme="light")
1001+
return
1002+
else:
1003+
self.create_shepherd_tour(name, theme)
1004+
else:
1005+
self.create_shepherd_tour(name, theme="light")
1006+
1007+
def create_shepherd_tour(self, name=None, theme=None):
1008+
""" Creates a Shepherd JS website tour.
1009+
@Params
1010+
name - If creating multiple tours, use this to select the
1011+
tour you wish to add steps to.
1012+
theme - Sets the default theme for the tour.
1013+
Choose from "light"/"arrows", "dark", "default", "square",
1014+
and "square-dark". ("light" is used if None is selected.)
1015+
"""
1016+
1017+
shepherd_theme = "shepherd-theme-arrows"
1018+
if theme:
1019+
if theme.lower() == "default":
9891020
shepherd_theme = "shepherd-theme-default"
990-
elif theme == "dark":
1021+
elif theme.lower() == "dark":
9911022
shepherd_theme = "shepherd-theme-dark"
992-
elif theme == "light":
1023+
elif theme.lower() == "light":
9931024
shepherd_theme = "shepherd-theme-arrows"
994-
elif theme == "arrows":
1025+
elif theme.lower() == "arrows":
9951026
shepherd_theme = "shepherd-theme-arrows"
996-
elif theme == "square":
1027+
elif theme.lower() == "square":
9971028
shepherd_theme = "shepherd-theme-square"
998-
elif theme == "square-dark":
1029+
elif theme.lower() == "square-dark":
9991030
shepherd_theme = "shepherd-theme-square-dark"
10001031

1001-
new_tour = ("""
1002-
// Shepherd Tour
1003-
let tour = new Shepherd.Tour({
1004-
defaults: {
1005-
classes: '%s',
1006-
scrollTo: true
1007-
}
1008-
});
1009-
""" % shepherd_theme)
1032+
if not name:
1033+
name = "default"
1034+
1035+
new_tour = (
1036+
"""
1037+
// Shepherd Tour
1038+
let tour = new Shepherd.Tour({
1039+
defaults: {
1040+
classes: '%s',
1041+
scrollTo: true
1042+
}
1043+
});""" % shepherd_theme)
10101044

10111045
self._tour_steps[name] = []
10121046
self._tour_steps[name].append(new_tour)
@@ -1071,22 +1105,19 @@ def add_tour_step(self, message, selector=None, name=None,
10711105
"""
10721106
if not selector:
10731107
selector = "html"
1074-
selector = re.escape(selector)
10751108
selector = self.__escape_quotes_if_needed(selector)
10761109

10771110
if not name:
10781111
name = "default"
10791112
if name not in self._tour_steps:
1080-
# By default, will create a Bootstrap tour if no tours exist
1081-
self.create_tour(name=name, theme="bootstrap")
1113+
# By default, will create an IntroJS tour if no tours exist
1114+
self.create_tour(name=name, theme="introjs")
10821115

10831116
if not title:
10841117
title = ""
1085-
title = re.escape(title)
10861118
title = self.__escape_quotes_if_needed(title)
10871119

10881120
if message:
1089-
message = re.escape(message)
10901121
message = self.__escape_quotes_if_needed(message)
10911122
else:
10921123
message = ""
@@ -1171,6 +1202,7 @@ def __add_bootstrap_tour_step(self, message, selector=None, name=None,
11711202
before automatically advancing to the next tour step.
11721203
"""
11731204
if selector != "html":
1205+
selector = self.__make_css_match_first_element_only(selector)
11741206
element_row = "element: '%s'," % selector
11751207
else:
11761208
element_row = ""
@@ -1303,23 +1335,29 @@ def __play_shepherd_tour(self, name=None, interval=0):
13031335
if autoplay:
13041336
try:
13051337
element = self.execute_script(
1306-
"Shepherd.activeTour.currentStep"
1338+
"return Shepherd.activeTour.currentStep"
13071339
".options.attachTo.element")
13081340
shep_text = self.execute_script(
1309-
"Shepherd.activeTour.currentStep.options.text")
1341+
"return Shepherd.activeTour.currentStep"
1342+
".options.text")
13101343
except Exception:
13111344
continue
1345+
if element != latest_element or shep_text != latest_text:
1346+
latest_element = element
1347+
latest_text = shep_text
1348+
start_ms = time.time() * 1000.0
1349+
stop_ms = start_ms + (interval * 1000.0)
13121350
now_ms = time.time() * 1000.0
13131351
if now_ms >= stop_ms:
13141352
if ((element == latest_element) and
13151353
(shep_text == latest_text)):
13161354
self.execute_script("Shepherd.activeTour.next()")
13171355
try:
13181356
latest_element = self.execute_script(
1319-
"Shepherd.activeTour.currentStep"
1357+
"return Shepherd.activeTour.currentStep"
13201358
".options.attachTo.element")
13211359
latest_text = self.execute_script(
1322-
"Shepherd.activeTour.currentStep"
1360+
"return Shepherd.activeTour.currentStep"
13231361
".options.text")
13241362
start_ms = time.time() * 1000.0
13251363
stop_ms = start_ms + (interval * 1000.0)
@@ -1335,7 +1373,7 @@ def __play_shepherd_tour(self, name=None, interval=0):
13351373
".currentStep.options.attachTo.element")
13361374
try:
13371375
self.__wait_for_css_query_selector(
1338-
selector, timeout=(settings.MINI_TIMEOUT))
1376+
selector, timeout=(settings.SMALL_TIMEOUT))
13391377
except Exception:
13401378
self.remove_elements("div.shepherd-content")
13411379
self.__post_messenger_error_message(
@@ -1364,7 +1402,7 @@ def __play_bootstrap_tour(self, name=None, interval=0):
13641402
for tour_step in self._tour_steps[name]:
13651403
instructions += tour_step
13661404
instructions += (
1367-
"""]});
1405+
"""]);
13681406
13691407
// Initialize the tour
13701408
tour.init();
@@ -1391,7 +1429,7 @@ def __play_bootstrap_tour(self, name=None, interval=0):
13911429
selector = re.search(
13921430
r"[\S\s]+element: '([\S\s]+)',[\S\s]+title: '",
13931431
self._tour_steps[name][1]).group(1)
1394-
selector = selector.replace('\\', '')
1432+
selector = selector.replace('\\', '').replace(':first', '')
13951433
self.wait_for_element_present(
13961434
selector, timeout=(settings.SMALL_TIMEOUT))
13971435
else:
@@ -1411,8 +1449,12 @@ def __play_bootstrap_tour(self, name=None, interval=0):
14111449
while tour_on:
14121450
try:
14131451
time.sleep(0.01)
1414-
result = self.execute_script(
1415-
"return $tour.ended()")
1452+
if self.browser != "firefox":
1453+
result = self.execute_script(
1454+
"return $tour.ended()")
1455+
else:
1456+
self.wait_for_element_present(".tour-tour", timeout=0.4)
1457+
result = False
14161458
except Exception:
14171459
tour_on = False
14181460
result = None
@@ -1421,8 +1463,13 @@ def __play_bootstrap_tour(self, name=None, interval=0):
14211463
else:
14221464
try:
14231465
time.sleep(0.01)
1424-
result = self.execute_script(
1425-
"return $tour.ended()")
1466+
if self.browser != "firefox":
1467+
result = self.execute_script(
1468+
"return $tour.ended()")
1469+
else:
1470+
self.wait_for_element_present(
1471+
".tour-tour", timeout=0.4)
1472+
result = False
14261473
if result is False:
14271474
time.sleep(0.1)
14281475
continue

0 commit comments

Comments
 (0)