NewAutoUser
NewAutoUser

Reputation: 573

Google analytics automation testing

The ask is, we have a website which triggers multiple events based on user's action and we wanted to simulate those scenarios using automation script and also needs to understand if Google Analytics events has been raised behind the scene.

Just curious whether is there any tool available in market which can help us to automate. Thanks!

Upvotes: 0

Views: 1316

Answers (1)

Cynic
Cynic

Reputation: 7216

Install Chrome Extension Source Viewer. Go to the analytics debugger extension in the store and use the Extension Source Viewer to download a zip file of the extension. Open background.js and edit debug = false (line 4 currently) to debug = true.

In Chrome Browser, go the Extensions Window, turn on Dev Mode (checkbox in that window). Use the Pack Extension button and select the folder you just edited to make a file called ga_tracker.crx.

Drop that crx file into your project. For example I copied it into my virtualenv.

test.py
env/
    bin/
        ga_tracker.crx

And this is Python Selenium test test.py. Edit the path for add_extension if you put it somewhere else.

import re
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

class FindTest():

    def test(self):

        self.chrome_options = webdriver.ChromeOptions()
        self.chrome_options.add_extension('env/bin/ga_tracker.crx')
        self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
        self.driver.get('https://www.localsbarguide.com')
        for entry in self.driver.get_log('browser'):
            print(entry)
            for entry in context.driver.get_log('browser'):
                if 'https://www.google-analytics.com/analytics_debug.js' in entry['message']:
                     my_regex = re.escape('title') + r".*." + re.escape('The Premiere Drink Special & Happy Hour resource | Locals Bar Guide San Francisco')
                     if re.search(my_regex, entry, re.IGNORECASE):
                         print('Found GA event with expected title.')

        self.driver.quit()

runtest = FindTest()
runtest.test()

Upvotes: 0

Related Questions