Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate test google analytics on every event fired using Selenium WebDriver

I am working on Google Analytics automation test with Selenium WebDriver java bindings. Our site has Google Analytics tracking events set on important elements on the site. I need to verify that on clicking a certain element under test, the Google Analytic event is in fact fired.

I'm testing it on FireFox. When I click F12, I can see in the console that a Google Analytics is fired with message GET http://www.google-analytics.com/__utm.gif on each element click event.

    SampleCode :-
      WebDriver wd = new FirefoxDriver();
      wd.get("http://www.dummyExample.com/");
      wd.findElement(By.linkText("Document Referece")).click();
      wd.findElement(By.id("Ex2vc2")).click();

How can I achieve this task? I have googled it but could not find good answers. If anyone can help me with some references or sample code I would be very Thankful.

like image 743
Little bird Avatar asked Jul 25 '14 07:07

Little bird


1 Answers

  1. Good way to test GA is by using javascript. Example shows tests with phantomjs, because it's faster with js stuff, but i think you could use FF or other browsers as well. Also Author added github link with code to try it. The main approach of test is cool, but it may take time to create code for that and support it later.

http://viget.com/extend/testing-google-analytics-with-phantomjs

  1. Second way is to use HttpProxy. AFAIK there was tool - BrowserMobProxy. You'll be able to sniff all requests on testing website, but they in HAR format. So the algorithm is simple, enable proxy, open website, do actions, sniff requests, parse them and assert the values.

  2. Change endpoint and save all requests to assert them. So usually analytics sends requests to the same url, like 'http://google.com/analytics' or whatever. So, you can change this behavior on testing webserver. F.e. add this url to /etc/hosts with localhost address. And then all requests on webserver which are sent to GA domain will be received by webserver.

That will require extra programming. But it's the cleanest way for testing IMHO.

like image 114
kimandrew Avatar answered Nov 03 '22 01:11

kimandrew