get elements from DOM not page source
Hi
I am able to get hold of a JSON object in a script on my page source, this is some datalayer information created at page load.
I do this with a script routine:
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 JourneyData(){
var digitalData = GetDigitalData()
//do something
}
We then update some of these values using javascript but we do not update the page source at that point.
For eg,
when I read my data from the page source script below, some values are null as expected "is_mobile" in this example as it is set to null at load then updated later:
<script type="text/javascript" class="dataMapper">
var digitalData = {JourneyData:{is_mobile:null}}; (I've removed some data here)
</script>
then some javascript runs setting these values to true/false, in memory almost.
I can see that the values have been updated in the console:
I really need some help in getting hold of these updated values via javascript script routine or the find object function in the keyword tests.
Smartbear support simply advised to use Page.contentDocument but this just gets the page source.
Any help greatly appreciated!
With support from our dev team, we have the following solution, we need to look in the correct window (is also browser specific):
function GetDocWindow()
{
var document, window;
document = Sys.Browser().Page("*").contentDocument
// Check if the document.parentWindow property is available
if (aqObject.IsSupported(document, "parentWindow")){
window = document.parentWindow
Log.Message("Document type in use is 'parentWindow'")
}// Check if the document.defaultView property is available
else if (aqObject.IsSupported(document, "defaultView")){
window = document.defaultView
Log.Message("Document type in use is 'defaultView'")
}
else
Log.Error("Unknown browser that doesn't support document.parentWindow or document.defaultView.");
return window
}
function JourneyData(){
var window = GetDocWindow() //gets the return window object from the above function
//items to compare eg
var currency_code = "GBP"
//execute checks eg
aqObject.CompareProperty(window.digitalData.journey_data.currency_code, cmpEqual, currency_code)