Eclipse configuration for Syntax Highlighting, Template proposals and Refactoring in Sahi scripts
You can use any text editor to write your sahi scripts. But Eclipse as an editor provides advantages like Syntax highlighting, Template proposals and Refactoring. This article explains how to use Eclipse as editor for Sahi scripts.
Sahi scripts - .sah Vs .js
Section titled “Sahi scripts - .sah Vs .js”Sahi scripts carry a .sah extension. But a .js extension can be used in place of .sah. Sahi treats a .js file the same as a .sah file.
To write scripts in Eclipse, one needs to have the JSDT Eclipse plug-in installed. The JSDT Eclipse plug-in provides native support for JavaScript (.js) files. One can add support for .sah files as well. But the JSDT plugin provides some additional features for .js files as compared to .sah files. Hence it is possible to rename .sah files as .js files and get better Eclipse support without losing any Sahi functionality.
Eclipse configuration
Section titled “Eclipse configuration”-
Open Eclipse. It will ask for the workspace path. We will assume that the workspace is (to be) created at the following location:
C:\Sahi\SampleWorkspace. Specify the workspace path as indicated below, and clickOK.
-
Ensure that the
JSDTEclipse plug-in has been installed. Click onHelp | About Eclipse SDK | Installation Details.
-
If not installed, click on
Help | Install New Software. Use the settings displayed in the image below. If you are using an older version of Eclipse, you should see a corresponding update site in theWork withdropdown box.
Proceed with the installation.
Restart Eclipse after installation. Give the same workspace path as before -
C:\Sahi\SampleWorkspace.
-
Adding support for
.sahfiles-
Click on Windows | Preferences | General | Content Types
-
In
Content types:section, selectText | JavaScript Source Fileand then inFile associations:section, click onAdd. -
Make association for
*.sahand clickOK.
-
-
Let us now create a new JavaScript Project.
Click on
File | New | Project...
Search for JavaScript Project in Wizards and click on
Next
Give a Project name of your choice. We have given it the name:
SampleProject. ClickNext
You should see the following two JavaScript Libraries in the Libraries tab
- ECMAScript Built-in Library
- ECMA 3 Browser Support Library
Click on
Finish
If this is the first JavaScript project you created, an
Open Associated Perspectivedialog box will be opened, prompting you to use the JavaScript perspective.Click
Yes.
-
You can create new files and folders under the project created. We will place all Sahi scripts inside a
scriptsfolder. To create a new folder, right click on the project, and then click onNew | Folder. Give the folder name asscriptsand click onFinish.
To create a script file inside the
scriptsfolder, right click on the folder, and then click onNew | File. Give the new file its appropriate name and click onFinish. Let us name the file assampleFile.js. If you intend to use.sahextension, name it assampleFile.sah
You can copy your existing scripts into the scripts folder of the current project by pasting them at the appropriate location -
C:\Sahi\SampleWorkspace\SampleProject\scripts. The screenshot shows scripts as.jsfiles. If you intend to use.sahextension, the scripts will be.sahfiles.
Refresh the project to see the copied files in your Eclipse IDE.

-
The
JSDTplugin provides various templates for auto completion. Example:for,ifetc.Sahi ships with a readymade template file, SahiEclipseEditorTemplates.xml that customizes some of these templates for better use in Sahi scripts. Download SahiEclipseEditorTemplates.xml to a folder location, say
C:\Sahi\SampleWorkspace\EditorTemplatesTo install this templates file, click onWindow | Preferences.
Now go to
Javascript | Editor | Templatesas shown in the figure and click onImporton the right.
Select the templates file
SahiEclipseEditorTemplates.xmlthat we downloaded above, and pressOpen.
Press
OKon theTemplateswindow.SahiEclipseEditorTemplates.xmlhas now been successfully imported.To make use of any template, type the first few letters of the template and press
Ctrl + Spaceto bring up the template proposal.Here are a few examples.
Example: Type the first few letters of the template
testcaseand pressCtrl + Spaceto bring up the template proposal for_testcase.
Example: Type the first few letters of the
forloop command and pressCtrl + Spaceto bring up template proposals related tofor.
Likewise you can expand other templates listed at the location below.

