/mixpanel-integration-testing

Tools to verify mixpanel integration and tracking by using Selenium Driver.

Primary LanguageRubyMIT LicenseMIT

Mixpanel integration testing tool

Description

This tool helps you to test your Mixpanel integration in your website by using Selenium Driver. You can connect to your own browser with Selenium, use the Docker Helper to connect to to Firefox and Google Chrome or just use any other service that provide the Selenium Driver Interface like Browserstack.

The tests done with this tools can be integrated into your rspec tests.

You can see a integration example with a simple Sinatra app in next url:

github.com/ant30/mixpanel-integration-testing/tree/master/example/site

Setup

Gem Instalation

Just add the mixpaneltesting gem to your Gemfile or install it.

gem install mixpaneltesting

We haven’t published the gem jet, so you need to point to github in your gemfile:

gem 'mixpaneltesting', git: "https://github.com/ant30/mixpanel-integration-testing.git"

Preparing Docker integration

The docker integration is based on DOCKER_HOST environment variable. So you can use boot2docker, docker-machine or just docker if are using a Linux box.

If your are using boot2docker mixed with docker-machine you could have some SSL problems. If you can’t fix it, you can ignore the SSL verification. Do this only if the docker is in your system. This can be very dangerous!!.

DOCKER_SSL_VERIFY=false

You should pull with your docker interface the Selenium official docker images:

hub.docker.com/u/selenium/

P.E. if you want a Firefox selenium system with debug (vnc enabled) you should execute:

$ docker pull selenium/standalone-firefox-debug:2.46.0

Preparing Selenium service integration

The selenium connector is prepared to connect to BrowserStack by fill the correct section in the yaml settings file. The setup require selenium_uri and capabilities with browser description and identification if you use BrowserStackLocal

Using the app runner

If you want to do the tests with the site running in your system, you can provide a starter script in the yaml settings. With this, the suite can boot up and shutdown the app.

Settings file

The settings file is a yaml file. It is in your RSpec suite if you are using it. The yaml is going to be preprocessed by ERB, so you can use variables like ENV if you need it

The config file looks like:

mixpanel:
  api_key: your_project_mixpanel_api_key
  api_secret: your_project_mixpanel_api_secret

app:
  url: the url accesible by selenium

  # If you want to use the app runner use something like this
  # run_local: PORT=3000 ./start
  # In other case, run_local default behaviour is to no act (false)
  run_local: false

# execute_mode is where selenium lives (docker or browserstack service)
execute_mode: docker

# A generic timeout settings to connect to selenium, docker boot up,
# local app boot up
generic_timeout: 20

# This section is required is execute_mode is docker
docker:
  # browser can be firefox and chrome. Firefox is more stable.
  browser: firefox

  # The selenium version in container. The firefox version can vary, but each
  # selenium version is recommended to a specific Firefox version.
  selenium_version: 2.46.0

  # With debug true a open vnc:// is going to be launch, so if your system
  # can handle it, you will see a VNC window to watch the browser process
  debug: true

# This section is required is execute_mode is browserstack
browserstack:
  selenium_uri: http://user@password@hub.browserstack.com/wd/hub
  # Take capabilities from https://www.browserstack.com/automate/ruby
  capabilities:
    os: Windows
    os_version: 7
    browser: Firefox
    browser_version: 39.0
    resolution: 1280x800
    # Optional settings:
    browserstack_local: true
    browserstack_localIdentifier: asdfasdf
    build: 0020
    project: example-project

RSpec integration

Enable mixpanel testing session in your mixpanel tracking code

Your mixpanel code should be able to capture the parameter in the url *mp_session_start=identifier*. Thanks to this parameter, each test example is independent from others navigation examples. There are a mp_session_end too, but we don’t use that by now. The mp_session_start record Mixpanel superproperties called mp_session_id and mp_sesion_timestamp

There are an code example of this integration in the example site:

Just do the tests

You need to require the mixpaneltesting in your rspec file and include the mixpaneltesting context in your describe sections, before the it part. To include the context you need to use include_context

Remember to add the configuration file before to include the context

You can find an example here

require_relative "spec_helper"
require "mixpaneltesting"

describe "Mixpaneltesting environment" do
  RSpec.configuration.mixpanelfilesettings = "./config/mixpaneltesting.yml"
  include_context "mixpaneltesting"

  describe "Simple session with one interation (click)" do

    it "Expected 2 page views/1 click" do
      @selenium.waitfor_object_displayed(:tag_name, 'title')
      @selenium.click(:link_text => 'Page 1')

      expect(@selenium.get_page_source).to match(/<title>Page 1<\/title>/)

      expect(@mixpanel.validate_events({
        'PageView' => 2,
        'ClickInteraction' => 1
      })).to be true

      expect(@mixpanel.validate_complex_query(
        'PageView',
        'string(properties["$os"]) == "Linux"',
        2)
      ).to be true
    end
  end
  ...
end

After include the mixpaneltesting context, you can and want to access to @selenium and @mixpanel objects. The @selenium object is a wrapper to do page interaction and navigation through selenium. The @mixpanel object allow to do queries to mixpanel segmentation api. @mixpanel provides two methods to verify tracking by counting events in the mixpanel response.

The data in mixpanel is not stored in real time, but it is close to that by some seconds. This can cause problems in *@mixpanel.validate_…* results, so we repeat the query some times until we got the correct result from mixpanel. The process is repeated 10 times with 2 seconds delay.

License

Mixpanel integration testing tool is available under an MIT-style license.

:include: LICENSE