Pages

Thursday, May 10, 2012

Http profiler using Selenium to track omniture calls


We often come across issues like missing tracking calls for omniture or beacon tracking. These calls are useful for various reasons like to generate hit map using beacon tracking where it tracks the mouse clicks of users, to track business metrics like page views, number of orders placed online, user engagement with page objects etc.

Traditionally while development we verify the calls using tools like firebug in Firefox or Httpwatch in IE but checking the calls for a end to end flow of order placement or many pages in a automated way could get tricky.

I would like to share how selenium can be used for this purpose. DefaultSelenium has captureNetworkTraffic() method that can be used for this purpose. This method can be used to track omniture , beacon tracking calls for complete page views as well as ajax calls.


Sample code
 
package com.example.common;

import com.thoughtworks.selenium.Wait;
import fitlibrary.SequenceFixture;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.StringReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.logging.Logger;

public class Omniture extends SequenceFixture {
    public static final String TIMEOUT = "120000";
    Logger logger = Logger.getLogger(Omniture.class.getName());
    static Properties props = new Properties();
 
    String omnitureCall;
 
    // This method validate presence of omniture call in
    // an action like openPage
 
    public boolean validateOmnitureCall(String calls) {
        BufferedReader reader = new BufferedReader(new StringReader(calls));
        String strline;
        try {
            while ((strline = reader.readLine()) != null) {


                if (strline.contains("200 GET http://domain-name.d1.sc.net/b/ss/")) {

                    omnitureCall = strline;
                    return true;
                }
                if (strline.contains("200 GET https://domain-name.d1.sc.net:443/b/ss/")) {

                    omnitureCall = strline;
                    return true;
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("omniture call not found.");
        return false;
    }

// This method validate each omniture params mentioned in
//Omniture.properties file with the actual omniture call for
//the action like openPage

    public boolean validateOmnitureParams(String page, String action) {

        try {
            FileInputStream f = new FileInputStream(config.locators + "/Omniture.properties");
            props.load(f);
            omnitureCall = URLDecoder.decode(omnitureCall);

            String omnitureQueryParamns = omnitureCall.substring(omnitureCall.indexOf("?") + 1);

            StringTokenizer stringTokenizer = new StringTokenizer(omnitureQueryParamns, "&");

            while (stringTokenizer.hasMoreElements()) {
                String param = stringTokenizer.nextToken();
                String paramArray[] = param.split("=");
                String paramName = paramArray[0];
                String paramValue = paramArray[1];

                String value = props.getProperty(page + "." + action + "." + paramName);

                if (!(value == null)){
                    if (!paramValue.contains(value)) {
                        logger.info("page "+page + " action " + action + " paramName " + paramName );
                        logger.info("Omniture param value defined in Omniture.properties for " + paramName + " is  " + value);
                        logger.info("Omniture param value in omniture call for " + paramName + " is " + paramValue);
                        return false;

                    }
                }
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }


// A open page call using selenium to open a webpage
 
     public boolean openPage(String page, String url) throws Exception {
        config.selenium.open(url);
        String calls = config.selenium.captureNetworkTraffic("plain");
        config.selenium.waitForPageToLoad(TIMEOUT);
        setWalletBalance();

        if (!validateOmnitureCall(calls)) {
            return false;
        }
        if (!validateOmnitureParams(page, "openPage")) {
            return false;
        }

        return true;
    }


Sample properties file
       
 Omniture.properties
         
 Homepage.openPage.v1 = Home
 Homepage.openPage.v2 = Home
 Homepage.openPage.v3 = Homepage
 Homepage.openPage.pageName = Homepage