Forum Discussion

mikakoistinen's avatar
mikakoistinen
Contributor
7 years ago

Script extension method parameter

Hi,

I tried to make my 1st script extension.

function cleanStr(s)
{
  s = aqString.Replace(s, chr(27), '');
  s = aqString.Replace(s,chr(2), '');
  s = aqString.Replace(s,chr(28), '');
  s = aqString.Replace(s,chr(14), '');
  return s;
}

same code works okay if used directly in jscript, but if it is extension I get error Value must be an object (or similar, error is in  native language)

i call it like (skjstrutils is the extension )

 showmessage(skjstrutils.cleanStr('aaaaa'));

 

  • The error message is a bit misleading.  The problem is not with the parameter, it's with your use of the chr method.  That method is a method provided within the TestComplete environment and is not available within script extensions.

     

    To do what you want to do, use the native JScript method String.fromCharCode like so

     

    function cleanStr(s)
    {
      s = aqString.Replace(s, String.fromCharCode(27), '');
      s = aqString.Replace(s,String.fromCharCode(2), '');
      s = aqString.Replace(s,String.fromCharCode(28), '');
      s = aqString.Replace(s,String.fromCharCode(14), '');
      return s;
    }

     

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The error message is a bit misleading.  The problem is not with the parameter, it's with your use of the chr method.  That method is a method provided within the TestComplete environment and is not available within script extensions.

     

    To do what you want to do, use the native JScript method String.fromCharCode like so

     

    function cleanStr(s)
    {
      s = aqString.Replace(s, String.fromCharCode(27), '');
      s = aqString.Replace(s,String.fromCharCode(2), '');
      s = aqString.Replace(s,String.fromCharCode(28), '');
      s = aqString.Replace(s,String.fromCharCode(14), '');
      return s;
    }

     

     

  • mikakoistinen As per my understanding you need to create a run time object  within extension package to access the methods.Create a filename.xml and keep within extentesion folder,refer the below sample code for your reference.Try with this approcah it might be help you.

     

    <?xml version="1.0"?>
    <ScriptExtensionGroup Name="">
    <Category Name="RunTime Objects">
    <Script Name="sciptunitname.js">
    <RuntimeObject Name="skjstrutils">
    <Method Name="cleanStr" Routine="cleanStr">Method Desciption</Method>
    </RuntimeObject>
    </Script>
    </Category>
    </ScriptExtensionGroup>

     

    After create the .xml file,try to access your method like below.

    Runtimeobjectname(skjstrutils).MethodName(cleanStr)

     

    Thanks ,

    Ashok

     

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      AshokKumarSahoo Your usage of the runtime object is incorrect.  Once it's wrapped with the description.xml file (it needs to be named, specifically, as description.xml), you can just simply call it as mika has.