DEV Community

Cover image for Automate the typing test websites with Python
Rashid
Rashid

Posted on

Automate the typing test websites with Python

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 kind of websites are generating collection of words and allows user to test his typing speed by measuring the entered word count. I always wanted to took first place but usually my speed was not higher than 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
Enter fullscreen mode Exit fullscreen mode

If you are using Linux as operating system then you can activate the virtual environment by following command:

virtualenv env
source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Once you installed the selenium, then we will need chromedriver 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:

1.Open Google Chrome
2.Click the three dots in the upper right corner of the window
3.Hover your cursor over the "Help" option
4.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 selenium module then create a new variable named driver and assign it to executable path of Chrome.

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'path\to\chromedriver.exe')
Enter fullscreen mode Exit fullscreen mode

The executable path must be correctly pointed to the chromedriver and since we copied the driver in our working directory you can easily find path by checking properties of this file from file explorer. As you noticed the path surrounded with r' '  quotes that 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 collection of random words and user have to type each word then click to space button to get next words. Copy the URL above and pass into driver.get() function like below:

driver.get("https://typing-speed-test.aoeu.eu")
Enter fullscreen mode Exit fullscreen mode

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 script by typing python filename.py. I named the file typing.py so in my case I will run the following command:

python typing.py
Enter fullscreen mode Exit fullscreen mode

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 input area. Now, if you hover the cursor on the active word you will it has ID property named currentword. 

Try to type the words and you will see this ID will be assigned to next word each time which means there are few milliseconds of delay while assigning the ID. Selenium provides explicit waits which is waiting for certain condition 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")))
Enter fullscreen mode Exit fullscreen mode

We are going to use 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 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 website you will see it has 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)
Enter fullscreen mode Exit fullscreen mode

In order to start the timer, we have to click the input field because there is a click listener which triggers timer once the input state becomes focused. Then, the program 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)
Enter fullscreen mode Exit fullscreen mode

Great! 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 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 same class name which is nextword so we can use it to find total number of words.

total_words = len(driver.find_elements_by_class_name("nextword"))
Enter fullscreen mode Exit fullscreen mode

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 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)
Enter fullscreen mode Exit fullscreen mode

As you noticed, we added plus one to the length in order to calculate first word as well beacuse it will be not included 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 the first place!

WATCH THE RESULT ON YOUTUBE

coderasha

Follow me on IG for more: @coderasha

Thumbnail References:
Robot Designed by Freepik
Typewriter - Designed by Zirconicusso

Top comments (3)

Collapse
 
adishjain profile image
Adish Jain

Hey coderasha! I really enjoyed this tutorial & made a more interactive version of it here: getleaf.app/dsps301/automatethetyp.... I wanted to DM you but I couldn't find your contact info. If you have some time to talk, let's get in touch and discuss more!

Collapse
 
thedevtimeline profile image
Rashid

Oh wow! That's really creative way of showing tutorials👍

Collapse
 
adishjain profile image
Adish Jain

Thanks, appreciate it! Would you be down to get on a call & talk sometime? Would love to hear your thoughts