Skip to content

Commit 434d4ed

Browse files
authored
Merge pull request #167 from seleniumbase/autoplay-walkthrough-tours
Autoplay steps of walkthrough tours
2 parents 212fdd7 + c5e0f11 commit 434d4ed

26 files changed

+2693
-26
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ script:
2828
- "nosetests examples/boilerplates/boilerplate_test.py --browser=chrome --headless"
2929
- "pytest examples/my_first_test.py --browser=firefox -s --headless --with-db_reporting"
3030
- "pytest examples/my_first_test.py --browser=chrome -s --headless --with-db_reporting --demo_mode --demo_sleep=0.2"
31+
- "pytest examples/tour_examples/google_tour.py -s --headless --with-db_reporting"
3132
- "sudo mysql --password=test -e 'select test_address,browser,state,start_time,runtime from test_db.test_run_data'"
3233
after_script:
3334
- "sudo mysql -e 'DROP DATABASE test_db;'"

examples/proxy_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ class MyTestClass(BaseCase):
66

77
def test_proxy(self):
88
self.open('https://ipinfo.io/')
9-
ip_address = self.get_page_title().split(' ')[0]
9+
ip_address = self.get_text("div.home-ip-details span.value")[1:-1]
10+
self.open('https://ipinfo.io/%s' % ip_address)
1011
print("\n\nIP Address = %s\n" % ip_address)
11-
href = '/%s' % ip_address
12-
self.click('[href="%s"]' % href)
1312
print("Displaying Host Info:")
1413
print(self.get_text('table.table'))
1514
print("\nThe browser will close automatically in 7 seconds...")

