QA Graphic

PyTest Example

Test Google Homepage using Google

Py Test Graphic

Over the past couple of years, I have been doing various automation programs. I have spent a lot of time using PyTest to test various websites and web objects.

If you are new to PyTest, here's a sample code that can help you get started. This piece of code will check to see if Google changed the text on the footer of the page.

The "/Library/Frameworks/Python.framework/Versions/3.9/bin/pytest" is the path to pytest, and makes this script easy to run from the command line.

#!/usr/bin/env /Library/Frameworks/Python.framework/Versions/3.9/bin/pytest
# Verify the Pytest was installed
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
def test_google():
	global chrome_driver
	chrome_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))    
	chrome_driver.get('https://www.google.com/')
	title = "Google"
	assert title == chrome_driver.title
	pageSource = chrome_driver.page_source
	gateway = "Our third decade of climate action"
	if not gateway in pageSource:
		pytest.fail( "Google Changed their Webpage")
	chrome_driver.close()

Install Modules

To get this to work, you need to install, PyTest, Selenium, and Webdriver. From the command line, run these commands:

pip3 install pytest
pip3 install selenium
pip3 install webdriver_manager