2

So similar to ALt-Shift-F in Netbeans, is there a to do this right in the ide in TestComplete? Not sure if this is possible or if anyone can think of a workaround to autoFormat without leaving the TestComplete window.

I'm trying to get the below solution to work with http://jsbeautifier.org/ for javascript / Jscript code in TestComplete.

Thanks

Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71

2 Answers2

1

Great question!

There is no built-in function for that. So, we should not expect any solution to be 100% convenient - it is just not a simple task to modify the current script editor contents (if at all possible). So, whatever you do, it will still be some kind of compromise.

In general, the task is three-fold:

  1. Get the current unit code.

  2. Format the code.

  3. Put the code back to the unit.

According to my understanding, items 1 and 3 can be accomplished only by creating a TestComplete plug-in - accessing editors for project nodes is not an easy thing.

UPDATE: silly me! There is a way to access the script editor code - I've updated the below part.

What will help us avoid switching to a different app, are the Script Extensions:

  • We create a custom Checkpoint in the form of a Script Extension, and install it to TestComplete. As a result, we get a button on the toolbar that we can click to invoke our code.

  • In the design time action, we call some code that reads the editor contents, then uses external code formatting functionality, and replaces the editor contents with the formatted code.

It would extremely interesting to see the implementations other TestComplete users can suggest! As a start, I am posting a solution that includes using an external web site to format VBScript code (http://www.vbindent.com/). I know that the starter of the post is probably using JScript, but I have not found a JScript formatter yet.

My solution is a simple Script Extension. I can't post a file here, so I will post the code of the two Script Extension files:

Description file:

<!-- Description.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<ScriptExtensionGroup>
  <Category Name="Checkpoints">
    <ScriptExtension Name="VBScript Code Indent" Author="SmartBear Software" Version="0.1" HomePage="smartbear.com">
      <Script Name="VBIndent.js">
        <DesignTimeAction Name="Indent Current VBScript Unit" Routine="DesignTimeExecute"/>
      </Script>
      <Description>
        Indents VBScript code in the currently active unit.
      </Description>
    </ScriptExtension>
  </Category>
</ScriptExtensionGroup>

Code file:

// VBIndent.js
function DesignTimeExecute()
{
  if (CodeEditor.IsEditorActive)
  {
    var newCode = IndentVBSCode_Through_VBIndent(CodeEditor.Text);
    if (null == newCode)
      return;
    CodeEditor.Text = newCode;
  }
}

function IndentVBSCode_Through_VBIndent(codeToIndent)
{
  var URL_VBIndent = "http://www.vbindent.com/?indent";

  var httpObj = Sys.OleObject("MSXML2.XMLHTTP");

  httpObj.open("POST", URL_VBIndent, false);
  httpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  httpObj.send("thecode=" + escape(codeToIndent));

  var responseText = httpObj.responseText;

  // Extract the indented code from the response
  var rx = /<textarea name=\"thecode\".*?>((.*\n)*?)<\/textarea>/;
  matches = rx.exec(responseText);
  if (null == matches)
  {
    return null;
  }
  codeIndented = matches[1];
  return codeIndented;
}

After you create these files, and put them to something like "\Bin\Extensions\ScriptExtensions\VBIndent", and click "File | Install Script Extensions | Reload", you will see a new "Indent Current VBScript Unit" item in the custom checkpoints drop-down button on the Tools toolbar. Clicking the element will format the VBScript code in the currently active editor.

So, this is to give a clear idea of what a solution can look like. Better suggestions are welcome! Share your thoughts!

Alex
  • 587
  • 3
  • 9
  • Come on, people - take the dare! Suggest your solution! :) The complex part is already sorted out - you just need to change the DesignTimeExecute routine implementation with your code. I would even start a bounty here, but looks like I can't do this yet... – Alex Jul 20 '12 at 08:56
  • My thoughts are maybe people just dont use the testComplete ide? I have been throwing my code in netbeans every so often and back in testComplete to format it. – Frank Visaggio Jul 23 '12 at 00:38
  • http://jsbeautifier.org/ Im trying to incorporate this in order to make your solution work for Javascript – Frank Visaggio Jul 23 '12 at 16:19
  • Well... while this may be the case, I don't think that many people do this - the approahc is just not easy for the daily work. At that, while TC's script editor does not have this ability, it is just fine for many other usage scenarios. So, maybe the task itself is not that popular? What are the use cases when you need to re-format the code? – Alex Jul 23 '12 at 17:38
  • Oh, I did not find the jsbeautifier.org originally. Would be cool to see your solution here! Let me know if you need a piece of advice regarding the implementation. I have also thought about using a local command-line utility to avoid using the Internet, but did not have a suitable tool at hand. – Alex Jul 23 '12 at 17:41
-2

FYI

I've done. Based on your posts.

JSFormat.tcx

https://drive.google.com/uc?export=download&id=0B1x_73bHRc2Jcm8wbTJ2dUpZQTQ

To install the extension copy attached file JSFormat.tcx to C:\Program Files (x86)\SmartBear\TestComplete 10\Bin\Extensions\ScriptExtensions

To use view next image:

https://drive.google.com/uc?export=download&id=0B1x_73bHRc2Jc3RuLXFpTnlCSnc

Regards

TioJack
  • 1
  • 1