cancel
Showing results for 
Search instead for 
Did you mean: 

get elements from DOM not page source

SOLVED
geneticmaterial
Contributor

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:

digitalData console.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
geneticmaterial
Contributor

Re: get elements from DOM not page source

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)

View solution in original post

5 REPLIES 5
A_Roskoshnyi
Contributor

Re: get elements from DOM not page source

I think you're making the task harder than it really is.
First: you read the data from page source and make dynamically change, why? If you know your data structure of JSON, you can create one template and save it in a text file.


Second: to change data in json does not need JavaScript. Just create the required number of files with the data you want, for example "chandet_JourneyData_1.txt" -  your TestCase 1...


Finally, to apply this data, use aqHttp Object.

 

//Take JSON from local storage, chandet_JourneyData_1.txt

var body = ....;

 

//POST - Method to send your JSON Data

var aqHttpRequest = aqHttp["CreatePostRequest"]("your_url");
aqHttpRequest["SetHeader"]("Content-Type", "application/json");
aqHttpRequest["SetHeader"]("Authorization","your_auth");
var aqHttpResponse = aqHttpRequest["Send"](body);

//Check status

if(aqHttpResponse["StatusCode"] == 200)
{

     //...
    //Test - Ok.
}

geneticmaterial
Contributor

Re: get elements from DOM not page source

Hi. Thanks for the response.
I may have neglected to state that I am trying to implement an automated suite of tests.
I do not have control of the html source or JSON object.
I will check the data at page load and then through interactions on the website, which is where I need the 'in memory' changes to the data.
Hope that helps. Ta
sonya_m
Community Manager

Re: get elements from DOM not page source

Hi @geneticmaterial ! Please share your progress with the Community, did you manage to try this approach?


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
geneticmaterial
Contributor

Re: get elements from DOM not page source

No, I was hoping for a reply from my update

geneticmaterial
Contributor

Re: get elements from DOM not page source

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)

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors