Export Selenium IDE Tests to Code, and When to Graduate
On this page
Every Selenium IDE command maps to a Selenium WebDriver call, so the IDE can write your tests out as code. It is a good way to see what a recorded test really does, and a reasonable first step when a suite has outgrown the table. It is not a finished test suite. This chapter exports the course project to Python, runs what came out, and shows what had to change.
How to export#
In the IDE, right-click a test in the Tests tab and choose Export test(s) to, or right-click a suite in the Suites tab and choose Export suite(s) to, then pick a language. Version 4.1.0-beta.3 ships exporters for:
| Language | Test framework |
|---|---|
| Java | JUnit |
| Python | pytest |
| JavaScript | Mocha |
| Ruby | RSpec |
| C# | NUnit and xUnit |
The exporters are separate npm packages (such as @seleniumhq/code-export-python-pytest), and the IDE uses the same packages. We ran the 4.1.0-beta.3 exporters from the command line on the-internet.side, once per test and once per suite, and ran the Python results with pytest 9 and Selenium 4.52.
Export suites, not single tests, when they use run. The per-test export of "Log in and log out" called self.login() but did not contain a login method, so it failed with AttributeError: 'TestLoginandlogout' object has no attribute 'login'. The suite export wrote the shared Log in test as a login method in the same class.
What came out#
Here is the exported "Log in with valid credentials", unedited:
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestLoginwithvalidcredentials():
def setup_method(self, method):
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_loginwithvalidcredentials(self):
self.driver.get("https://the-internet.herokuapp.com/login")
self.driver.find_element(By.ID, "username").send_keys("tomsmith")
self.driver.find_element(By.ID, "password").send_keys("SuperSecretPassword!")
self.driver.find_element(By.CSS_SELECTOR, ".radius").click()
elements = self.driver.find_elements(By.CSS_SELECTOR, "#flash.success")
assert len(elements) > 0
assert self.driver.find_element(By.CSS_SELECTOR, "h2").text == "Secure Area"
# Only on TestingBot: name the session and mark it passed
if self.driver.execute_script("return (arguments[0] === 'yes')", self.vars["env:TB_REPORT"]):
pass
The commands are all there and readable, which is the value of the export. Then we ran it, and all eight exported tests failed.
What the export gets wrong#
1. Waiting is lost. In the IDE, assertElementPresent waits up to the step timeout for the element. The export checks once, straight after the click:
> assert len(elements) > 0
E assert 0 > 0
The secure page had not loaded yet. This is the timing problem from chapter 3 again, and the reason a recorded test that is solid in the IDE can be flaky as code. Commands with waitFor in their name do become waits (waitForElementVisible turned into a WebDriverWait with visibility_of_element_located), but the implicit waiting of other commands does not carry over.
2. Environment variables are not translated. ${env:TB_REPORT} became self.vars["env:TB_REPORT"], a dictionary key that never exists:
E KeyError: 'env:TB_REPORT'
And the two executeScript steps inside that if were dropped; the block only contains pass.
3. Some commands are dropped. In "Store and reuse values", storeTitle | pageTitle produced no line at all, so the next assertion failed with KeyError: 'pageTitle'.
4. The browser is hard-coded. setup_method always starts a local webdriver.Chrome(). There is no option for Firefox or a remote grid.
5. The structure is one class per file with names like TestLoginwithvalidcredentials, copies of the same locators in every test, and no fixtures.
The Java export has the same shape. One extra trap there: it checks element presence with Java's assert keyword, which the JVM skips unless you start it with -ea, so that check silently does nothing.
None of this makes the export useless. Treat it as a translation draft: it tells you which WebDriver calls a recorded step corresponds to, and you rewrite it into a proper test.
A test that never fails in the IDE fails now and then after export, on a line that checks an element straight after a click. Why?
The locator is the same; the waiting is not. Add an explicit wait (WebDriverWait with an expected condition) for something that only exists after the click.
What the cleaned-up version looks like#
The same two login tests, rewritten by hand. A pytest fixture owns the browser, the login is a function with parameters instead of a run with stored variables, and each check waits for the page it needs:
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
BASE_URL = "https://the-internet.herokuapp.com"
@pytest.fixture
def driver():
driver = webdriver.Chrome()
yield driver
driver.quit()
def log_in(driver, username, password):
driver.get(f"{BASE_URL}/login")
driver.find_element(By.ID, "username").send_keys(username)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element(By.CSS_SELECTOR, ".radius").click()
def test_log_in_with_valid_credentials(driver):
log_in(driver, "tomsmith", "SuperSecretPassword!")
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#flash.success")))
assert driver.find_element(By.CSS_SELECTOR, "h2").text == "Secure Area"
def test_wrong_password_shows_an_error(driver):
log_in(driver, "tomsmith", "not-the-password")
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#flash.error")))
assert not driver.find_elements(By.CSS_SELECTOR, "#flash.success")
$ pytest -q test_login.py
2 passed in 5.65s
It passed three runs in a row with Selenium 4.52.0. The next steps, a browser option, a remote grid and page objects, are what the code courses below teach.
When to graduate from the IDE#
There is no line count at which you must switch. These are the signals that the table is costing more than it saves:
-
You store five variables before every
run. You want functions with parameters. - The same locator is in twenty tests. When it changes, you want to fix it in one place: that is a page object.
- Tests need data you cannot click together: a fresh user from an API, rows in a database, values from a file.
-
Several people edit the suite. JSON diffs and merge conflicts in
.sidefiles are painful; code reviews are not. -
executeScriptsteps are growing. JavaScript in a table cell is code without an editor, linting or tests.
A common and healthy split is to keep a small Selenium IDE smoke suite that non-developers own, and write the rest in code.
Where to go next#
Pick the course for the language your team uses. Each one starts from an empty folder and ends on a cloud grid:
- Python: Selenium with Python and pytest, the natural next step after this chapter's example.
- Java: Selenium with TestNG, for Java teams and Maven projects.
- Structure: Page Object Model, the pattern that solves "the same locator in twenty tests".
- Browsers: Cross-Browser Testing, when the question becomes which browsers to test and why they differ.