Skip to main content

Sample Script with Regions SectionsSample WebSockets Script with Regions Sections

The following sample script shows the regions sections in which you can place your custom code so that it is not overwritten or lost when the script is regenerated. See Regions for more information about the different regions.

package com.testplant.testing;
import java.time.Duration;
import java.util.EnumSet;
import java.util.List;
import java.util.ArrayList;
import com.facilita.fc.runtime.*;
import com.facilita.fc.runtime.backgroundScripting.*;
import com.facilita.util.*;
import com.facilita.exception.*;
import com.facilita.fc.web.*;
import com.facilita.fc.jni.*;
// #region EPP_IMPORTS
// Code added here will be preserved during script regeneration
// #endregion EPP_IMPORTS
public class MyScript extends com.testplant.testing.MyCustomJavaScript
{
// Generated variables
IpEndPoint myWebsite = null; // a parameterized web server address
Protocol protocol = null; // a parameterized protocol
// End of generated variables
// Your global variables
// #region EPP_GLOBAL_VARIABLES
// Code added here will be preserved during script regeneration
// #endregion EPP_GLOBAL_VARIABLES
// End of your global variables
public void pre() throws Exception
{
//do not remove following line
super.pre();
// The following INITIALISATION is executed once only.
// START INITIALISATION CODE
getWebBrowser().setDefaultUserAgent(getString("User-Agent","Mozilla/5.0"));
// END INITIALISATION CODE
// Put any code that needs to execute at the start of the test here
// #region EPP_PRE
// Code added here will be preserved during script regeneration
// #endregion EPP_PRE
}
public void script() throws Exception
{
// This 'script' method is the main method executed by the runtime engine.
myWebsite = new IpEndPoint(getString("myHost", "www.mywebsite.com"), getInt("myPort", 80));
protocol = getProtocol("protocol", "http");
// Actions
// Note: if you have complex logic that you would like to preserve between re-generations of the script,
// you can turn on the "Script Actions" region in the Advanced web script generation options
// #region EPP_ACTIONS
// Code added here will be preserved during script regeneration
Action1_Login();
Action2_Logout();
// #endregion EPP_ACTIONS
// Put your code for finalizing the script here
// #region EPP_SCRIPT
// Code added here will be preserved during script regeneration
// #endregion EPP_SCRIPT
}
void Action1_Login() throws Exception
{
// #region EPP_BEFORE_START_TRANSACTION for Transaction "Login"
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_START_TRANSACTION for Transaction "Login"
startTransaction("Login");
Url url1 = new Url(protocol, myWebsite, "/login");
try (Request request1 = getWebBrowser().createRequest(HttpMethod.GET, url1, 1))
{
// References to the following page resources could not be found within the contents of the received web page.
// This can occur because resources are fetched as a result of style sheets or the browser executing script code e.g. JavaScript.
request1.addSubRequest(protocol, myWebsite, "/image.png"); // Request: 2, 200 OK
// #region EPP_BEFORE_REQUEST_SENT for Request 1
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_REQUEST_SENT for Request 1
try (Response response1 = request1.send())
{
// #region EPP_AFTER_RESPONSE_RECEIVED for Request 1
// Code added here will be preserved during script regeneration
// #endregion EPP_AFTER_RESPONSE_RECEIVED for Request 1
// Rule: Verify that the page title matches what was recorded
response1.verifyTitle("My website", ActionType.ACT_WARNING);
}
}
// #region EPP_BEFORE_END_TRANSACTION for Transaction "Login"
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_END_TRANSACTION for Transaction "Login"
endTransaction("Login");
}
void Action2_Logout() throws Exception
{
// #region EPP_BEFORE_END_TRANSACTION for Transaction "Logout"
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_END_TRANSACTION for Transaction "Logout"
startTransaction("Logout");
Url url3 = new Url(protocol, myWebsite, "/logout");
try (Request request3 = getWebBrowser().createRequest(HttpMethod.GET, url3, 3))
{
// References to the following page resources could not be found within the contents of the received web page.
// This can occur because resources are fetched as a result of style sheets or the browser executing script code e.g. JavaScript.
request3.addSubRequest(protocol, myWebsite, "/image.png"); // Request: 4, 200 OK
// #region EPP_BEFORE_REQUEST_SENT for Request 3
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_REQUEST_SENT for Request 3
try (Response response3 = request3.send())
{
// #region EPP_AFTER_RESPONSE_RECEIVED for Request 3
// Code added here will be preserved during script regeneration
// #endregion EPP_AFTER_RESPONSE_RECEIVED for Request 3
// Rule: Verify that the page title matches what was recorded
response3.verifyTitle("My website", ActionType.ACT_WARNING);
}
}
// #region EPP_BEFORE_END_TRANSACTION for Transaction "Logout"
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_END_TRANSACTION for Transaction "Logout"
endTransaction("Logout");
}
}

