Suppose you work for GitHub and you are tasked with testing the login page, to ensure it works as intended with any type of data users might enter. You would usually get a bunch of test cases from QA with a list of actions to perform, such as filling the inputs with some good and/or bad data, clicking the login button when inputs are empty, etc. and ensuring the server returns the expected response.

testing gif

As I recently found myself in a similar situation, as part of my job, my first thought was “can I create something to do this task for me?". The answer is yes! With Selenium, we can record all those actions and play them back whenever we need to run such tests, instead of having to manually type data into forms, clicking buttons, and checking whether error messages match what QA expects. Here I will teach you some tricks to automate those actions, so that you can spend your time doing more productive things.



Creating your first test

Once you have installed the Selenium IDE plugin in your browser of choice:

  1. Open Selenium IDE
  2. Create a new project
  3. Create a new test case
  4. Click the REC button
  5. Enter the URL of the website/page to test
  6. Select Start recording
  7. Perform all the actions you would normally do
  8. Once you’re done typing data and clicking buttons, stop the recording
Gif showing how to record a Selenium IDE test

Your test should now contain a list of commands, with their associated targets and values, that Selenium has recorded for you. You can now play the test back and make sure that everything works as expected. If Selenium wasn’t able to find an element, you can try using the element’s XPath instead, which you can get through Selenium or from your browser’s Web Developer Tools.

Exporting your tests

After you’re done recording and tweaking your tests, export them to Python: this will allow you to do more complex things, like sourcing test data from csv files or REST APIs (coming soon!), and composing tests.

Selenium IDE: export tests

If everything went well, the exported Python file should contain the test we just created

# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from import By
from selenium.webdriver.common.action_chains import ActionChains
from import expected_conditions
from import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestTestLoginFail():
  def setup_method(self, method):
    self.driver = webdriver.Firefox()
    self.vars = {}
  def teardown_method(self, method):
  def test_testLoginFail(self):
    self.driver.set_window_size(1074, 792)
    self.driver.find_element(By.ID, "login_field").send_keys("[email protected]")
    self.driver.find_element(By.ID, "password").send_keys("password123")
    self.driver.find_element(By.NAME, "commit").click()

Now that we have exported out test, the only thing left to do is to run it using pytest. After installing the required packages in your virtual env and activating it

  • Windows
$ py -3 -m venv venv
$ source ./venv/bin/activate
$ py -3 -m pip install selenium pytest
  • *nix
$ python3 -m venv venv
$ source ./venv/Scripts/activate
$ python3 -m pip install selenium pytest

If you get an error message like “Message: ‘geckodriver’ executable needs to be in PATH”, you can either add the executable to your path or you can use webdriver-manager to take care of the problem for you (remember to install the package)

from selenium import webdriver
from import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager

class TestTestLoginFail():
  def setup_method(self, method):
    # choose the driver you need here
    self.chrome_driver = webdriver.Chrome(ChromeDriverManager().install())
    self.firefox_driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

The only thing left to do is to launch pytest and cross our fingers 🤞

running the test with pytest