github seleniumbase/SeleniumBase v1.49.16
Update the script that converts SeleniumIDE exports into SeleniumBase format

latest releases: v4.31.2, v4.31.1, v4.31.0...
4 years ago

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:

See also: https://m.youtube.com/watch?v=Sjzq9kU5kOw

Don't miss a new SeleniumBase release

NewReleases is sending notifications on new releases.