Java APIs
Following is the list of Java APIs:
ns_eval_string
Description: This API is used to evaluate the string representation of the NS variable.
Fields: It has following fields:
- Evaluation String: This string contains an NS variable, which is selected from the list of parameters. All occurrences of NS variable are replaced in the input string with NS variable’s current value.
- Return Variable: This is an optional field.
Example
String Id; Id= nsApi.ns_eval_string("{ProductId}"); ProductId is ns_variable. System.out.println("The output will be:" + Id);
ns_save_string
Description: This API is used to save a null-terminated string to an NS variable (or parameter). This is the only way to assign a value to an NS variable (if assignment is allowed for its type).
Fields: It has following fields:
- Variable Value: This value is assigned to Variable Name.
- Variable Name: This is the name of the NS variable. Variable name must be declared in the spec file and it should be of the type that supports setting the value.
- Return Variable: ns_save_string assigns the null terminated variable value to NS Variable variable_name. For function to succeed, it returns 0 on success and -1 on failure.
Example: nsApi.ns_save_string(“Load”, “action-type”);
This sets the value of NS variable action-type to Load.
“action-type” is NS variable that must be present in registrations.spec file.
nsApi.ns_save_string(“null”, “action-type”);
#This sets the value of NS variable action-type to null.
ns_log_string
Description: This API is used to create a log of strings.
Fields
- Level: level is the log level to which the logged message belongs. Possible values of Level are: NS_LOG_STD, NS_LOG_EXTENDED, and NS_LOG_DEBUG.
- String Variable: This is the string message to be logged or may be a string variable already defined in java file.
- Return Variable: This API returns an integer value which should be stored in a return variable of int
ns_get_session_status
Description: This API is used to get the current session status.
Syntax: nsApi.ns_get_session_status();
Return Value: Returns 0 on success.
Example
int SessCode; SessCode = nsApi.ns_get_session_status(); System.out.println(“current session status is:”, + SessCode);
ns_get_auto_cookie_mode
Description: This API returns mode value which may have following possible values:
- Disable auto cookie, that is, do not use Auto cookie (Default)
- Use auto cookie with name, path and domain
- Use auto cookie with name only
- Use auto cookie with name and path only
- Use auto cookie with name and domain only
Syntax: nsApi.ns_auto_cookie_mode();
Return Value: Returns integer value.
Example
int mode; mode = nsApi.ns_get_auto_cookie_mode(); System.out.println(“The output will be:”, + mode);
- 0 for Disable auto cookie
- 1 for Use auto cookie with name, path and domain
- 2 for Use auto cookie with name only
- 3 for Use auto cookie with name and path only
- 4 for Use auto cookie with name and domain only
ns_get_cookies_disallowed
Description: This API is used to get disallowed cookie flag which shows whether or not cookies are allowed. It sets the cookie allowed or not flag. Val = 1 means cookies are disallowed.
Syntax: nsApi.ns_get_cookies_disallowed();
Return Value: This API returns Boolean value.
Example
int Flag; Flag = nsApi.ns_get_cookies_disallowed();
ns_get_user_ip
Description: This API is used to get the user IP.
Syntax: nsApi.ns_get_user_ip();
Return Value: Returns the user IP.
Example
String uip = nsApi.ns_get_user_ip(); System.out.println(“The output of above API will be”, + uip);
ns_is_rampdown_user
Description: This API is used to check whether the vuser is in rampdown phase or not.
Syntax: nsApi.ns_is_rampdown_user(void);
Return Value: This API returns integer value indicating ramp (Up/Down) phase.
Example
int isRampDown = nsApi.ns_is_rampdown_user(); System.out.println(“The output of above API is ”, + isRampDown);
ns_get_pg_think_time
Description: This API is used to get the page think time.
Syntax: nsApi.ns_get_pg_think_time()
Return Value: This function returns integer value.
Example: nsApi.ns_get_pg_think_time(2.0);
Page think time will be applied of 2 seconds.
ns_get_int_val
Description: This API is used to get the integer value of the NS variable (or parameter) var_name.
Syntax: nsApi.ns_get_int_val(char var_name);
Return Value: Returns 0 on success and –1 on failure. Function fails, if the variable is invalid or variable length is greater than and equal to 4096.
Example
int val = 0; val = nsApi.ns_get_int_val(“ns_variable”); System.out.println(“integer value of the parameter is:”, + val);
ns_set_int_val
Description: This API is used to set the value to an NS variable (or parameter).
Syntax: nsApi.ns_set_int_val(char var_name, int value);
Return Value: Returns 0 on success, -1 if not allowed, -2 if size is more than max size.
Example
int value = 0; Value = nsApi.ns_set_int_val(“ns_variable”,12); System.out.println(“The Output will be:”, + Value);
ns_get_cookie_idx
Description: This API is used to get the cookie IDX value and set its value to the return variable.
Syntax: nsApi.ns_get_cookie_idx(String cname, String sname)
- Cname – Cookie Name
- Sname – Script Name
Return Value: This API returns the integer identity of the cookie.
Example
int idx = nsApi.ns_get_cookie_idx(“cname”, “sname”); System.out.println(“Output will be”, + idx);
ns_get_cookie_val
Description: This API is used to get the cookie value.
Syntax: nsApi.ns_get_cookie_val(int cookie_idx);
Return Value: This API returns string value.
Example
String s_val_cookie = nsApi.ns_get_cookie_val(0); System.out.println(“Output will be:”, + s_val_cookie);
ns_get_cookie_val_ex
Description: This API is used to get cookie value populated in a static buffer.
Syntax: nsApi.ns_get_cookie_val_ex(String cookie_name, String domain, String path);
Where,
- cookie_name – Cookie Name
- domain – Get Cookie of this domain. NULL if domain is not to be checked
- path – Get Cookie of this path. NULL if path is not to be checked
Return Value: This API returns Pointer to cookie value populated in a static buffer or NULL if cookie name is not found.
Example
String val = nsApi.ns_get_cookie_val_ex(“JsessionId”,”www.server1.com”,”/”); System.out.println(“Output will be:”, + val); String sessionIdValue = nsApi.ns_get_cookie_val_ex("JsessionId", NULL, NULL); System.out.println(“Output will be:”, + sessionIdValue);
ns_set_cookie_val
Description: This API takes cookie identity and cookie value to set.
Syntax: nsApi.ns_set_cookie_val(int cookie_idx, String cookie_val);
Return Value: None
Example
cookieIdx = nsApi.ns_get_cookie_idx(); int val = nsApi.ns_set_cookie_val(1, “cookie_val”); System.out.println(“The output will be:”, + val);
ns_set_cookie_val_ex
Description: This API is used to set the value of the cookie.
Syntax: nsApi.ns_set_cookie_val_ex(String cookie_name, String domain, String path, String cookie_val);
Where,
- cookie_name – Cookie Name
- domain – Get Cookie of this domain. NULL if domain is not to be check
- path – Get Cookie of this path. NULL if path is not to be check
- cookie_val – Value of the cookie
Return Value: Return 0 on success and -1 if on failure.
Example
int setCookieVal= nsApi.ns_set_cookie_val_ex(“sessionId”, ”www.server1.com”, ”/”, ”as001”); System.out.println(“Output will be:”, + setCookieVal);
ns_set_ua_string
Description: This API is used to set the user agent value.
Syntax: nsApi.ns_set_ua_string(String ua_static);
Return Value: Not applicable.
Example
String ua_static; ua_static = “Mozilla 5.0 ….” nsApi.ns_set_ua_string(ua_static);
ns_set_cookies_disallowed
Description: This API sets the cookie allowed or not flag. Val = 1 means cookies are disallowed.
Syntax: nsApi.ns_set_cookies_disallowed(int val);
Return Value: Not applicable.
Example
int val = 1; nsApi.ns_set_cookies_disallowed(val);
ns_add_cookie_val_ex
Description: This API is used to add the cookie value.
Syntax: nsApi.ns_add_cookie_val_ex(String cookie_name, String domain, String path, String cookie_val);
Where
- Cookie_name: Cookie Name
- Domain: Get Cookie of this domain. NULL if domain is not to be checked
- Path: Get Cookie of this path. NULL if path is not to be checked
- cookie_val: Value of the cookie
Return Value: Return 0 on success and -1 on failure.
Example
int status=nsApi.ns_add_cookie_val_ex("JsessionId, "www.server1.com", "/", "session001"); System.out.println("Add cookie status is: "+ status);
ns_advance_param
Description: This API advances to the next available value in the parameter data file. Next value is based on the MODE of the parameter.
Syntax: nsApi.ns_advance_param(“param_name”);
param_name is a file parameter.
Return Value: Return 0 on success and -1 on failure.
Example: nsApi.ns_advance_param(“Param1”);
where Param1 is a file parameter.
ns_paramarr_idx
Description: This API returns a string containing the value of the parameter at the specified position in a parameter array. If the parameter does not exist, the return value is the concatenation of “{“, paramArrayName, “_”, index and “}”.
Syntax: nsApi.ns_paramarr_idx(const String paramArrayName, unsigned int index);
Arguments: Parameter array and index value.
Return Value: On success: Return a string buffer containing the value. On failure: Return NULL.
Example: nsApi.ns_paramarr_idx(“myParam”, 3);
ns_paramarr_len
Description: This API gets the number of elements in parameter array.
Syntax: nsApi.ns_paramarr_len(const String paramArrayName);
Return Value:
- On success: Returns the number of elements in parameter array.
- On failure: return -1
Example
int val=nsApi.ns_paramarr_len(“myParam”);
System.out.println(“value is: “+ val);
ns_paramarr_random
Description:This API returns a string containing the value of the parameter in a parameter array. The value is returned from the parameter at a position chosen randomly by NetStorm.
If the parameter does not exist, the return value is the concatenation of “{“, paramArrayName, “_”, index and “}”.
Example: “{myParam_4}”
Syntax: nsApi.ns_paramarr_random(const String paramArrayName);
Return Value
- On success: Return a string buffer containing the value.
- On failure: Return NULL.
Example
String val=nsApi.ns_paramarr_random(“myParam”);
System.out.println(“value is: “+ val);
ns_check_reply_size
Description: This API is used to compare the response size with value1 and value2.
Arguments:
- mode: This indicates the mode value. Currently NotBetweenMinMax mode is supported only.
- value1: This indicates the user defined value1 that is used to compare with response size.
- value2: This indicates the user defined value2 that is used to compare with response size.
Returns value:
- 0 : on success if the response size is as per mode.
- 1 : if response/reply size is small.
- 2 : if response/reply size is big.
- -1 : on error
Syntax: nsApi.ns_check_reply_size(int mode, int value1, int value2);
Example: int replySize = nsApi.ns_check_reply_size(1,100,200);
ns_start_transaction
Description: This API acts as a marker to define the start of a transaction. Transaction timing measurement would start at the start of accessing the next page.
Syntax: nsApi.ns_start_transaction(String tx_name);
Argument: tx_name is any string and specifies the name of the transaction being started.
Return Value: This API returns 0 on success and -1 on failure.
Example: int replySize = nsApi.ns_start_transaction(“login”);
ns_end_transaction
Description: This API acts as a marker to define the end of a transaction.
Syntax: nsApi.ns_end_transaction(String tx_name, int status);
Argument: tx_name is any string and specifies the name of the transaction being ended. A transaction by the specified transaction name should have been successfully started earlier, for this function to succeed. Status is the transaction status. Possible values of status can be 0-15. 0 specifies success and -1 specifies failure. Status codes 1-15 are other user defined transaction failures status codes.
Return Value: This API returns 0 on success and -1 on failure.
Example: int replySize = nsApi.ns_end_transaction(“login”, NS_AUTO_STATUS);
ns_end_transaction_as
Description: This API acts as a marker to define the end of a specified transaction name. [end_name].
Syntax: nsApi.ns_end_transaction_as(String tx_name, int status, String end_name);
Argument:
- tx_name is any string and specifies the name of the transaction being ended. A transaction by the specified transaction name should have been successfully started earlier, for this function to succeed. Status is the transaction status. Possible values of status can be 0-15. 0 specifies success and -1 specifies failure. Status codes 1-15 are other user defined transaction failures status codes.
- end_name: Transaction name.
Return Value: This API returns 0 on success and -1 on failure.
Example: int replySize = nsApi.ns_end_transaction_as(“login”, NS_AUTO_STATUS, “login_fail”);
ns_web_url
Description: It is an action API that is used to send http request.
Syntax
nsApi.ns_web_url(“<PageName>”,
“URL=<Url>”,
“Method=<sHTTPMethod>”, (default is Get)
“Header=<HeaderName: HeaderValue>”, (0 or more)
“Cookie=<cookies names separated by ;>”,
“Body=<one or more lines of Body>”,
INLINE_URLS,
“URL=http://www.example.com/main.css”, END_INLINE,
“URL=http://www.example.com/logo.gif”, “Method=Post”,
“BODY=This is a body of inline url with quotes(“) and multiple lines.
Line2 of body”,
END_INLINE
);
Note: The URL field is mandatory.
Return Value: This API returns 0 on success.
Example
int status=0;
status = nsApi.ns_web_url (“index_html”,
“URL=http://www.server.com/tours/index.html”,
“HEADER=Accept-Language:en-us”,
“HEADER=Upgrade-Insecure-Requests:1”,
INLINE_URLS,
“URL=http://www.server.com/tours/Merc10-dev/images/banner_animated.gif”, “HEADER=Accept-Language:en-us”, END_INLINE,
“URL=http://www.server.com/tours/Merc10-dev/images/sun_swede.gif”, “HEADER=Accept-Language:en-us”, END_INLINE,
“URL=http://www.server.com/tours/Merc10-dev/images/login.gif”, “HEADER=Accept-Language:en-us”, END_INLINE,
“URL=http://www.server.com/tours/images/banner_merctur.jpg”, “HEADER=Accept-Language:en-us”, END_INLINE
);
ns_set_session_status
Description: This API is used to set session status forcefully on the basis of requirement.
Syntax: nsApi.ns_set_session_status(int status);
Note: This API must be used just before end_session so that session status can be set successfully.
Return Value: This API returns 0 on success.
Example
int ret_sess_status=nsApi.ns_set_session_status(0);
Set session status success forcefully in case of any failure within a session.
System.out.println(“\n\n ### set session status is:”, + ret_sess_status);
ns_add_cookies
Description: This API is used to send custom cookies with http request.
Syntax: nsApi.ns_add_cookies(String cookie_buf);
The input buffer should be in the following format:
<CookieName>=<CookieValue>;path=<path>;domain=<domain>,<other cookies>
Note: Cookies are separated by a comma.
Return Value: This API returns 0 on success.
Example
int cookie_status=nsApi.ns_add_cookies(“Cookie1=value1;path=/;domain=www.domain1.com,Cookie2=value2;path=/;domain=www.domain2.com”);
System.out.println(“cookie status is :”+ cookie_status);
ns_set_ka_time
Description: This API is used to set KeepAlive time.
Syntax: nsApi.ns_set_ka_time(int ka_time);
Return Value: Return type is NA.
Example: nsApi.ns_set_ka_time(30);
It will set the KeepAlive time to 30 milliseconds.
ns_get_ka_time
Description: This API is used to get KeepAlive time.
Return Value: Return time in milliseconds.
Example
nsApi.ns_set_ka_time(30);
int val=nsApi.ns_get_ka_time();
System.out.println(“ka time is: “+val);
The output will be 30.
ns_get_double_val
Description: This API is used to get the double value of the NS variable (or parameter).
Syntax: nsApi.ns_get_double_val(String var_name);
Return type int to get the API status.
Return Value: This API returns 0 on success and -1 on failure.
Example
nsApi.ns_save_string(“5″,”var”);
Value “5” saved in declare_var “var” as a string.
double data=nsApi.ns_get_double_val(“var”);
System.out.println(“\n####Value of var in double:”+ data);
The output will be 5.0.
ns_set_double_val
Description: This API is used to set the valu e to an NS variable (or parameter).
Syntax: nsApi.ns_set_double_val(const String param_name, double value);
Return Value: This API returns 0 on success and -1 on failure.
Example: double data=nsApi.ns_set_double_val(“var”,8.0);
This will set the value of “var” as double type.
System.out.println(“####Value of var is: “+ nsApi.ns_eval_string(“{var}”));
The output will be 8.000000.
ns_get_transaction_time
Description: This API is used to get the total time in milliseconds taken by the transaction until it is called. This must be called before ending the transaction.
Syntax: nsApi.ns_get_transaction_time(String transaction_name);
Return Value: Return time in milliseconds.
Example
int time=nsApi.ns_get_transaction_time(“login”);
System.out.println(“####Transaction time is: “+ time);
It will print time in milliseconds.
ns_set_tx_status
Description: This API is used for setting the status of a transaction. This must be called before ending the transaction.
Syntax: nsApi.ns_set_tx_status(String tx_name, int status);
Return Value: This API returns 0 on success and < 0 on failure.
Example: nsApi.ns_set_tx_status(“login”, NS_AUTO_STATUS);
ns_get_tx_status
Description: This API is used to get the transaction status. This must be called before ending the transaction.
Syntax: nsApi.ns_get_tx_status(String tx_name);
Return Value: This API returns >=0 on success and <0 on failure.
Example: int tx_status=nsApi.ns_get_tx_status(“login”);
Where, login is the transaction name.
ns_sync_point
Description: This API is used to apply sync point.
Syntax: nsApi.nsApi.ns_sync_point(String sp_name);
Return Value: This API returns 0 on success.
Example: nsApi.ns_sync_point(“cavisson”);
Where, cavisson is the sync point name.
ns_get_page_status
Description: This API is used to get the page status.
Syntax: nsApi.ns_get_page_status(void);
Return Value: This API returns 0 on success.
Example: int page_status=nsApi.ns_get_page_status();
ns_page_think_time
Description: This API is used to start think timer with value passed.
Syntax: nsApi.ns_page_think_time(double page_think_time)
Return Value: This API returns 0 on success and -1 on failure.
Example: nsApi.ns_page_think_time(2.0);
Think time will be applied of 2 seconds.
ns_start_timer
Description: This API is used to start the timer that calculates passage of time in milliseconds. The time is relative from the time of start test.
Syntax: long nsApi.ns_start_timer(String startTimerName)
Return Value: This API returns the start time, that is, the time when the timer started.
Example: nsApi.ns_start_timer(“checkout”);
ns_end_timer
Description: This API is used to stop a timer that began timing when ns_start_timer was called.
Syntax: long nsApi.ns_end_timer(String endTimerName)
Return Value: This API returns elapsed time in milliseconds.
Example: nsApi.ns_end_timer(“checkout”);