cancel
Showing results for 
Search instead for 
Did you mean: 

Extract datalayer JSON object from html page source testcomplete

geneticmaterial
Occasional Contributor

Extract datalayer JSON object from html page source testcomplete

Hi

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

3 REPLIES 3
geneticmaterial
Occasional Contributor

Re: Extract datalayer JSON object from html page source testcomplete

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

function Test()
{
  var url = "*";
  Browsers.Item(btIExplorer).Run(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.

geneticmaterial
Occasional Contributor

Re: Extract datalayer JSON object from html page source testcomplete

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.

 

geneticmaterial
Occasional Contributor

Re: Extract datalayer JSON object from html page source testcomplete

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
}
New Here?
Join us and watch the welcome video:
Announcements