Friday 15 April 2022

How I automated Tableau dashboard testing

Tableau is a traditional BI tool. It is a collection of different views or visualization where each view showcases a different kind of data at the same time. It allows users to get a holistic view of all the data on one screen.

The crux of automating any Tableau dashboard test scenario is to extract data from UI and compare that with the data source (Eg: Database). Extracting data from Tableau dashboards using traditional UI automation tools such as Selenium or Cypress is very difficult because these dashboards are displayed as <canvas> elements in UI. And making them work with complex xpaths is not a feasible solution. On the other hand we have a few paid tools like Kinesis and BI Validator which can serve the purpose.

But I wanted a reliable, light weight and completely free alternative for all the above options. While searching for it, I stumbled upon Tableau’s Rest APIs. In this post we are going to discuss about extracting the data from these dashboards using Tableau’s REST APIs. These APIs give you simple access to the functionality behind Tableau data sources, projects, workbooks, site users, sites, flows,and more. In this approach we would need a little help from our Tableau development team.


Before going into the REST APIs, we should understand the internal architecture of the tableau dashboards. Each Tableau dashboard uses a workbook. Each workbook contains multiple sheets and views. A single dashboard component such as a bar chart/ pie chart is a combination of one or more views. So when you are trying to extract the data from a chart, you have to drill till the views.

Now lets get into the approach. Everything starts with the Sign-In API. Any Tableau Server REST API requires that you send a credentials token with each request. To get a token, you call Sign-In API and pass credentials of a valid user, either a Personal Access Token (PAT) or a username and password, along with the content URL (subpath) of the site you are signing in to. 


Once you have access to Tableau dashboard, you can set up your own PAT token. Check here for more details on this. Note the PAT token and substitute it in the below API.


Request Body:


{
"credentials": {
"personalAccessTokenName": "<MY_PAT_TOKEN_NAME>",
"personalAccessTokenSecret": "<MY_PAT_TOKEN_SECRET>",
"site": {
"contentUrl": "<CONTENT_URL>"
}
}
}

Parameters:

MY_PAT_TOKEN_NAME,MY_PAT_TOKEN_SECRET are the PAT token details that you create in the Tableau dashboard UI. By default CONTENT_URL is empty string ("") unless your Tableau team customizes the URL.

If you execute the API with all the required details, you will receive the response like below:

{
"credentials": {
"site": {
"id": "7668ddfd-sdfds-sdfds87-sdfdsds",
"contentUrl": "https://mydhashboatd.com"
},
"user": {
"id": "6746-sddf78sdfds-sad5763"
},
"token": "hjhjhdDSDghGHGghGH233jHhhjHJKKJ",
"estimatedTimeToExpiration": "234:18:27"
}
}
From the above response, we need site id, token for all the subsequent APIs.

At this point, you will need some information from your Tableau developers such as workbook_id. From the workbook_id, you can get all the views that are available using the below API. Make sure you pass in header “X-Tableau-Auth” with the token that received from above request.






GET:
http://my-server/api/<api-version>/sites/<site-id>/workbooks/<workbook_id>/views

Parameter Values:

api-version The version of the API to use, such as 3.15. 

site-id The ID of the site that we noted above.

Workbook_id The ID that you receive from your development team.


Request Body

None


Once you receive all the views, you need to work with your Tableau development team to identify the relation/mapping between each view and the corresponding dashboard component. For example a single pie chart may have more than one view. 


After identifying the views, you can get the data from each view using the below API. Make sure you pass in header “X-Tableau-Auth” with the token.


GET     http://my-server/api/<api-version>/sites/<site-id>/views/<view-id>/data

Parameter Values

api-version The version of the API to use, such as 3.15. 

site-id The ID of the site that contains the group.

view-id The ID of the view whose details are requested.


Request Body

None


Header
X-Tableau-Auth

This way you can easily retrieve the data from each dashboard view. This is just a single approach. But these REST APIs provide multiple options for us to receive the data otherwise could be very difficult to extract via UI.