Joao Correia

Web analytics regression testing with WAUnit

WAUnit is a web analytics regression testing framework I created. It is written in Python and leverages Selenium webdriver and a built-in proxy server to test web analytics implementations.

With WAUnit you can automagically verify your entire web analytics implementation!

WAUnit Google Store test results

Currently, it only supports Google Analytics, but support for other platforms is under way.

How does it work?

I’m going to use the Google Store as an example. When a user clicks the ‘BUY FROM $499.00’ button (marked in red below), an event with the following specifications is fired:

Snowplow Analytics

WAUnit is composed of two main components:

  1. The local proxy is started using a shell command: waunitproxy.

  2. A simple a unit test using Selenium programmatically visits the Nexus 6P page and clicks the ‘Buy from $499.00’ button.

  3. Meanwhile, the local proxy intercepts the web analytics hit.

  4. The unit test connects and compares the proxy hit log with the specification. If the specification matches, the unit test will pass, if not it will fail and provide information on what doesn’t match.

WAUnit Google Store test results

WAUnit allows you to verify web analytics implementations, automatically. Trust in the implementation is invaluable to the success of web analytics.

See a 5 minute video with the complete Google Store example (view full screen)

Installation & Google Store Demo

Install WAUnit using Python package manager or download WAUnit from GitHub

pip install waunit

Create a project folder for the sample unit test. We will use it to store the configuration file and the unit tests.

Create a file named: waunit.cfg and save it to your project folder.

[WAUnit]
# Only googleanalyticsuniversal is available right now
parser = googleanalyticsuniversal

[Proxy]
host = 127.0.0.1
port = 8080
hitsdb = hitsdata

Open a shell in you project folder and launch the proxy server, the default configuration file should work fine.

waunitproxy --config waunit.cfg

Configure firefox to use the local proxy 127.0.0.1:8080. Go to a website and verify you can see the google analytics hits in the proxy console.

Open your favorite Python editor and paste the code below in a file named: sample.py. Edit line 9 to reflect your WAUnit configuration file.

import unittest
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy,ProxyType
from selenium.webdriver.common.keys import Keys
import time
import shelve

import ConfigParser
config = ConfigParser.RawConfigParser()

# Insert the path to your configuration file
config.read('waunit.cfg')

from WAUnit import *


class WAUnitDemo(unittest.TestCase):

    def setUp(self):
        myProxy = config.get('Proxy', 'host')+':'+config.get('Proxy', 'port')

        proxy = Proxy({
            'proxyType': ProxyType.MANUAL,
            'httpProxy': myProxy,
            'ftpProxy': myProxy,
            'sslProxy': myProxy,
            'noProxy': '' # set this value as desired
            })

        self.driver = webdriver.Firefox(proxy=proxy)
        self.driver.set_window_size(920, 860)
        #self.driver = WebDriver('firefox', proxy=proxy, reuse_browser=True)

    def test1_google_store_buy_now_click(self):
        """
        The test external link verifies if external tracking is working
        """
        utid = '001'

        driver = self.driver
        driver.get("https://store.google.com/product/nexus_6p?utid="+utid)

        # Click Button Buy From $499
        driver.find_element_by_class_name('transaction').click()
        time.sleep(2)

        driver.close()

        d = shelve.open(config.get('Proxy', 'hitsdb'))
        proxy_utid = d[utid]
        d.close()

        spec_utid = {}
        spec_utid = { 'hitType' : 'event',
                      'eventAction' : 'Buy Now',
                      'eventCategory' : 'Checkout Funnel',
                      'eventLabel' : 'nexus_6p'}

        verification = verify_spec(spec_utid, proxy_utid)

        self.assertNotIn('False',verification)



    def test2_google_store_2add_to_cart(self):
        """
        The test external link verifies if external tracking is working
        """
        utid = '002'

        driver = self.driver
        driver.get("https://store.google.com/product/nexus_6p?utid="+utid)

        # Click Button Buy From $499
        driver.find_element_by_class_name('transaction').click()

        # Select the 32GB
        driver.find_element_by_xpath("//button[@data-variation-id='32']").click()
        time.sleep(2)

        # Scroll all the way down
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # Add to Cart
        driver.find_element_by_xpath("//button[@data-backend-docid='_nexus_6p_aluminium_32gb_lte']").click()
        time.sleep(2)

        d = shelve.open(config.get('Proxy', 'hitsdb'))
        proxy_utid = d[utid]
        d.close()

        spec_utid = {}
        spec_utid = { 'hitType' : 'event',
                      'eventAction' : 'Add to Cart',
                      'eventCategory' : 'Checkout Funnel',
                      'eventLabel' : 'nexus_6p'}
        verification = verify_spec(spec_utid, proxy_utid)

        self.assertNotIn('False',verification)


    def tearDown(self):
        driver = self.driver
        driver.quit()


suite = unittest.TestLoader().loadTestsFromTestCase(WAUnitDemo)
unittest.TextTestRunner(verbosity=2).run(suite)

In the shell run sample.py. A Firefox window should open automatically and you should see the hits to Google Analytics in the proxy console.

python sample.py

WAUnit Google Store test results

WAUnit will soon support other web analytics platforms. Make sure you follow and star WAUnit on GitHub.


WRITTEN BY

Joao Correia

SHARE OR SAVE IT FOR LATER