Top Rounds
| We are Hiring! |

Functions ·

Sahi functions are similar to javascript functions in syntax.

eg.

A simple example would be:

_click(_link("Login Page")); _setValue("login", "karthik"); _setValue("password", "secret"); _assertContainsText(document.body, "Welcome Karthik");

We want to move out the login part so that it can be reused. So we extract it into a function.

function login(){ _setValue("login", "karthik"); _setValue("password", "secret"); }

_click(_link("Login Page")); login(); _assertContainsText(document.body, "Welcome Karthik");

Now it makes more sense to pass the username and password into the function than hardcoding it inside the function. So:

function login($username, $password){ _setValue("login", $username); _setValue("password", $password); }

_click(_link("Login Page")); login("karthik", "secret"); _assertContainsText(document.body, "Welcome Karthik");

Note that the variables being used have a $ prefix.

We can further move this login function into another file say loginHelper.sah and include that file into the current script using _include

_include("loginHelper.sah"); _click(_link("Login Page")); login("karthik", "secret"); _assertContainsText(document.body, "Welcome Karthik");


Related topics

Sahi Scripting Basics - Part 1
Sahi Scripting Basics - Part 2
Sahi Scripting - Calling Java
Scripting Changes in Sahi V2
For loops
While loops
If condition
Functions
Exception handling using try-catch
Recovering without try-catch using _setRecovery




---


Top Rounds