Forum Discussion

blatec's avatar
blatec
Occasional Contributor
8 years ago
Solved

Array Type mismatch in javascript

Hi,

 

if i call this function csSetControlValue("ctrlInterestRateTypeSleList",['003','004']) in the chrome console (F12) it work correct.

 

But if i try to call this -> Aliases.browser.MainPage.contentDocument.script.csSetControlValue("ctrlInterestRateTypeSleList",['003','004']) from Testcomplete Javascript, i get an type mismatch error.

 

If i provide a single string instead of an array it works. -> Aliases.browser.MainPage.contentDocument.script.csSetControlValue("ctrlInterestRateTypeSleList","003")

 

Maybe someone can help me.

 

best regards

 

Horst

  • I think, but I'm not sure, that this is because you are passing a native JavaScript array through to a function that comes from an object in the AUT.  While it works just fine through the chrome console (because you are accessing the JavaScript process directly), going through the TestComplete code, I think, requires that array to be passed, not as a native JavaScript array but as a variant array.

    Try converting your array first using something like:

     

    function ConvertJScriptArray(JScriptArray)
    {
      // Uses the Dictionary object to convert a JScript array
      var objDict = Sys.OleObject("Scripting.Dictionary");
      objDict.RemoveAll();
      for (var i in JScriptArray)
        objDict.Add(i, JScriptArray[i]);
      return objDict.Items();
    }
    

    Again, I could be wrong, but that seems to be the best bet.

  • Just a bit of investigation... drop a break point on the var test1 = line and then bring up the evaluator for your test variable.  If you can screenshot that, that would be great.  I want to see how TestComplete sees the item.

    The other thing to try is that, if you're using JavaScript, getting values of indexed items that are not native JavaScript objects requires the use of the $get method.

    So... something else to try is to do the following:

    function test()
    {
    var test = Aliases.browser.MainPage.contentDocument.script.csGetControlValue("ctrlInterestRateTypeSleList");
    Log.Message("Value of item 0 is : " + test.$get('0'));
    }

    Even though the code you are calling is a JavaScript method on the page itself, from the previous problem, it's obvious that TestComplete is putting it through some sort of process that masks it as a JavaScript array.  I've had this problem myself in a slightly different situation so I think the $get method should work.

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I think, but I'm not sure, that this is because you are passing a native JavaScript array through to a function that comes from an object in the AUT.  While it works just fine through the chrome console (because you are accessing the JavaScript process directly), going through the TestComplete code, I think, requires that array to be passed, not as a native JavaScript array but as a variant array.

    Try converting your array first using something like:

     

    function ConvertJScriptArray(JScriptArray)
    {
      // Uses the Dictionary object to convert a JScript array
      var objDict = Sys.OleObject("Scripting.Dictionary");
      objDict.RemoveAll();
      for (var i in JScriptArray)
        objDict.Add(i, JScriptArray[i]);
      return objDict.Items();
    }
    

    Again, I could be wrong, but that seems to be the best bet.

    • blatec's avatar
      blatec
      Occasional Contributor

      Hi,

       

      thank you!! works perfect!!

       

      best regards

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Awesome!  Glad to hear it.

        The reason why I wasn't quite sure was because that bit of code comes from a help article talking about JScript and native arrays rather than JavaScript.  Since, however, they are, essentially, the same (although JavaScript uses a newer engine), I guessed that they would have a similar limitation.

        TanyaYatskovska and HKosova, this might be a documentation item that needs updated.