Skip to content

Commit e3b8de0

Browse files
committed
Refactoring
1 parent 88d7ff6 commit e3b8de0

File tree

5 files changed

+169
-61
lines changed

5 files changed

+169
-61
lines changed

seleniumbase/console_scripts/run.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,33 +1067,33 @@ def main():
10671067
show_translate_usage()
10681068
elif command == "extract-objects" or command == "extract_objects":
10691069
if len(command_args) >= 1:
1070-
from seleniumbase.console_scripts import objectify
1070+
from seleniumbase.console_scripts import sb_objectify
10711071

1072-
objectify.extract_objects()
1072+
sb_objectify.extract_objects()
10731073
else:
10741074
show_basic_usage()
10751075
show_extract_objects_usage()
10761076
elif command == "inject-objects" or command == "inject_objects":
10771077
if len(command_args) >= 1:
1078-
from seleniumbase.console_scripts import objectify
1078+
from seleniumbase.console_scripts import sb_objectify
10791079

1080-
objectify.inject_objects()
1080+
sb_objectify.inject_objects()
10811081
else:
10821082
show_basic_usage()
10831083
show_inject_objects_usage()
10841084
elif command == "objectify":
10851085
if len(command_args) >= 1:
1086-
from seleniumbase.console_scripts import objectify
1086+
from seleniumbase.console_scripts import sb_objectify
10871087

1088-
objectify.objectify()
1088+
sb_objectify.objectify()
10891089
else:
10901090
show_basic_usage()
10911091
show_objectify_usage()
10921092
elif command == "revert-objects" or command == "revert_objects":
10931093
if len(command_args) >= 1:
1094-
from seleniumbase.console_scripts import objectify
1094+
from seleniumbase.console_scripts import sb_objectify
10951095

1096-
objectify.revert_objects()
1096+
sb_objectify.revert_objects()
10971097
else:
10981098
show_basic_usage()
10991099
show_revert_objects_usage()

seleniumbase/console_scripts/objectify.py renamed to seleniumbase/console_scripts/sb_objectify.py

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import sys
1616

1717
PAGE_OBJECTS_FILE = "page_objects.py" # Don't change this. It's hard-coded.
18+
p_o_import = "from .page_objects import "
19+
if not os.path.exists("__init__.py"):
20+
p_o_import = "from page_objects import "
1821

1922

