Skip to content

Update the script that converts SeleniumIDE exports into SeleniumBase format #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2020

Conversation

mdmintz
Copy link
Member

@mdmintz mdmintz commented Sep 20, 2020

Update the script that converts SeleniumIDE exports into SeleniumBase format

The following is an example of a Katalon Recorder exported file (WebDriver + unittest format).
It is messy and has unnecessary lines of code to do the task that was recorded.

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Swag(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_swag(self):
        driver = self.driver
        driver.get("https://www.saucedemo.com/")
        driver.find_element_by_id("user-name").click()
        driver.find_element_by_id("user-name").clear()
        driver.find_element_by_id("user-name").send_keys("standard_user")
        driver.find_element_by_id("password").click()
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("secret_sauce")
        driver.find_element_by_id("login-button").click()
        driver.find_element_by_xpath("//div[@id='inventory_container']/div/div/div[3]/button").click()
        driver.find_element_by_css_selector("path").click()
        driver.find_element_by_link_text("CHECKOUT").click()
        driver.find_element_by_id("first-name").click()
        driver.find_element_by_id("first-name").clear()
        driver.find_element_by_id("first-name").send_keys("a")
        driver.find_element_by_id("last-name").click()
        driver.find_element_by_id("last-name").clear()
        driver.find_element_by_id("last-name").send_keys("b")
        driver.find_element_by_id("postal-code").click()
        driver.find_element_by_id("postal-code").clear()
        driver.find_element_by_id("postal-code").send_keys("12345")
        driver.find_element_by_xpath("//input[@value='CONTINUE']").click()
        driver.find_element_by_link_text("FINISH").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

This can be improved on.


After running seleniumbase convert [FILE] on it, here is the new result:

# -*- coding: utf-8 -*-
from seleniumbase import BaseCase


class Swag(BaseCase):

    def test_swag(self):
        self.open('https://www.saucedemo.com/')
        self.type('#user-name', 'standard_user')
        self.type('#password', 'secret_sauce')
        self.click('#login-button')
        self.click("//div[@id='inventory_container']/div/div/div[3]/button")
        self.click('path')
        self.click("link=CHECKOUT")
        self.type('#first-name', 'a')
        self.type('#last-name', 'b')
        self.type('#postal-code', '12345')
        self.click("//input[@value='CONTINUE']")
        self.click("link=FINISH")

This is much cleaner than the original version.
It also uses the more reliable SeleniumBase methods.


To learn more about SeleniumBase console scripts, see:

@mdmintz mdmintz merged commit 759469a into master Sep 20, 2020
@mdmintz mdmintz deleted the update-the-selenium-ide-export-converter branch September 20, 2020 15:13
@mdmintz mdmintz changed the title Update the tool for converting SeleniumIDE exports to SB format Update the tool for converting SeleniumIDE exports into SeleniumBase format Sep 20, 2020
@mdmintz mdmintz changed the title Update the tool for converting SeleniumIDE exports into SeleniumBase format Update the script that converts SeleniumIDE exports into SeleniumBase format Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant