Forum Discussion

maxthomassin's avatar
maxthomassin
Occasional Contributor
12 years ago

Get the curret focused object

Hello everyone,



in certain case, im moving on my page using [Tab] and i need to retrieve the object in a variable because I need to use his properties after.



Any suggestion on how I could do that?



Thank you !



  • Hi Maxime,



    I think you can parse the _NewEnum property of your Window (and recursively the subWindows) in order to find the Object for which the "Focused" property is true.



    Regards,

    Christophe
  • maxthomassin's avatar
    maxthomassin
    Occasional Contributor
    Thank you for the answer Christophe.



    Im testing on website (on IE). Im using TC 7 and we are going to switch to the lastest version soon.



    I dont see the property focused in the property list.
  • Try to use document.activeElement property. It will return the native web object, so it will not have TestComplete specific properties and methods.
  • maxthomassin's avatar
    maxthomassin
    Occasional Contributor
    Is there a way to tranform it into the TC Object? Cause i need to insert text or do a click on the item. I tried to get the name of the item, but its not always working.



    Does it only work on item that have the property onFocus != null  ?
  • Try to find some property of focused object that you can use to find corresponding TestComplete's object using Page.NativeWebObject.Find or FindChild method.

    I believe that activeElement is not related to onFocus.
  • maxthomassin's avatar
    maxthomassin
    Occasional Contributor
    Well, i tried to retrieve the id of the NativeWebObject and it's not woking. I added a picture to show what im trying to do and the result.



  • It seems that actually some other page element has focus, maybe it is the parent of the element with id proprieteVide288. Or in the worst case this property is not what you are looking for.

    You can set breakpoint at the line Log.Message(item.id) and inspect the item variable. Take a look at its nodeName, outerHTML properties and I think you will get the key to this issue.
  • Hi Maxime,

    Sorry for my late come back, I didn't pay attention that Object on a Page doesn't have the OnFocus property (I work on an application).

    It's quite difficult to find some properties with specific values when the object is focused, because, when you open the Object Spy, the Object of interest lost the focus...

    So you need in your script to capture the value of the different properties.



    A workaround is possible if you have at the beginning of the test, a list of the different elements you will meet. You may previously add a method to the onfocus event of each element.



    var theFocusedElement;



    MyElementA["onFocus"] =  function() {

    Log["Message"]("Element A receives focus.");

    theFocusedElement = MyElementA;

    }

    MyElementB["onFocus"] =  function() {

    Log["Message"]("Element B receives focus.");

    theFocusedElement = MyElementB;

    }

    etc...



    The "problem" is that you modify your element under test...



    regards,

    Christophe