Snowplow Automated Testing with Selenium

I’m working on setting up automated testing with Snowplow Micro and Selenium on my website, and I am having trouble getting events to send consistently. My current testing program loads the website, resets Snowplow Micro with /micro/reset, and then performs an action (e.g. typing in a textbox). The results on \micro\all are then recorded. The program loops by reloading the site, resetting Micro, doing the action, and rerecording.

My problem is that the results of \micro\all can vary from iteration to iteration, which is odd since it is running the same command each time. For example, one iteration might send the events change_form and focus_form, while the next might only send focus_form. Is there any reason that the events sent would be changing across each test? Happy to provide additional information if needed, and thanks so much in advance!

Hi @eileen_dover, the trackers as well as Micro contain a lot of asynchronous processes that might need various amounts of time to process. Do your tests wait (sleep) for a certain amount of time before checking the results in Micro? One mechanism that we recommend is to wait in a loop until the expected events are present in Micro, see the suggestion here.

1 Like

Hi @matus ! Thanks for your response–we do use a wait command after each test before checking (10 seconds), and while results are more consistent, they are still not constant. Is there anything else that might be the issue?

1 Like

If possible I’d call flush() on the tracker and wait 100ms or so before continuing on to the next action. If you are seeing events appear unexpectedly in calls (after resetting Micro) it’s possible that the events might not be getting sent immediately, queued in local storage and then sending on the next page load.

1 Like

@mike Thanks for the suggestion! For clarification, how would flush() be implemented in my code? For reference, I currently have this in the html file of my web application:

<!-- Snowplow starts plowing -->
  <script type="text/javascript">
  ;(function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[];
  p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments)
  };p[i].q=p[i].q||[];n=l.createElement(o);g=l.getElementsByTagName(o)[0];n.async=1;
  n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","http://localhost:8000/sp.js","snowplow"));

  snowplow('newTracker', 'cf', 'http://localhost:9090', { // Initialise a tracker
    appId: 'sample-html-tracker-test-3',
    cookieDomain: '*'
  });

  snowplow('enableActivityTracking', { 
    minimumVisitLength: 30, 
    heartbeatDelay: 60 
  });
  snowplow('trackPageView');
  snowplow('enableLinkClickTracking');
  snowplow('enableFormTracking');`
</script> 
<!-- Snowplow stops plowing -->

And this is what I have implemented for Selenium in Python:

f = open('./output.txt', "w")
iter = 0
while (iter < 50):
    reload() # reloads application page
    reset() # calls /micro/reset
    inv_user() # sends invalid user event
    time.sleep(10) # wait for events to send
    sp_micro.get("http://localhost:9090/micro/all") # get results from /micro/all
    event = sp_micro.find_element(By.TAG_NAME, 'body')
    f.write("\n\n\n")
    f.flush()
    f.write(event.text)
    f.flush()
    reset()
    iter += 1
f.close()

Thanks so much in advance!

1 Like

I believe it’s flushBuffer - snowplow-javascript-tracker/browser-tracker.flushbuffer.md at master · snowplow/snowplow-javascript-tracker · GitHub