Ask a Question

Extract datalayer JSON object from html page source testcomplete


Extract datalayer JSON object from html page source testcomplete


Long time user of readyAPi etc but looking to help out with some automation of the datalayer for my employer. The system testers use testcomplete and I'm just wondering what would be the best way to extract the JSON object in the datalayer held on the html page source, should we wish to piggyback on their already written journey based tests.


This link  seems to be getting me on my way but I am unsure how or where to use it.


Should it be using code snippets, logging etc?


Thanks, Dan


What I have currently managed to achieve is create a script in the project:

function Test()
  var url = "*";
  var browser = Sys.Browser("*");
  var body = browser.Page("*").contentDocument.body;    
  Log.Message(aqObject.GetPropertyValue(body, "innerHTML")); 

and is run using the script routines.

This gets hold of the innerHTML or whatever I want.

I think what I will try next is to get hold of the specific JSON object and compare with what I expect that string/object to be.

Further to the solution, I have implemented:

function Test()
  var browser = Sys.Browser("*")
  var page = browser.Page("*")
  var body = browser.Page("*").contentDocument.body
  var dataLayer = body.querySelector('script[class="dataMapper"]')
  // FOR REFERENCE <script type="text/javascript" class="dataMapper">

  var currency_code = 'currency_code:"GBP"'
  aqObject.CheckProperty(dataLayer, "innerText", cmpContains, currency_code, false)

This pulls out the specific innerText of the script class dataMapper which holds the JSON object.

Then use the aqObject.CheckProperty and cmpContains to assert a match.


Now doing this should someone come across and need a solution:


function GetDigitalData(){
var page = Sys.Browser().Page("*");
var digitalData = page.contentDocument.querySelector('script[class="dataMapper"]').innerHTML;
return x = digitalData.substring(23).slice(0, -2);

function PageData(){
var digitalData = GetDigitalData()
//do something
Showing results for 
Search instead for 
Did you mean: