Skip to content

Commit 068eb5a

Browse files
committed
Add feature to autoplay steps of walkthrough tours.
1 parent d58348a commit 068eb5a

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,15 +1030,24 @@ def add_tour_step(self, message, selector=None, name=None,
10301030

10311031
self._tour_steps[name].append(step)
10321032

1033-
def play_tour(self, name=None):
1033+
def play_tour(self, name=None, interval=0):
10341034
""" Plays a tour on the current website.
10351035
@Params
10361036
name - If creating multiple tours, use this to select the
10371037
tour you wish to play.
1038+
interval - The delay time between autoplaying tour steps.
1039+
If set to 0 (default), the tour is fully manual control.
10381040
"""
10391041
if self.headless:
10401042
return # Tours should not run in headless mode.
10411043

1044+
autoplay = False
1045+
if interval and interval > 0:
1046+
autoplay = True
1047+
interval = float(interval)
1048+
if interval < 0.5:
1049+
interval = 0.5
1050+
10421051
if not name:
10431052
name = "default"
10441053
if name not in self._tour_steps:
@@ -1070,6 +1079,11 @@ def play_tour(self, name=None):
10701079

10711080
self.execute_script(instructions)
10721081
tour_on = True
1082+
if autoplay:
1083+
start_ms = time.time() * 1000.0
1084+
stop_ms = start_ms + (interval * 1000.0)
1085+
latest_element = None
1086+
latest_text = None
10731087
while tour_on:
10741088
try:
10751089
time.sleep(0.01)
@@ -1080,6 +1094,33 @@ def play_tour(self, name=None):
10801094
result = None
10811095
if result:
10821096
tour_on = True
1097+
if autoplay:
1098+
try:
1099+
element = self.execute_script(
1100+
"Shepherd.activeTour.currentStep"
1101+
".options.attachTo.element")
1102+
shep_text = self.execute_script(
1103+
"Shepherd.activeTour.currentStep.options.text")
1104+
except Exception:
1105+
continue
1106+
now_ms = time.time() * 1000.0
1107+
if now_ms >= stop_ms:
1108+
if ((element == latest_element) and
1109+
(shep_text == latest_text)):
1110+
self.execute_script("Shepherd.activeTour.next()")
1111+
try:
1112+
latest_element = self.execute_script(
1113+
"Shepherd.activeTour.currentStep"
1114+
".options.attachTo.element")
1115+
latest_text = self.execute_script(
1116+
"Shepherd.activeTour.currentStep"
1117+
".options.text")
1118+
start_ms = time.time() * 1000.0
1119+
stop_ms = start_ms + (interval * 1000.0)
1120+
except Exception:
1121+
pass
1122+
continue
1123+
10831124
else:
10841125
try:
10851126
time.sleep(0.01)
@@ -1097,6 +1138,9 @@ def play_tour(self, name=None):
10971138
duration=settings.SMALL_TIMEOUT)
10981139
time.sleep(0.1)
10991140
self.execute_script("Shepherd.activeTour.next()")
1141+
if autoplay:
1142+
start_ms = time.time() * 1000.0
1143+
stop_ms = start_ms + (interval * 1000.0)
11001144
tour_on = True
11011145
except Exception:
11021146
tour_on = False

0 commit comments

Comments
 (0)