Skip to content
Get Free Trial

Write Keywords in Sahi Script

Write and refactor keyword code in the Test Scenario Editor's script view: the toolbar, the Context panel, Create Function, Find and Replace, Find Usage and Create AR.

Most keywords are built without code in the No-Code Test Editor. Some need logic that no-code can’t express, such as a loop, a calculation or a call to Java. Behind every keyword is a Sahi Script function in a keyword file (.sah), and you can write that code yourself in the Test Scenario Editor’s script view.

To open the script view, click a keyword file (.sah) in the Navigator, or Ctrl-click (⌘-click on Mac) a keyword in a scenario file.

The visual_books.sah keyword file in the script view, with numbered badges on the toolbar buttons Auto Format, Create Function, Show Context, Find and Replace, Rest Client and Create AR
  1. Auto Format: fixes the indentation of the whole file. See Auto Format.
  2. Create Function: turns selected lines into a function. See Create a Function.
  3. Show Context: opens the Context panel.
  4. Find and Replace: finds text in the file. See Find and Replace.
  5. Rest Client: opens the Rest API Editor.
  6. Create AR: creates an Accessor Repository from the file. See Create an Accessor Repository.

A call to a keyword or an accessor kept in an Accessor Repository doesn’t show where it’s defined. The Context panel does. Click Show Context, or Ctrl-click (⌘-click on Mac) the call.

  • Function: the file that defines the function, its parameters and their values. Open opens that file, and Include adds it to the current file if it isn’t included yet. Insert adds the call, and Update changes the values in the line.
  • Accessor: the AR key and value of the element in the line. You can change the value, or the key in every file or only this one.
The Context panel with its Function and Accessor tabs, Function File Name with Open and Include, Function Name, a Parameter and Value table, and Insert, Insert and Go to Next Line and Update

Ctrl-click (⌘-click on Mac) also moves between files: on a keyword in a scenario file, on a function or AR key, or on a file path in an _include or loadSahi line. Previous Location and Next Location, the arrows at the right of the tabs, take you back.

Recorded steps often repeat with different values. Turn them into one function with parameters:

  1. Select the lines and click Create Function.
  2. Enter a Function Name. You can choose from names listed in userdata/config/function_names.txt.
  3. Check the values to turn into parameters under Function Parameters, and name them. (more) shows more of them.
  4. Choose the options: Remove Duplicates merges parameters with the same value, Add Function Call replaces the lines with a call to the new function, and Create JS Doc adds the documentation comment.
  5. Click Create.
The Create Function dialog with Function Name, the options Remove Duplicates, Add Function Call and Create JS Doc, and three values 2, 3 and 1 as parameters $Q, $Q1 and $Q2, with Cancel and Create

Find and Replace searches the open file, with Exact match and Match Case. Enter text in Replace with and click Replace or ReplaceAll. To search only, press Ctrl + F (⌘ + F on Mac).

The Find and Replace panel with Find, Exact match, Match Case, Replace with, and the Replace and ReplaceAll buttons

When you rename a keyword or a variable, every file that uses it has to change too. Tools > Find Usage searches all the files in the project. Filter the results by Exact match, Keyword, Variable or Pattern match (for example *.sah to search only keyword files). To replace, enter the new name in Replace With, select the files and click Replace.

The Find Usage - Keywords and Variables panel at the bottom of the Editor, with Find, Replace With, and the filters Exact match, Keyword, Variable and Pattern match

Auto Format fixes the indentation of the whole file and makes it easier to read. It treats a semicolon as the end of a line.

Create AR moves the accessors of the selected files into an Accessor Repository. With no file selected, it works on the open file. To undo it, copy the files from userdata/scripts_backup back into the project.

Shortcut Explanation
Ctrl + S (⌘ + S on Mac) Save the current file
Alt + Up Arrow Move current line up
Alt + Down Arrow Move current line down
Ctrl + D (⌘ + D on Mac) Delete the current line
Ctrl + F (⌘ + F on Mac) Search in the file
Ctrl + / (⌘ + / on Mac) Comment selected lines
Ctrl + Alt + Up Arrow (⌘ + Alt + Up Arrow on Mac) Copy current line above
Ctrl + Alt + Down Arrow (⌘ + Alt + Down Arrow on Mac) Copy current line below
Ctrl + L (⌘ + L on Mac) Go to a line number

To run or debug the keywords you write, see Run Tests and Debug a Test.