Skip to content

Commit 597e61b

Browse files
committed
Update Selenium IDE conversion tool
1 parent 44a05bb commit 597e61b

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

integrations/selenium_ide/convert_ide.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,96 @@ def main():
240240
seleniumbase_lines.append(command)
241241
continue
242242

243+
# Handle self.is_element_present(By.LINK_TEXT, *)
244+
data = re.match(
245+
'''^(\s*)([\S\s]*)self\.is_element_present\(By.LINK_TEXT, '''
246+
'''u?\"([\S\s]+)\"\)([\S\s]*)$''', line)
247+
if data:
248+
whitespace = data.group(1)
249+
pre = data.group(2)
250+
link_text = '''%s''' % data.group(3)
251+
post = data.group(4)
252+
uni = ""
253+
if '(u"' in line:
254+
uni = "u"
255+
has_unicode = True
256+
command = '''%s%sself.is_link_text_present(%s"%s")%s''' % (
257+
whitespace, pre, uni, link_text, post)
258+
seleniumbase_lines.append(command)
259+
continue
260+
261+
# Handle self.is_element_present(By.NAME, *)
262+
data = re.match(
263+
'''^(\s*)([\S\s]*)self\.is_element_present\(By.NAME, '''
264+
'''u?\"([\S\s]+)\"\)([\S\s]*)$''', line)
265+
if data:
266+
whitespace = data.group(1)
267+
pre = data.group(2)
268+
name = '''%s''' % data.group(3)
269+
post = data.group(4)
270+
uni = ""
271+
if '(u"' in line:
272+
uni = "u"
273+
has_unicode = True
274+
command = '''%s%sself.is_element_present('[name="%s"]')%s''' % (
275+
whitespace, pre, name, post)
276+
seleniumbase_lines.append(command)
277+
continue
278+
279+
# Handle self.is_element_present(By.ID, *)
280+
data = re.match(
281+
'''^(\s*)([\S\s]*)self\.is_element_present\(By.ID, '''
282+
'''u?\"([\S\s]+)\"\)([\S\s]*)$''', line)
283+
if data:
284+
whitespace = data.group(1)
285+
pre = data.group(2)
286+
the_id = '''%s''' % data.group(3)
287+
post = data.group(4)
288+
uni = ""
289+
if '(u"' in line:
290+
uni = "u"
291+
has_unicode = True
292+
command = '''%s%sself.is_element_present("#%s")%s''' % (
293+
whitespace, pre, the_id, post)
294+
seleniumbase_lines.append(command)
295+
continue
296+
297+
# Handle self.is_element_present(By.CLASS, *)
298+
data = re.match(
299+
'''^(\s*)([\S\s]*)self\.is_element_present\(By.CLASS, '''
300+
'''u?\"([\S\s]+)\"\)([\S\s]*)$''', line)
301+
if data:
302+
whitespace = data.group(1)
303+
pre = data.group(2)
304+
the_class = '''%s''' % data.group(3)
305+
post = data.group(4)
306+
uni = ""
307+
if '(u"' in line:
308+
uni = "u"
309+
has_unicode = True
310+
command = '''%s%sself.is_element_present(".%s")%s''' % (
311+
whitespace, pre, the_class, post)
312+
seleniumbase_lines.append(command)
313+
continue
314+
315+
# Handle self.is_element_present(By.XPATH, *)
316+
data = re.match(
317+
'''^(\s*)([\S\s]*)self\.is_element_present\(By.XPATH, '''
318+
'''u?\"([\S\s]+)\"\)([\S\s]*)$''', line)
319+
if data:
320+
whitespace = data.group(1)
321+
pre = data.group(2)
322+
xpath = '''%s''' % data.group(3)
323+
post = data.group(4)
324+
uni = ""
325+
if '(u"' in line:
326+
uni = "u"
327+
has_unicode = True
328+
command = '''%s%sself.is_element_present("%s")%s''' % (
329+
whitespace, pre, xpath, post)
330+
seleniumbase_lines.append(command)
331+
continue
332+
243333
# Replace "self.base_url" with actual url if not already done
244334
if 'self.base_url' in line:
245335
line = line.replace("self.base_url", '"%s"' % ide_base_url)
@@ -251,6 +341,57 @@ def main():
251341
# Add all other lines to final script without making changes
252342
seleniumbase_lines.append(line)
253343

344+
# Chunk processing of inefficient waiting from Selenium IDE
345+
in_inefficient_wait = False
346+
whitespace = ""
347+
lines = seleniumbase_lines
348+
seleniumbase_lines = []
349+
for line in lines:
350+
data = re.match('^(\s*)for i in range\(60\):\s*$', line)
351+
if data:
352+
in_inefficient_wait = True
353+
whitespace = data.group(1)
354+
continue
355+
356+
data = re.match('^(\s*)else: self.fail\("time out"\)\s*$', line)
357+
if data:
358+
in_inefficient_wait = False
359+
continue
360+
361+
if in_inefficient_wait:
362+
data = re.match('''^\s*if self.is_element_present\("([\S\s]+)"\)'''
363+
''': break\s*$''', line)
364+
if data:
365+
selector = data.group(1)
366+
command = '%sself.wait_for_element("%s")' % (
367+
whitespace, selector)
368+
seleniumbase_lines.append(command)
369+
continue
370+
371+
data = re.match('''^\s*if self.is_element_present\('([\S\s]+)'\)'''
372+
''': break\s*$''', line)
373+
if data:
374+
selector = data.group(1)
375+
command = "%sself.wait_for_element('%s')" % (
376+
whitespace, selector)
377+
seleniumbase_lines.append(command)
378+
continue
379+
380+
data = re.match('''^\s*if self.is_link_text_present'''
381+
'''\("([\S\s]+)"\): break\s*$''', line)
382+
if data:
383+
uni = ""
384+
if '(u"' in line:
385+
uni = "u"
386+
link_text = data.group(1)
387+
command = '''%sself.wait_for_link_text(%s"%s")''' % (
388+
whitespace, uni, link_text)
389+
seleniumbase_lines.append(command)
390+
continue
391+
else:
392+
seleniumbase_lines.append(line)
393+
continue
394+
254395
seleniumbase_code = ""
255396
if has_unicode:
256397
seleniumbase_code = "# -*- coding: utf-8 -*-\n"

0 commit comments

Comments
 (0)