-
Template functionality can also be added for
Sahi functions.- If you are using
.sahextension, copyapis.sahfrom<SAHI_INSTALL_FOLDER>/userdata/scriptsinto thescriptsfolder in the project. - If you are using
.jsextension, copyapis.sahfrom<SAHI_INSTALL_FOLDER>/userdata/scriptsinto thescriptsfolder in the project and rename it asapis.js.
Open any
.js(or.sah) file from the JavaScript project, and you should be able to seeSyntax highlighting.Type the first few letters of a Sahi function and press
Ctrl + Spaceto bring up template proposals for Sahi functions.
- If you are using
Refactoring a script into functions
Section titled “Refactoring a script into functions”Once you have a recorded script, Eclipse can help in refactoring the linear script into functions.
We have taken the following script to demonstrate refactoring using Eclipse

Refactoring in Eclipse requires the code to be part of a function. Since the recorded script is not part of any function, we will first create a function surrounding our original code.
Select all the lines of code, Right Click | Surround With | function (function) as shown in the figure. Give the function an appropriate name (we have named it sampleApp). We will remove this function at the end of the Refactoring.

Now, to extract a function out of selected lines of code, do the following: Select the lines of code | Right Click | Refactor | Extract Function.

A dialogue box would show up, write the function name, and press OK, as shown in the figure.

Repeat the same steps for all the lines of code you want to extract out as functions. At the end of this exercise, our script now looks like this:

-
You can see the following changes in the script:
-
The selected lines of code have been replaced by functions and calls to the respective functions.
-
The refactored function code may not be formatted correctly. If so, you will have to format it manually.
-
The created functions do not have any parameters. We will fix this in the next step.
To convert a value inside the function into a function parameter, do the following. Select the value | Right Click | Refactor | Introduce Parameter.

Give a name to the parameter, beginning with $ and press OK. Here we have named the parameter as $java

You can see that the value has been extracted out as a parameter and function calls have been modified to pass a value for the parameter.

We continue doing this for all the variables in our script. At this point, the script looks like this:

We still have the temporary function we had made at the starting of Refactoring code, sampleApp. The script now looks like this:

Let us now move the functions login, addAndAssertTotalCost and logout to a different file so that they can be included from multiple scripts.
Add a new file called sampleLib.js (or sampleLib.sah if you are using .sah extension). Copy the functions to this file. This will have to be done manually.

Remove the functions from sampleFile.js (or sampleFile.sah if you are using .sah extension). Include sampleLib.js (or sampleLib.sah) using _include manually. sampleFile.js now looks like this

Renaming a function or variable name
Section titled “Renaming a function or variable name”There are times when one needs to rename a variable or function name. On doing so, the name should get changed at all places of usage, in all scripts.
Eclipse allows for such refactoring. Let us see how to rename a function name. We will rename the function addAndAssertTotalCost to addBooksAndAssertTotalCost.
To do this, open sampleLib.js, select addAndAssertTotalCost, Right Click | Refactor | Rename (or select addAndAssertTotalCost and press Alt + Shift + R).

You will be asked to enter a new name. Enter the new name addBooksAndAssertTotalCost and press Enter.

The function name gets changed to addBooksAndAssertTotalCost.

Open sampleFile.js. You will notice that the function name has been changed automatically in this file as well.

If this function were to be included in other files, it would get modified at all these locations.
Navigate to functions
Section titled “Navigate to functions”One often needs to open a function declaration to view its contents. The function declaration could be present inside another script altogether. For example, let us try and look at the function declaration of addBooksAndAssertTotalCost from inside sampleFile.js.
One can open a function declaration in the following ways.
-
Press the
Ctrlkey andhover the mouse over the functionthat you want to open. The function name will appear as alink.
Click on the
linkwhile keeping theCtrlkey pressed. -
Or select the function,
Right Click | Open Declaration.
-
Or select the function, and press
F3.
You will be navigated to the function declaration in the appropriate script file. In this case, you will be navigated to addBooksAndAssertTotalCost in sampleLib.js.


