Forum Discussion

geneticmaterial's avatar
geneticmaterial
Contributor
4 years ago

Cannot get array details from JSON object [undefined]

There seems to be missing functionality with testComplete when attempting to get array details held within a JSON object.

This is basic javascript and should work, regardless of testComplete.
I am getting hold of a json object from the browser memory (the contentDocument.defaultView for eg),

this json (let's call it digitalData as that's how it is referenced in the browser (or even in the console if you were to search there for it)) does also hold an array of 

objects (in this example, that array is called page_assets[]):
{
"page_data": {
"page_url": "https://ourTestWebsite.co.uk/portal/SignUp",
},
"page_assets": [
{
"asset_id": "97be8bb9-2544-4c23-a407-89d0977d3284",
"asset_category": "",
"asset_sub_category": "",
"asset_name": "Alert Panel",
"asset": "Panel",
"campaign": "",
"has_emphasis": false,
"standard": false,
"type": "Alert",
"variant": "Information",
"title": "To allow us to continually improve, there will be planned system maintenance betwe"
},
{
"asset_id": "1eb085a6-b958-4dca-86bd-bde6e87e7817",
"asset_category": "instance",
"asset_sub_category": "",
"asset_name": "Login Box",
"asset": "Form Panel",
"campaign": "",
"has_emphasis": false,
"standard": true,
"type": "Login",
"variant": "MVC",
"title": "Log in"
}
],
"journey_data": {
"home_page_section": null,
"home_page_section_name": null,
"home_page_total_sections": null,
"internal_campaign": null,
"is_mobile": "true",
"currency_code": "GBP",
"megamenu_clicked": null,
"megamenu_item_campaign": null,
"megamenu_item_location": null,
"megamenu_item_top_selection": null,
"megamenu_item_bottom_selection": null,
"footer_clicked": null,
"footer_item": null,
"campaign_code": null,
"login_success": "false",
"logout_success": null,
"registration_success": null,
"search_result_clicked": null,
"search_result_position": null,
"search_result_title": null,
"branch_search_name": null,
"branch_search_clicked": null,
"branch_search_position": null,
"cookie_policy_header_link_clicked": null,
"portal_nav_clicked": null,
"portal_nav_item": null,
"agm_registration": null,
"agm_email_contact": null,
"contact_form_info": {
"account_number": null,
"preferred_contact_time": null,
"receive_contact": null,
"form_type": null,
"feedback_reason": null
},
"key_element_clicked": null,
"key_element_data": {
"asset_category": null,
"asset_sub_category": null,
}
},
}



Using Sys.Browser("*").Page("*").contentDocument.parentWindow.digitalData.page_data.page_url
I correctly get the value "https://ourTestWebsite.co.uk/portal/SignUp"

However when looking for the array detail;
using Sys.Browser("*").Page("*").contentDocument.parentWindow.digitalData.page_assets[0].asset_id
I get a javascript runtime error "typeError: cannot read property 'asset_id' of undefined"

As said, surely the basic javascript functions should work for this.

You can test this should work by going to https://playcode.io/new/
Enter the above json as 'var = digitalData {insertJSONabove}
print to console digitalData.page_data.page_url which will return 'https://ourTestWebsite.co.uk/portal/SignUp' to prove the non array held details,
or
digitalData.page_assets[0].asset_id to get '97be8bb9-2544-4c23-a407-89d0977d3284' (array details).

 

Any ideas?

  • We got a resolution from SmartBear on this:

    "Unfortunately, TestComplete can't work with arrays like that contentDocument.parentWindow.digitalData.page_assets[0].
    It's possible to access asset_id using eval():
    page.contentDocument.Script.eval("digitalData.page_assets[0].asset_id")"

     

    This works perfectly. Hopefully this helps out others.

     

    Note: to the person who marked this as spam; your action is cowardly without comments, it is neither constructive nor correct and it was overturned. Unlucky.

  • We got a resolution from SmartBear on this:

    "Unfortunately, TestComplete can't work with arrays like that contentDocument.parentWindow.digitalData.page_assets[0].
    It's possible to access asset_id using eval():
    page.contentDocument.Script.eval("digitalData.page_assets[0].asset_id")"

     

    This works perfectly. Hopefully this helps out others.

     

    Note: to the person who marked this as spam; your action is cowardly without comments, it is neither constructive nor correct and it was overturned. Unlucky.