The following sample WebSockets script shows the regions sections in which you can place your custom code so that it is not overwritten or lost when the script is regenerated. See Regions for more information about the different regions.

package com.testplant.testing;
import java.time.Duration;
import java.util.EnumSet;
import java.util.List;
import java.util.ArrayList;
import com.facilita.fc.runtime.*;
import com.facilita.fc.runtime.backgroundScripting.*;
import com.facilita.util.*;
import com.facilita.exception.*;
import com.facilita.fc.web.*;
import com.facilita.fc.jni.*;
// #region EPP_IMPORTS
// Code added here will be preserved during script regeneration
// #endregion EPP_IMPORTS
public class MyWebSocketScript extends com.testplant.testing.MyCustomJavaScript
{
// Generated variables
IpEndPoint myWebsite = null; // a parameterized web server address
Protocol protocol = null; // a parameterized protocol
// End of generated variables
// Your global variables
// #region EPP_GLOBAL_VARIABLES
// Code added here will be preserved during script regeneration
// #endregion EPP_GLOBAL_VARIABLES
// End of your global variables
public void pre() throws Exception
{
//do not remove following line
super.pre();
// The following INITIALISATION is executed once only.
// START INITIALISATION CODE
getWebBrowser().setDefaultUserAgent(getString("User-Agent","Mozilla/5.0"));
// END INITIALISATION CODE
// Put any code that needs to execute at the start of the test here
// #region EPP_PRE
// Code added here will be preserved during script regeneration
// #endregion EPP_PRE
}
public void script() throws Exception
{
// This 'script' method is the main method executed by the runtime engine.
myWebsite = new IpEndPoint(getString("myHost", "www.mywebsite.com"), getInt("myPort", 80));
protocol = getProtocol("protocol1", "http");
// Actions
// Note: if you have complex logic that you would like to preserve between re-generations of the script,
// you can turn on the "Script Actions" region in the Advanced web script generation options
// #region EPP_ACTIONS
// Code added here will be preserved during script regeneration
Action1_Start();
// #endregion EPP_ACTIONS
// Put your code for finalizing the script here
// #region EPP_SCRIPT
// Code added here will be preserved during script regeneration
// #endregion EPP_SCRIPT
}
void Action1_Start() throws Exception
{
// #region EPP_BEFORE_START_TRANSACTION "Start"
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_START_TRANSACTION for Transaction "Start"
startTransaction("Start");
Url url1 = new Url(protocol, myWebsite, "/StockTicker");
try (Request request1 = getWebBrowser().createRequest(HttpMethod.GET, url1, 1))
{
request1_ProcessSubRequests(request1);
// #region EPP_BEFORE_REQUEST_SENT for Request 1
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_REQUEST_SENT for Request 1
try (Response response1 = request1.send())
{
// #region EPP_AFTER_RESPONSE_RECEIVED for Request 1
// Code added here will be preserved during script regeneration
// #endregion EPP_AFTER_RESPONSE_RECEIVED for Request 1
// Rule: Verify that the page title matches what was recorded
response1.verifyTitle("My website", ActionType.ACT_WARNING);
}
}
Url url2 = new Url(protocol, myWebsite, "/StockTicker/start");
QueryData queryData2 = new QueryData();
queryData2.add("transport", "webSockets");
queryData2.add("connectionData", "[{\"name\":\"stockticker\"}]");
url2 = url2.withQuery(queryData2);
WebSocket webSocket2 = getWebBrowser().createWebSocket(url2, 2);
set("webSocketMessageQueue2", new WebSocketMessageQueue(webSocket2));
webSocket2.registerReceivedMessageCallback((ws, message) -> webSocket2_onReceivedMessage(ws, message));
webSocket2.registerErrorCallback((ws, errorMessage) -> webSocket2_onError(ws, errorMessage));
webSocket2.registerClosedCallback((ws, closedByClient, reason)-> webSocket2_onClosed(ws, closedByClient, reason));
// #region EPP_BEFORE_WEB_SOCKET_OPENED for WebSocket 2
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_WEB_SOCKET_OPENED for WebSocket 2
webSocket2.open();
// #region EPP_AFTER_WEB_SOCKET_OPENED for WebSocket 2
// Code added here will be preserved during script regeneration
// #endregion EPP_AFTER_WEB_SOCKET_OPENED for WebSocket 2
set("webSocket2", webSocket2);
// webSocket2 message 1
// #region EPP_BEFORE_WEB_SOCKET_SEND_MESSAGE for WebSocket 2 of frame 1
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_WEB_SOCKET_SEND_MESSAGE for WebSocket 2 of frame 1
((WebSocket)get("webSocket2")).sendMessage("{\"S\": \"EPP\"}");
// #region EPP_AFTER_WEB_SOCKET_SEND_MESSAGE for WebSocket 2 of frame 1
// Code added here will be preserved during script regeneration
// #endregion EPP_AFTER_WEB_SOCKET_SEND_MESSAGE for WebSocket 2 of frame 1
// webSocket2 message 2
// Received text message: {\"R\":[{\"Symbol\":\"EPP\",\"DayOpen\":543.01,\"DayLow\":541.46,\"DayHigh\":544.32,\"LastChange\":... (truncated)
/* This WebSocketMessageQueue code and the surrounding regions
only appear if a WebSocket rule is defined
with the "Incoming messages" option set to "Generate code to wait
for the messages to arrive from the server" */
// #region REGION_BEFORE_WEB_SOCKET_MESSAGE_QUEUE_GET_NEXT_MESSAGE for WebSocket 2 of frame 2
// Code added here will be preserved during script regeneration
// #endregion REGION_BEFORE_WEB_SOCKET_MESSAGE_QUEUE_GET_NEXT_MESSAGE for WebSocket 2 of frame 2
try (WebSocketMessage webSocket2_message2 = ((WebSocketMessageQueue)get("webSocketMessageQueue2")).getNextMessage(60000))
{
// #region REGION_AFTER_WEB_SOCKET_MESSAGE_QUEUE_GET_NEXT_MESSAGE for WebSocket 2 of frame 2
// Code added here will be preserved during script regeneration
// #endregion REGION_AFTER_WEB_SOCKET_MESSAGE_QUEUE_GET_NEXT_MESSAGE for WebSocket 2 of frame 2
}
// #region EPP_BEFORE_START_TRANSACTION "Start"
// Code added here will be preserved during script regeneration
// #endregion EPP_BEFORE_START_TRANSACTION for Transaction "Start"
endTransaction("Start");
}
private void webSocket2_onReceivedMessage(WebSocket webSocket, WebSocketMessage message) throws Exception
{
// #region EPP_WEB_SOCKET_RECEIVED_MESSAGE_CALLBACK for WebSocket 2
// Code added here will be preserved during script regeneration
// Put code here to process messages received on webSocket2
// #endregion EPP_WEB_SOCKET_RECEIVED_MESSAGE_CALLBACK for WebSocket 2
}
private void webSocket2_onError(WebSocket webSocket, String errorMessage) throws Exception
{
// #region EPP_WEB_SOCKET_ERROR_CALLBACK for WebSocket 2
// Code added here will be preserved during script regeneration
// Put code here to handle errors for webSocket2
// #endregion EPP_WEB_SOCKET_ERROR_CALLBACK for WebSocket 2
}
private void webSocket2_onClosed(WebSocket webSocket, boolean closedByClient, String reason) throws Exception
{
// #region EPP_WEB_SOCKET_CLOSED_CALLBACK for WebSocket 2
// Code added here will be preserved during script regeneration
// Put code here to be executed when webSocket2 is closed
// #endregion EPP_WEB_SOCKET_CLOSED_CALLBACK for WebSocket 2
}
private void request1_ProcessSubRequests(Request request1) throws Exception
{
// References to the following page resources could not be found within the contents of the received web page.
// This can occur because resources are fetched as a result of style sheets or the browser executing script code e.g. JavaScript.
request1.addSubRequest(protocol, myWebsite, "/StockTicker/style.css");
}
}