2023
def invalid_run_command(shell_command):
@@ -339,6 +342,63 @@ def process_test_file(
339342
seleniumbase_lines.append(command)
340343
continue
341344

345+
# Handle self.js_click_*(SELECTOR) * = all/if_present/if_visible
346+
if not object_dict:
347+
data = re.match(
348+
r"""^(\s*)self\.js_click_(\S*)"""
349+
r"""\((r?['"][\S\s]+['"])\)([\S\s]*)"""
350+
r"""$""",
351+
line,
352+
)
353+
else:
354+
data = re.match(
355+
r"""^(\s*)self\.js_click_(\S*)"""
356+
r"""\(([\S]+)\)([\S\s]*)"""
357+
r"""$""",
358+
line,
359+
)
360+
if data:
361+
whitespace = data.group(1)
362+
by_type = data.group(2)
363+
selector = "%s" % data.group(3)
364+
selector = remove_extra_slashes(selector)
365+
page_selectors.append(selector)
366+
comments = data.group(4)
367+
command = """%sself.js_click_%s(%s)%s""" % (
368+
whitespace,
369+
by_type,
370+
selector,
371+
comments,
372+
)
373+
if selector_dict:
374+
if add_comments:
375+
comments = " # %s" % selector
376+
selector = optimize_selector(selector)
377+
if selector in selector_dict.keys():
378+
selector_object = selector_dict[selector]
379+
changed.append(selector_object.split(".")[0])
380+
command = """%sself.js_click_%s(%s)%s""" % (
381+
whitespace,
382+
by_type,
383+
selector_object,
384+
comments,
385+
)
386+
if object_dict:
387+
if not add_comments:
388+
comments = ""
389+
object_name = selector
390+
if object_name in object_dict.keys():
391+
selector_object = object_dict[object_name]
392+
changed.append(object_name.split(".")[0])
393+
command = """%sself.js_click_%s(%s)%s""" % (
394+
whitespace,
395+
by_type,
396+
selector_object,
397+
comments,
398+
)
399+
seleniumbase_lines.append(command)
400+
continue
401+
342402
# Handle self.slow_click(SELECTOR)
343403
if not object_dict:
344404
data = re.match(
@@ -498,6 +558,59 @@ def process_test_file(
498558
seleniumbase_lines.append(command)
499559
continue
500560

561+
# Handle self.click_if_visible(SELECTOR)
562+
if not object_dict:
563+
data = re.match(
564+
r"""^(\s*)self\.click_if_visible"""
565+
r"""\((r?['"][\S\s]+['"])\)([\S\s]*)"""
566+
r"""$""",
567+
line,
568+
)
569+
else:
570+
data = re.match(
571+
r"""^(\s*)self\.click_if_visible"""
572+
r"""\(([\S]+)\)([\S\s]*)"""
573+
r"""$""",
574+
line,
575+
)
576+
if data:
577+
whitespace = data.group(1)
578+
selector = "%s" % data.group(2)
579+
selector = remove_extra_slashes(selector)
580+
page_selectors.append(selector)
581+
comments = data.group(3)
582+
command = """%sself.click_if_visible(%s)%s""" % (
583+
whitespace,
584+
selector,
585+
comments,
586+
)
587+
if selector_dict:
588+
if add_comments:
589+
comments = " # %s" % selector
590+
selector = optimize_selector(selector)
591+
if selector in selector_dict.keys():
592+
selector_object = selector_dict[selector]
593+
changed.append(selector_object.split(".")[0])
594+
command = """%sself.click_if_visible(%s)%s""" % (
595+
whitespace,
596+
selector_object,
597+
comments,
598+
)
599+
if object_dict:
600+
if not add_comments:
601+
comments = ""
602+
object_name = selector
603+
if object_name in object_dict.keys():
604+
selector_object = object_dict[object_name]
605+
changed.append(object_name.split(".")[0])
606+
command = """%sself.click_if_visible(%s)%s""" % (
607+
whitespace,
608+
selector_object,
609+
comments,
610+
)
611+
seleniumbase_lines.append(command)
612+
continue
613+
501614
# Handle self.highlight(SELECTOR)
502615
if not object_dict:
503616
data = re.match(
@@ -2969,8 +3082,8 @@ def main(shell_command):
29693082
if item not in added_classes:
29703083
added_classes.append(item)
29713084
for line in seleniumbase_lines:
2972-
if "from .page_objects import" in line:
2973-
token = line.split("from .page_objects import ")[1].strip()
3085+
if p_o_import in line:
3086+
token = line.split(p_o_import)[1].strip()
29743087
if token in added_classes:
29753088
# Don't import page_objects classes if already imported
29763089
added_classes.remove(token)
@@ -2981,7 +3094,7 @@ def main(shell_command):
29813094
if line.startswith("from") and "import" in line and not fit_in:
29823095
fit_in = True
29833096
for add_me in added_classes:
2984-
import_line = "from .page_objects import %s" % add_me
3097+
import_line = "%s%s" % (p_o_import, add_me)
29853098
sb_lines.append(import_line)
29863099
sb_lines.append(line)
29873100
seleniumbase_lines = sb_lines
@@ -2997,8 +3110,8 @@ def main(shell_command):
29973110
if removed_classes:
29983111
sb_lines = []
29993112
for line in seleniumbase_lines:
3000-
if "from .page_objects import" in line:
3001-
token = line.split("from .page_objects import ")[1].strip()
3113+
if p_o_import in line:
3114+
token = line.split(p_o_import)[1].strip()
30023115
if token in removed_classes:
30033116
continue
30043117
sb_lines.append(line)

seleniumbase/core/mysql.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ def __init__(self, database_env="test", conf_creds=None):
4242
count = 0
4343
while count < retry_count:
4444
try:
45-
if (
46-
sys.version_info[0] == 3
47-
and sys.version_info[1] >= 6
48-
or (sys.version_info[0] > 3)
49-
):
45+
if sys.version_info >= (3, 6):
5046
# PyMySQL 1.0.0 or above renamed the variables.
5147
self.conn = pymysql.connect(
5248
host=db_server,

seleniumbase/core/tour_helper.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import re
77
import textwrap
88
import time
9-
from selenium.webdriver.common.by import By
109
from seleniumbase import config as sb_config
1110
from seleniumbase.config import settings
1211
from seleniumbase.core import style_sheet
@@ -290,7 +289,7 @@ def play_shepherd_tour(driver, tour_steps, msg_dur, name=None, interval=0):
290289
page_actions.wait_for_element_present(
291290
driver,
292291
selector,
293-
by=By.CSS_SELECTOR,
292+
by="css selector",
294293
timeout=settings.SMALL_TIMEOUT,
295294
)
296295
except Exception:
@@ -428,7 +427,7 @@ def play_bootstrap_tour(
428427
page_actions.wait_for_element_present(
429428
driver,
430429
selector,
431-
by=By.CSS_SELECTOR,
430+
by="css selector",
432431
timeout=settings.SMALL_TIMEOUT,
433432
)
434433
else:
@@ -452,7 +451,7 @@ def play_bootstrap_tour(
452451
result = driver.execute_script("return $tour.ended()")
453452
else:
454453
page_actions.wait_for_element_present(
455-
driver, ".tour-tour", by=By.CSS_SELECTOR, timeout=0.65
454+
driver, ".tour-tour", by="css selector", timeout=0.65
456455
)
457456
result = False
458457
except Exception:
@@ -468,7 +467,7 @@ def play_bootstrap_tour(
468467
result = driver.execute_script("return $tour.ended()")
469468
else:
470469
page_actions.wait_for_element_present(
471-
driver, ".tour-tour", by=By.CSS_SELECTOR, timeout=0.65
470+
driver, ".tour-tour", by="css selector", timeout=0.65
472471
)
473472
result = False
474473
if result is False:
@@ -514,7 +513,7 @@ def play_driverjs_tour(
514513
page_actions.wait_for_element_present(
515514
driver,
516515
selector,
517-
by=By.CSS_SELECTOR,
516+
by="css selector",
518517
timeout=settings.SMALL_TIMEOUT,
519518
)
520519
else:
@@ -547,7 +546,7 @@ def play_driverjs_tour(
547546
page_actions.wait_for_element_visible(
548547
driver,
549548
"#driver-popover-item",
550-
by=By.CSS_SELECTOR,
549+
by="css selector",
551550
timeout=1.1,
552551
)
553552
result = False
@@ -594,7 +593,7 @@ def play_driverjs_tour(
594593
page_actions.wait_for_element_visible(
595594
driver,
596595
"#driver-popover-item",
597-
by=By.CSS_SELECTOR,
596+
by="css selector",
598597
timeout=1.1,
599598
)
600599
result = False
@@ -641,7 +640,7 @@ def play_hopscotch_tour(
641640
page_actions.wait_for_element_present(
642641
driver,
643642
selector,
644-
by=By.CSS_SELECTOR,
643+
by="css selector",
645644
timeout=settings.SMALL_TIMEOUT,
646645
)
647646
else:
@@ -671,7 +670,7 @@ def play_hopscotch_tour(
671670
page_actions.wait_for_element_present(
672671
driver,
673672
".hopscotch-bubble",
674-
by=By.CSS_SELECTOR,
673+
by="css selector",
675674
timeout=0.4,
676675
)
677676
result = False
@@ -713,7 +712,7 @@ def play_hopscotch_tour(
713712
page_actions.wait_for_element_present(
714713
driver,
715714
".hopscotch-bubble",
716-
by=By.CSS_SELECTOR,
715+
by="css selector",
717716
timeout=0.4,
718717
)
719718
result = False
@@ -773,7 +772,7 @@ def play_introjs_tour(
773772
page_actions.wait_for_element_present(
774773
driver,
775774
selector,
776-
by=By.CSS_SELECTOR,
775+
by="css selector",
777776
timeout=settings.SMALL_TIMEOUT,
778777
)
779778
else:
@@ -800,7 +799,7 @@ def play_introjs_tour(
800799
result = driver.execute_script("return $tour._currentStep")
801800
else:
802801
page_actions.wait_for_element_present(
803-
driver, ".introjs-tooltip", by=By.CSS_SELECTOR, timeout=0.4
802+
driver, ".introjs-tooltip", by="css selector", timeout=0.4
804803
)
805804
result = True
806805
except Exception:
@@ -844,7 +843,7 @@ def play_introjs_tour(
844843
page_actions.wait_for_element_present(
845844
driver,
846845
".introjs-tooltip",
847-
by=By.CSS_SELECTOR,
846+
by="css selector",
848847
timeout=0.4,
849848
)
850849
result = True

0 commit comments

Comments
 (0)