Typing speed test automation with Python
Automate the typing test websites.
In this post, I will show you how to automate the typing test websites which is really fun and by building this project you will improve your automation skills with Selenium.
Basically, these kinds of websites are generating a collection of words and allow user to test their typing speed by measuring the entered word count. I always wanted to take first place but usually, my speed was not higher than a typical programmer. So, I decided to automate this action by creating a typing bot with the help of Selenium and Python.
I am assuming that you have already created a new working directory so let's start by creating a virtual environment to isolate dependencies and then install Selenium in order to work with automation:
virtualenv env
env\Scripts\activate
pip install selenium
If you are using Linux as an operating system then you can activate the virtual environment by the following command:
virtualenv env
source env/bin/activate
Once you installed the selenium, then we will need a chrome driver to execute selenium scripts in Chrome as well as automate any website on the internet. First, you need to know which version of chromedriver is compatible with your Chrome browser. If you don't know the current version of your browser then follow the steps below:
- Open Google Chrome
- Click the three dots in the upper right corner of the window
- Hover your cursor over the "Help" option
- Click the "About Google Chrome"
The current version of your Chrome browser will appear on the next window toward the top of the screen. At the time of writing this post, the last version is 87 so I must download chromedriver 87 in this case. Go to the following link in order to download chromedriver and select the proper version from the list:
https://chromedriver.chromium.org
Once you downloaded, extract the zip folder and copy chromedriver.exe
and paste it to your current working directory. Great! Now, we have our driver so it's time to test if works properly. Start by importing the webdriver
from the selenium module then create a new variable named driver and assign it to the executable path of Chrome.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'path\to\chromedriver.exe')
The executable path must be correctly pointed to the chromedriver
and since we copied the driver in our working directory you can easily find the path by checking properties of this file from file explorer. As you noticed the path is surrounded with r' '
quotes which means the string will be treated as a raw string and all escape codes will be ignored. Alternatively, you can use double forward slash but for now, let's keep it in this way. It's time to review our target website by navigating the following URL:
https://typing-speed-test.aoeu.eu
Basically, you will see a container filled with a collection of random words and the user have to type each word then click to space button to get the next words. Copy the URL above and pass it into driver.get()
function like below:
driver.get("https://typing-speed-test.aoeu.eu")
Now, we can launch the script to see if it's actually working. Open command prompt or terminal, navigate your current directory and start the program. I named the file typing.py
so in my case I will run the following command:
python typing.py
If it didn't work, please check the executable path of your driver as well as the version compatibility.
Next, open inspect element by pressing f12
button on your keyboard then we need to get the active word which platform expects to be entered inside the input area. Now, if you hover the cursor on the active word you will it has an ID property named currentword
.
Try to type the words and you will see this ID will be assigned to the next word each time which means there are a few milliseconds of delay while assigning the ID. Selenium provides explicit waits which are waiting for certain conditions to occur before proceeding further in the code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
word = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((BY.ID, "currentword")))
We are going to use the expected condition function which returns a boolean true in case of success or not null if it fails to locate the element and by using WebDriverWait
selenium will wait a maximum of 10 seconds for an element matching the given criteria to be found. Once we get the current word it needs to be passed inside input and if you inspect that element on the website you will see it has an ID named input so let's define this element by using standard selenium locators:
input_area = driver.find_element_by_id("input")
input_area.click()
input_area.send_keys(word.text)
In order to start the timer, we have to click the input field because there is a click listener which triggers the timer once the input state becomes focused. Then, the bot must click the space button to get the next word and we can achieve that by importing Keys
module:
from selenium.webdriver.common.keys import Keys
input_area = driver.find_element_by_id("input")
input_area.click()
input_area.send_keys(word.text)
input_area.send_keys(Keys.SPACE)
The main structure of the program is ready. Now, try to run the program and you will see the automation only applied for the first word, not all of them. So, this process needs to be executed continuously once all the words are finished.
At this point, we will use for loop with range function to iterate through the words. Open inspect element again to check other words and you will see they have the same class name which is nextword
so we can use it to find a total number of words.
total_words = len(driver.find_elements_by_class_name("nextword"))
By using find_elements_by_class_name()
locator we can get the list of these elements and since we only need the length, let's surround it with len()
function. After that, define a for loop with a range function to iterate through the list and indent the rest of the code to complete everything. The full code will look like below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path=r'path\to\chromedriver.exe')
driver.get("https://typing-speed-test.aoeu.eu")
total_words = len(driver.find_elements_by_class_name("nextword"))
for word in range(total_words + 1):
word = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((BY.ID, "currentword")))
input_area = driver.find_element_by_id("input")
input_area.click()
input_area.send_keys(word.text)
input_area.send_keys(Keys.SPACE)
As you noticed, we added plus one to the length in order to calculate the first word as well because it will be not included in to list due to the assigned ID.
Great! Now run the program and enjoy watching how fast actually automation is typing. You definitely will take first place!