20
20
21
21
22
22
def main ():
23
- expected_arg = ("[A Katalon/Selenium IDE recording exported as "
24
- "a Python-WebDriver unittest script ].py" )
23
+ expected_arg = ("[A PYTHON_WEBDRIVER_UNITTEST_FILE exported from a "
24
+ "Katalon/Selenium-IDE recording ].py" )
25
25
num_args = len (sys .argv )
26
26
if sys .argv [0 ].split ('/' )[- 1 ] == "seleniumbase" or (
27
27
sys .argv [0 ].split ('\\ ' )[- 1 ] == "seleniumbase" ):
28
28
if num_args < 3 or num_args > 3 :
29
- raise Exception ('\n * INVALID RUN COMMAND! * Usage:\n '
29
+ raise Exception ('\n \n * INVALID RUN COMMAND! * Usage:\n '
30
30
'"seleniumbase convert %s"\n ' % expected_arg )
31
31
else :
32
32
if num_args < 2 or num_args > 2 :
33
- raise Exception ('\n * INVALID RUN COMMAND! * Usage:\n '
33
+ raise Exception ('\n \n * INVALID RUN COMMAND! * Usage:\n '
34
34
'"python convert_ide.py %s"\n ' % expected_arg )
35
35
webdriver_python_file = sys .argv [num_args - 1 ]
36
36
if not webdriver_python_file .endswith ('.py' ):
37
- raise Exception ("* `%s` is not a Python file! * \n "
37
+ raise Exception ("\n \n `%s` is not a Python file!\n \n "
38
38
"Expecting: %s\n "
39
39
% (webdriver_python_file , expected_arg ))
40
40
@@ -47,15 +47,17 @@ def main():
47
47
in_test_method = False
48
48
has_unicode = False
49
49
uses_keys = False
50
+ uses_select = False
50
51
51
52
f = open (webdriver_python_file , 'r' )
52
53
all_code = f .read ()
53
54
f .close ()
54
55
if "def test_" not in all_code :
55
- raise Exception ("* `%s` is not a valid Python unittest.TestCase file! "
56
- "*\n Expecting: %s\n "
57
- "Did you properly export your Selenium-IDE recording "
58
- "as a Python WebDriver unittest file?" % expected_arg )
56
+ raise Exception ("\n \n `%s` is not a valid Python unittest.TestCase "
57
+ "file!\n \n Expecting: %s\n \n "
58
+ "Did you properly export your Katalon/Selenium-IDE "
59
+ "recording as a Python WebDriver unittest file?\n "
60
+ % (webdriver_python_file , expected_arg ))
59
61
code_lines = all_code .split ('\n ' )
60
62
for line in code_lines :
61
63
@@ -312,6 +314,22 @@ def main():
312
314
seleniumbase_lines .append (command )
313
315
continue
314
316
317
+ # Handle Select / by_id() / select_by_visible_text()
318
+ data = re .match (
319
+ '''^(\s*)Select\(driver\.find_element_by_id\('''
320
+ '''\" ([\S\s]+)\" \)\)\.select_by_visible_text\('''
321
+ '''\" ([\S\s]+)\" \)\s*$''' , line )
322
+ if data :
323
+ whitespace = data .group (1 )
324
+ selector = '#%s' % data .group (2 )
325
+ visible_text = '%s' % data .group (3 )
326
+ command = '''%sself.pick_select_option_by_text('%s', '%s')''' % (
327
+ whitespace , selector , visible_text )
328
+ if command .count ('\\ "' ) == command .count ('"' ):
329
+ command = command .replace ('\\ "' , '"' )
330
+ seleniumbase_lines .append (command )
331
+ continue
332
+
315
333
# Handle Select / by_xpath() / select_by_visible_text()
316
334
data = re .match (
317
335
'''^(\s*)Select\(driver\.find_element_by_xpath\('''
@@ -546,6 +564,12 @@ def main():
546
564
seleniumbase_lines .append (line )
547
565
continue
548
566
567
+ # Is there a Select() still present?
568
+ lines = seleniumbase_lines
569
+ for line_num in range (len (lines )):
570
+ if "Select(self.driver" in lines [line_num ]:
571
+ uses_select = True
572
+
549
573
# Remove duplicate functionality (wait_for_element)
550
574
lines = seleniumbase_lines
551
575
seleniumbase_lines = []
@@ -596,6 +620,9 @@ def main():
596
620
if uses_keys :
597
621
seleniumbase_code += (
598
622
"from selenium.webdriver.common.keys import Keys\n " )
623
+ if uses_select :
624
+ seleniumbase_code += (
625
+ "from selenium.webdriver.support.ui import Select\n " )
599
626
for line in seleniumbase_lines :
600
627
seleniumbase_code += line
601
628
seleniumbase_code += "\n "
0 commit comments