Forum Discussion

geneticmaterial's avatar
geneticmaterial
Contributor
3 years ago
Solved

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

  • geneticmaterial's avatar
    geneticmaterial
    3 years ago

    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
    }

3 Replies

  • 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's avatar
      geneticmaterial
      Contributor

      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's avatar
        geneticmaterial
        Contributor

        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
        }