2020
2121
2222def 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" )
2525 num_args = len (sys .argv )
2626 if sys .argv [0 ].split ('/' )[- 1 ] == "seleniumbase" or (
2727 sys .argv [0 ].split ('\\ ' )[- 1 ] == "seleniumbase" ):
2828 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 '
3030 '"seleniumbase convert %s"\n ' % expected_arg )
3131 else :
3232 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 '
3434 '"python convert_ide.py %s"\n ' % expected_arg )
3535 webdriver_python_file = sys .argv [num_args - 1 ]
3636 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 "
3838 "Expecting: %s\n "
3939 % (webdriver_python_file , expected_arg ))
4040
@@ -47,15 +47,17 @@ def main():
4747 in_test_method = False
4848 has_unicode = False
4949 uses_keys = False
50+ uses_select = False
5051
5152 f = open (webdriver_python_file , 'r' )
5253 all_code = f .read ()
5354 f .close ()
5455 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 ))
5961 code_lines = all_code .split ('\n ' )
6062 for line in code_lines :
6163
@@ -312,6 +314,22 @@ def main():
312314 seleniumbase_lines .append (command )
313315 continue
314316
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+
315333 # Handle Select / by_xpath() / select_by_visible_text()
316334 data = re .match (
317335 '''^(\s*)Select\(driver\.find_element_by_xpath\('''
@@ -546,6 +564,12 @@ def main():
546564 seleniumbase_lines .append (line )
547565 continue
548566
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+
549573 # Remove duplicate functionality (wait_for_element)
550574 lines = seleniumbase_lines
551575 seleniumbase_lines = []
@@ -596,6 +620,9 @@ def main():
596620 if uses_keys :
597621 seleniumbase_code += (
598622 "from selenium.webdriver.common.keys import Keys\n " )
623+ if uses_select :
624+ seleniumbase_code += (
625+ "from selenium.webdriver.support.ui import Select\n " )
599626 for line in seleniumbase_lines :
600627 seleniumbase_code += line
601628 seleniumbase_code += "\n "
0 commit comments