Simulate Click on Google Geo Chart

1 minute read

So I found out it’s not so documented, hopefully google will index this. I have a page on my system that shows a google geo chart (same as this) When clicking on a country on the map lot’s of interesting things happens. And I wanted to implement a ui automation test to cover this use case.

It’s not so easy cause even if you find the right svg path object and try clicking on it you get some interception errors. So I googled a bit without any luck, but the geo chart documents came to the rescue.

The easiest way is to run some javascript:

var yourChart = Chartkick.charts['counutries-map'].chart // Replace with code that gets your chart
google.visualization.events.trigger(yourChart, 'regionClick', { region:'US' });

This code triggers an event on the chart object that the United States region was clicked. super simple and easy I’m using capybara and it looks like this:

    page.evaluate_script(<<~JS)
        function(){
            var yourChart = Chartkick.charts['counutries-map'].chart // Replace with code that gets your chart
            google.visualization.events.trigger(yourChart, 'regionClick', { region:'US' });
        }()
      JS

It’s just injecting a self executing function to the page.

Comments