Automated testing is the application of software tools to automate a human-driven manual process of reviewing and validating a software product. Selenium is an open source umbrella project for a range of tools and libraries aimed at supporting browser automation. It provides a playback tool for authoring functional tests across most modern web browsers, without the need to learn a test scripting language. We will use Selenium in assignment 4 to automate the testing of a web-page.
For this class we are going to use Selenium WebDriver and Python to automate the testing of a webpage.
To install Selenium with Python, follow the instructions given at
Selenium, Python Package Index (PyPI). If you have
pip (package installer for Python) on your system, you
can simply install or upgrade the Python bindings. You can run the following from a command prompt. If you are using
Visual Studio Code, it is best to run this from within VS Code (from Terminal under the View menu):
py -m pip install -U selenium
Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver, which needs to be installed before any tests can be run. Make sure it's in your PATH, e.g., place it in /usr/bin or /usr/local/bin. On Windows, see How to set the path and environment variables in Windows by Computer Hope. For instance, on my machine, geckodriver appears under C:\Program Files\Geckodriver and has been added to the PATH environment variable. Failure to add geckodriver will give you an error selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. Other supported browsers will have their own drivers available. See the documentation at Selenium, Python Package Index (PyPI).
For full documentation on how to use Selenium, see Documentation, WebDriver. Sample scripts can be seen at Test1.py and Test2.py. The code for both tests are duplicated here.
Test1.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Firefox()
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element(By.NAME, 'q')
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
time.sleep(10)
driver.close()
The above test imports webdriver, Keys and By from Selenium and time directly from Python.Test2.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Firefox()
driver.get("https://github-pages.senecapolytechnic.ca/see600/Selenium/Tests/Test2/TemperatureConverter.html")
print(driver.title)
tempF = driver.find_element(By.ID,'from')
tempF.send_keys(36)
convert = driver.find_element(By.ID, 'convert')
convert.send_keys(Keys.RETURN)
#time.sleep(1)
tempC = driver.find_element(By.ID,'to')
print(tempC.get_attribute('value'))
print(driver.current_url)
time.sleep(10)
driver.close()
The above test imports the same libraries as for Test1.py.As you can see from the above tests, Selenium can navigate through a web-page using JavaScript like syntax. For a JavaScript tutorial, see JavaScript Tutorial.
Test2Marks.py, Test2Marks2.py
The script Test2Marks.py tests the marks webpage found at Marks.html.
The script Test2Marks2.py performs an exhaustive test of the above marks webpage. Are there ways you could improve the test?
Test5Homes.py
The script Test5Homes.py tests the marks webpage found at Homes.html.
TestBank.py
The script TestBank.py demonstrates the use of the class name to group and test similar elements on a webpage. The bank webpage can be found at Bank.html.
For a good discussion on unit tests in Python, see the article A Beginner's Guide to Unit Tests in Python (2023). The following code tests the class Calculations, which simply has functions to calculate a sum, difference, product, and quotient of two numbers. To test the methods inside this class, we need to create a class based on the TestCase class and this class will contain methods that perform the tests. A test case is considered a single unit of testing, and it's represented by the TestCase class. See testCalc.py and testCalc2.py.
This second example tests form input into a webpage. In the code, note the use of setUp and tearDown. The setUp and tearDown methods are part of unittest framework in Python. The setUp method executes before the test method and the tearDown method executes after the test method. This example enters form data into the practice form webpage found at https://www.techlistic.com/p/selenium-practice-form.html. This Python script does not do any validation; it simply enters data into the forms. You can see the script at testForm.py. Note the 10 second delay at startup.
This third example enters some text in the search bar of the Python home page and tests to see if results have been found. The webpage is found at http://www.python.org. The code can be found at testPython.py.
The following script opens the Python home page and searches for Jamaica: Jamaica.py. The code is thoroughly documented. Refer to it for explanations.
The following script opens the CBC home page and searches for Jamaica: CBC.py. The code is thoroughly documented. Refer to it for explanations.
The following script opens the CNN home page and searches for Canada: CNN.py. The code is thoroughly documented. Refer to it for explanations.
The following script will prompt the user for an URL and a word/phrase, and will search through the homepage of the URL for that word/phrase, providing links for each: SearchURL.py.