Ask a Question

Check if a property exist for an object

lodecesa
Contributor

Check if a property exist for an object

Hello everyone,

I am looking for a way to find out in a script if an HTML object has a specific property.

 

Thanks

7 REPLIES 7
rraghvani
Trusted Contributor

If you use the Object Spy tool, it will list all the exposed properties of the UI Object.

 

Or do you mean verifying a property, if so, see aqObject.CompareProperty Method


@rraghvani wrote:

If you use the Object Spy tool, it will list all the exposed properties of the UI Object.

 

Or do you mean verifying a property, if so, see aqObject.CompareProperty Method


Sorry, I have corrected my original post. I meant in a script, so via javascript code.

rraghvani
Trusted Contributor


@rraghvani wrote:

See aqObject.CheckProperty Method 


Thanks but this method evaluate the Value of a Property of an Element and not if the Property exist for the Element

rraghvani
Trusted Contributor

Hopefully, I understand what you mean now. Try this,

function CheckProperty()
 {
     const Name = {
         Firstname: "SmartBear"
     }
     
     Log.Message(Name.Firstname);
     Log.Message(Name.hasOwnProperty("Firstname")); // true
     Log.Message(Name.hasOwnProperty("Surname"));   // false
 }

Use "hasOwnProperty()"

AlexKaras
Community Hero

Hi,

 

if an HTML object has a specific property

I believe that you are looking for how to check if HTML object (actually - html markup for the given element) has not a specific property, but some specific attribute, aren't you?

In this case use the .hasProperty() html built-in method accessible from within TestComplete.

https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute

https://www.w3schools.com/jsref/met_element_hasattribute.asp

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
eykxas
Contributor

Use this method : 

//javascript

var hasProperty = aqObject.IsSupported(obj, "property_name");

if "hasProperty" is true, then the property is available on your object and you can interact with. If false, then the property does not exist in your object.

cancel
Showing results for 
Search instead for 
Did you mean: