We have a requirement to check the size of some of our website home pages, in MB. This is to ensure that they are not growing too large, and that large images haven't been uploaded. I couldn't find much info on this, but have come up with following which is working as required.
The code is designed to either run locally on my machine, or by leveraging our Selenium grid. It simply loads home page, and from the browser Performance log, we strip out the Network.dataReceived (bytes) information and sum it.
Finally, we have set levels which the site should fall within.
#!/usr/bin/python
""" This script will simply check the download page size (bytes) of a Home page."""
import argparse
import re
import sys
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
parser = argparse.ArgumentParser(description="This script will measure size of a home page.")
parser.add_argument('--site', default='somewebsite.com', required=True)
parser.add_argument('--local', action='store_true', default=False)
parser.add_argument('--datacentre', choices=['dc1', 'dc2'])
args = parser.parse_args()
logging_prefs = {'performance' : 'INFO'}
if args.local:
caps = DesiredCapabilities.CHROME.copy()
caps['loggingPrefs'] = logging_prefs
driver = webdriver.Chrome(desired_capabilities=caps)
else:
profile = webdriver.FirefoxProfile()
profile.set_preference('plugin.state.flash', 0)
profile.set_preference("webdriver_enable_native_events", False)
profile.update_preferences()
caps = DesiredCapabilities.FIREFOX.copy()
caps['loggingPrefs'], caps['acceptSslCerts'] = logging_prefs, False
if args.datacentre == 'dc1':
driver = webdriver.Remote(
command_executor='http://selenium/hub',
desired_capabilities=caps,
browser_profile=profile)
elif args.datacentre == 'dc2':
driver = webdriver.Remote(
command_executor='http://selenium/hub',
desired_capabilities=caps,
browser_profile=profile)
driver.implicitly_wait(30)
driver.set_page_load_timeout(30)
url = "http://" + args.site + "/"
driver.get(url)
total_bytes = []
try:
for entry in driver.get_log('performance'):
if "Network.dataReceived" in str(entry):
r = re.search(r'encodedDataLength\":(.*?),', str(entry))
total_bytes.append(int(r.group(1)))
except Exception:
print 'error'
driver.close()
sys.exit(3)
if total_bytes is not None:
mb = round((float(sum(total_bytes) / 1000) / 1000), 2)
if args.local:
from datetime import datetime
d = (datetime.today()).strftime("%d-%m-%y-%H-%M")
filename = 'results_{}.txt'.format(str(d))
with open(filename, 'a') as f:
f.write("{}, {}\n".format(args.site, mb))
try:
if mb < 2.0:
print "OK. Total Network Data Received size for {}: {}MB".format(args.site, str(mb))
sys.exit(0)
elif mb >= 2.0 and mb < 4.0:
print "Warning. Total Network Data Received size for {}: {}MB".format(args.site, str(mb))
sys.exit(1)
elif mb > 4.0:
print "CRITICAL. Total Network Data Received size for {}: {}MB".format(args.site, str(mb))
sys.exit(1)
except Exception:
print "UNKNOWN. Something went wrong."
sys.exit(3)
finally:
driver.close()