examples/tour_examples/ReadMe.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
![](https://cdn2.hubspot.net/hubfs/100006/images/google_tour.gif "SeleniumBase Tours")<br>
44

5-
SeleniumBase Tours utilize the [HubSpot Shepherd Library](http://github.hubspot.com/shepherd/docs/welcome/) for creating and running tours on any website.
5+
SeleniumBase Tours utilize the [HubSpot Shepherd Library](http://github.hubspot.com/shepherd/docs/welcome/) for creating and running tours, demos, and walkthroughs on any website.
66

77
To utilize tours, there are three methods that you need to know at the basic level (which contain optional arguments):
88

99
``self.create_tour(theme)``
1010

1111
``self.add_tour_step(message, css_selector, title, alignment, theme)``
1212

13-
``self.play_tour()``
13+
``self.play_tour(interval)``
1414

1515
With the ``create_tour()`` method, you can pass a default theme to change the look & feel of the tour steps. Valid themes are ``dark``, ``default``, ``arrows``, ``square``, and ``square-dark``.
1616

17-
With the ``self.add_tour_step()`` method, at minimum you must pass a message to display. Then you can specify a web element to attach to (by CSS selector). If no element is specified, the tour step will tether to the top of the screen by default. You can add an optional title above the message to display with the tour step. You can also change the theme for that step, as well as specifiy the alignment (which is the side of the element that you want the tour message to tether to).
17+
With the ``self.add_tour_step()`` method, at minimum you must pass a message to display. Then you can specify a web element to attach to (by CSS selector). If no element is specified, the tour step will tether to the top of the screen by default. You can also add an optional title above the message to display with the tour step, as well as change the theme for that step, and even specify the alignment (which is the side of the element that you want the tour message to tether to).
1818

19-
Finally, you can play a tour you created by calling the ``self.play_tour()`` method.
19+
Finally, you can play a tour you created by calling the ``self.play_tour()`` method. If you specify an interval, the tour will automatically walk through each step after that many seconds have passed.
2020

2121
### Here's an example of using SeleniumBase Tours:
2222

seleniumbase/fixtures/base_case.py

Lines changed: 55 additions & 18 deletions
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
@@ -1134,12 +1178,6 @@ def activate_messenger(self):
11341178
"/messenger/1.5.0/js/messenger-theme-flat.js")
11351179
msg_theme_future_js = ("https://cdnjs.cloudflare.com/ajax/libs"
11361180
"/messenger/1.5.0/js/messenger-theme-future.js")
1137-
msgr_theme_block_js = ("https://cdnjs.cloudflare.com/ajax/libs"
1138-
"/messenger/1.5.0/js/messenger-theme-block.js")
1139-
msgr_theme_air_js = ("https://cdnjs.cloudflare.com/ajax/libs"
1140-
"/messenger/1.5.0/js/messenger-theme-air.js")
1141-
msgr_theme_ice_js = ("https://cdnjs.cloudflare.com/ajax/libs"
1142-
"/messenger/1.5.0/js/messenger-theme-ice.js")
11431181
underscore_js = ("https://cdnjs.cloudflare.com/ajax/libs"
11441182
"/underscore.js/1.4.3/underscore-min.js")
11451183
backbone_js = ("https://cdnjs.cloudflare.com/ajax/libs"
@@ -1150,9 +1188,9 @@ def activate_messenger(self):
11501188
"/messenger/1.5.0/css/messenger.css")
11511189
msgr_theme_flat_css = ("https://cdnjs.cloudflare.com/ajax/libs"
11521190
"/messenger/1.5.0/css/messenger-theme-flat.css")
1153-
msgr_theme_futur_css = ("https://cdnjs.cloudflare.com/ajax/libs"
1154-
"/messenger/1.5.0/css/"
1155-
"messenger-theme-future.css")
1191+
msgr_theme_future_css = ("https://cdnjs.cloudflare.com/ajax/libs"
1192+
"/messenger/1.5.0/css/"
1193+
"messenger-theme-future.css")
11561194
msgr_theme_block_css = ("https://cdnjs.cloudflare.com/ajax/libs"
11571195
"/messenger/1.5.0/css/"
11581196
"messenger-theme-block.css")
@@ -1169,7 +1207,7 @@ def activate_messenger(self):
11691207
self.add_js_link(jquery_js)
11701208
self.add_css_link(messenger_css)
11711209
self.add_css_link(msgr_theme_flat_css)
1172-
self.add_css_link(msgr_theme_futur_css)
1210+
self.add_css_link(msgr_theme_future_css)
11731211
self.add_css_link(msgr_theme_block_css)
11741212
self.add_css_link(msgr_theme_air_css)
11751213
self.add_css_link(msgr_theme_ice_css)
@@ -1179,9 +1217,6 @@ def activate_messenger(self):
11791217
self.add_js_link(messenger_js)
11801218
self.add_js_link(msgr_theme_flat_js)
11811219
self.add_js_link(msg_theme_future_js)
1182-
self.add_js_link(msgr_theme_block_js)
1183-
self.add_js_link(msgr_theme_air_js)
1184-
self.add_js_link(msgr_theme_ice_js)
11851220

11861221
for x in range(int(settings.MINI_TIMEOUT * 10.0)):
11871222
# Messenger needs a small amount of time to load & activate.
@@ -1320,13 +1355,15 @@ def bring_to_front(self, selector, by=By.CSS_SELECTOR):
13201355
self.execute_script(script)
13211356

13221357
def highlight_click(self, selector, by=By.CSS_SELECTOR,
1323-
loops=2, scroll=True):
1324-
self.highlight(selector, by=by, loops=loops, scroll=scroll)
1358+
loops=3, scroll=True):
1359+
if not self.demo_mode:
1360+
self.highlight(selector, by=by, loops=loops, scroll=scroll)
13251361
self.click(selector, by=by)
13261362

13271363
def highlight_update_text(self, selector, new_value, by=By.CSS_SELECTOR,
1328-
loops=2, scroll=True):
1329-
self.highlight(selector, by=by, loops=loops, scroll=scroll)
1364+
loops=3, scroll=True):
1365+
if not self.demo_mode:
1366+
self.highlight(selector, by=by, loops=loops, scroll=scroll)
13301367
self.update_text(selector, new_value, by=by)
13311368

13321369
def highlight(self, selector, by=By.CSS_SELECTOR,

0 commit comments

Comments
 (0)