|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Test
The main control interface for Neustar Web Performance Management scripts - All scripts must start here first.
This object is available as the variable test from within the a JavaScript-enabled script. This object is
effectively the "starting point" for all other scripts, as it serves as the interface for interacting with web sites
(via openBrowser()
and openHttpClient()
as well as the mechanism for
controlling transactions and steps (via beginTransaction()
and beginStep(String)
).
For automating real and headless browsers, scripts can use Selenium WebDriver (See openBrowser()
).
In this example we open a browser and perform a google search using Selenium WebDriver:
var driver = test.openBrowser(); test.beginTransaction(); test.beginStep("Navigate to google.com"); driver.get("http://www.google.com"); test.endStep(); test.beginStep("Search for Cheese!"); // Find the text input element by its name var element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); test.waitFor(function() { return driver.getTitle().toLowerCase().startsWith("cheese!") }, 2000); test.endStep(); test.endTransaction();
A Selenese API that is also available for users more comfortable with scripts recorded with the Selenium IDE.
In example we navigate to google and do a search using the Selenese API:
var driver = test.openBrowser(); var selenium = driver.getSelenium(); test.beginTransaction(); test.beginStep("Navigate to google.com"); selenium.open("http://www.google.com"); test.endStep(); test.beginStep("Search for Cheese!"); // Type Cheese into the search box selenium.type("name=q", "cheese!"); // Submit the search form selenium.submit("name=gs"); // Wait for the cheese.com to show up in the search results selenium.waitForTextPresent("CHEESE.COM - All about cheese!") test.endStep(); test.endTransaction();
This object also provides other useful functions, such as assistance with file uploads (filePath(String)
),
simulating "user think time" (pause(long)
), and parameterizing tests with unique data (getUserNum()
and getTxCount()
).
Method Summary | |
---|---|
void |
assertEquals(java.lang.Object expected,
java.lang.Object actual)
Assert that two values are equal. |
void |
assertEquals(java.lang.Object expected,
java.lang.Object actual,
java.lang.String message)
Assert that two values are equal. |
void |
assertFalse(boolean assertion)
Assert that the given condition is false. |
void |
assertFalse(boolean assertion,
java.lang.String message)
Assert that the given condition is false. |
void |
assertNotEquals(java.lang.Object expected,
java.lang.Object actual)
Assert that two values are not equal. |
void |
assertNotEquals(java.lang.Object expected,
java.lang.Object actual,
java.lang.String message)
Assert that two values are not equal. |
void |
assertThrown(NativeFunction function)
Assert that the running the JS function threw an exception when run, otherwise an exception is thrown. |
void |
assertThrown(java.lang.String exceptionMessage,
NativeFunction function)
Assert that the running the JS function threw an exception when run. |
void |
assertTrue(boolean assertion)
Assert that the given condition is true. |
void |
assertTrue(boolean assertion,
java.lang.String message)
Assert that the given condition is true. |
TransactionStep |
beginStep()
Starts a new step (must be called from within a transaction). |
TransactionStep |
beginStep(NativeFunction stepFunction)
Starts a new step (must be called from within a transaction) by calling the given function, and automatically end the step when the function has finished running. |
TransactionStep |
beginStep(java.lang.String stepLabel)
Starts a new step (must be called from within a transaction). |
TransactionStep |
beginStep(java.lang.String stepLabel,
int timeoutMilliseconds,
NativeFunction stepFunction)
Starts a new step (must be called from within a transaction) with a timeout specified in milliseconds by calling the given function, and automatically end the step when the function has finished running. |
TransactionStep |
beginStep(java.lang.String stepLabel,
NativeFunction stepFunction)
Starts a new step (must be called from within a transaction) with a timeout specified in milliseconds by calling the given function, and automatically end the step when the function has finished running. |
Transaction |
beginTransaction()
Starts a new transaction. |
Transaction |
beginTransaction(NativeFunction transactionFunction)
Starts a new transaction that runs the given transaction function, and automatically ends the
transaction when the function has completed. |
Transaction |
beginTransaction(NativeFunction transactionFunction,
NativeObject params)
Starts a new transaction that runs the given transaction function, and automatically ends the
transaction when the function has completed. |
void |
closeBrowser()
Terminates the active Selenium browser session. |
DataFile |
datafile(java.lang.String name)
Returns a wrapped handle to an uploaded file. |
void |
endStep()
Stops the current step. |
void |
endTransaction()
Ends the current transaction. |
void |
endTransaction(NativeObject params)
Ends the current transaction. |
void |
fail()
Fails the test by throwing an exception. |
void |
fail(java.lang.String message)
Fails the test with a message by throwing exception with the given message. |
java.lang.String |
filePath(java.lang.String name)
Returns the local path for the specified file name. |
CSVTable |
fileToCSV(DataFile dataFile)
Creates and returns a table of data backed by a Comma Separated Value File. |
java.lang.String[] |
findRegexMatches(java.lang.String content,
java.lang.String regex)
A simple way to find one or more regular expression matches against some content. |
void |
forEach(java.lang.Object o,
NativeFunction function)
Loops through a list/map, applying the function to each element. |
TransactionStep |
getActiveStep()
Returns the currently active step, which is useful for advanced use cases where the step object is being manipulated directly. |
Transaction |
getActiveTransaction()
Returns the currently active transaction, which is useful for advanced use cases where the transaction object is being manipulated directly. |
CSVTable |
getCSV(java.lang.String name)
Creates and returns a table of data backed by a Comma Separated Value file. |
java.lang.String |
getLocation()
Returns the location that the script is running from. |
int |
getTxCount()
Returns the count of transactions that have been completed (regardless of success or failure) in the current script. |
int |
getUserNum()
Returns the unique number for each independent script executed during a load test. |
java.lang.String |
getVersion()
Return the Test Scripts API version. |
void |
globalPause(long pauseTimeInMs)
Sleep between steps for the duration passed in param. |
void |
globalPause(long minTimeInMS,
long maxTimeInMS)
Sleep between steps. |
void |
include(java.lang.String className)
Includes a JavaScript file into the current script's top level scope. |
boolean |
isLoadTesting()
Indicates whether the current execution context is within a load test. |
boolean |
isScriptValidator()
Indicates if the current execution environment is the local script validator. |
boolean |
isValidation()
Indicates if the current execution environment is an actual real time script playback or if this is simply a script validation run that is used during script development. |
LoadTestSettings |
loadTestSettings()
Get load testing specific settings, these settings have no effect while monitoring. |
void |
log(java.lang.String log)
Logs some text to the log buffer. |
long |
nextSequence(java.lang.String sequenceName)
Retrieves the next non-zero, positive integer in a particular sequence. |
WebDriver |
openBrowser()
Starts a Selenium WebDriver browser session. |
HttpClient |
openHttpClient()
Starts a basic HTTP client that is capable of GET and POST requests, allowing the script author to simulate real web browsers. |
WebSocket |
openWebSocket(java.lang.String url)
Opens a W3C WebSocket at the give URL. |
WebSocket |
openWebSocket(java.lang.String url,
java.lang.String version)
Opens a W3C WebSocket at the give URL. |
void |
pause(long pauseTimeInMs)
Instructs the script to pause for a specified amount of time. |
void |
pause(long pauseTimeInMs,
boolean includeInTimeActive)
Instructs the script to pause for a specified amount of time. |
void |
quickStop()
Normally when a load test is paused or the load level drops to fewer concurrent users, the actively running scripts are allowed to finish executing and complete the current transaction. |
void |
setBandwidthLimitMode(java.lang.String mode)
Turn on/off bandwidth limiting. |
void |
setDownstreamKbps(long downstreamKbps)
Sets an artificial kilobits-per-second value for all data downloaded. |
void |
setLatency(long latency)
Sets an artificial latency simulation for sending one full MTU (~1500 bytes on the internet). |
void |
setUpstreamKbps(long upstreamKbps)
Sets an artificial kilobits-per-second value for all data upload from the script (including headers). |
void |
startTimeoutTimer(long timeoutInMs)
Starts a timer that runs until timeoutInMs is reached, and if it has been reached, will terminate the running script. |
void |
stopExpected()
Indicate that the script is expecting that it could be stopped at any time and that if that happens, it should not be considered an error. |
void |
stopTimeoutTimer()
Stops the timeout timer, if it was started. |
void |
time(java.lang.String name)
Starts a timer you can use to track how long an operation takes. |
long |
timeEnd(java.lang.String name)
Ends and logs a timer if it exists |
boolean |
waitFor(NativeFunction callback,
long timeoutInMs)
Wait for the user specified function to return true. |
void |
waitForNetworkTrafficToStop(long quietPeriodInMs,
long timeoutInMs)
Waits until no HTTP traffic associated with the current test has taken place for at least quietPeriodInMs. |
void |
chromeoptions()
Options that you can use to customize and configure a ChromeDriver session. |
void |
firefoxoptions()
It is used to control the behaviour of Firefox. |
Method Detail |
---|
WebDriver openBrowser()
HttpClient openHttpClient()
void include(java.lang.String className)
className
- The name of the data file to include, minus the ".js" extension.WebSocket openWebSocket(java.lang.String url)
Checkout the WebSocket
class for details.
The other overload can be used to specific the websocket version to use.
url
- URL to a websocket server (i.e. "ws://example-websocket.com:8765")
WebSocket openWebSocket(java.lang.String url, java.lang.String version)
Checkout the WebSocket
class for details.
The WebSocket version should be one of:
"8" - which will give you WebSockets Hybi 10
"13" - which will give you WebSockets Hybi 17 / RFC 6455 (default)
url
- URL to a websocket server (i.e. "ws://example-websocket.com:8765")version
- The protocol version to connect using
java.lang.String getLocation()
Transaction beginTransaction()
Transaction beginTransaction(NativeFunction transactionFunction)
transaction
function, and automatically ends the
transaction when the function has completed.
For load testing scripts all transactions are recorded. For monitoring scripts only the final transaction is
recorded.
beginTransaction(function(transaction) { // steps });
transactionFunction
- the function to call for the transaction
Transaction beginTransaction(NativeFunction transactionFunction, NativeObject params)
transaction
function, and automatically ends the
transaction when the function has completed.
For load testing scripts all transactions are recorded. For monitoring scripts only the final transaction is
recorded.
beginTransaction(function(transaction) { // steps }, {persist: false, abortActiveRequests: false});
transactionFunction
- the function to call for the transactionparams
- A JavaScript object that contains key/value pairs for specific options when stopping the transaction
TransactionStep beginStep()
Use the override to set a step label and step timeout if required.
TransactionStep beginStep(java.lang.String stepLabel)
Use the override to set a step timeout if required.
stepLabel
- a description of the step, to be used for various reporting.
TransactionStep beginStep(NativeFunction stepFunction)
beginStep(function(step) { // do stuff });
stepFunction
- the function to call for the step
TransactionStep beginStep(java.lang.String stepLabel, NativeFunction stepFunction)
beginStep("Step 1", function(step) { // do stuff });
stepLabel
- the step's labelstepFunction
- the function to call for the step
TransactionStep beginStep(java.lang.String stepLabel, int timeoutMilliseconds, NativeFunction stepFunction)
beginStep("Step 1", 30000, function(step) { // do stuff });
stepLabel
- the step's labeltimeoutMilliseconds
- the step timeout in millisecondsstepFunction
- the function to call for the step
void endStep()
void pause(long pauseTimeInMs) throws java.lang.InterruptedException
pauseTimeInMs
- the time to pause (in milliseconds).
java.lang.InterruptedException
- if the script is paused, stopped, or aborted during executionvoid pause(long pauseTimeInMs, boolean includeInTimeActive) throws java.lang.InterruptedException
pauseTimeInMs
- the time to pause (in milliseconds).includeInTimeActive
- true to include in tx time, false otherwise
java.lang.InterruptedException
- if the script is paused, stopped, or aborted during executionvoid endTransaction()
#endTransaction(org.mozilla.javascript.NativeObject)
with
all the defaults.
void endTransaction(NativeObject params)
The possible parameters that can be supplied are:
test.endTransaction({persist: true/false (default is true), abortActiveRequests: true/false (default is true)});
The persist parameter controls whether the raw results will be saved to disk. The results will always be included in the aggregate results, but in some situations such as extremely large load tests, it is desirable to only store a small sampling of the raw results, ensuring that you aren't overwhelmed with raw data.
The abortActiveRequests parameter controls whether any open HTTP requests should be cut off while cleaning up the transaction that is ending. By default this is the preferred way to go, however in some rare cases, especially when looping multiple transactions in a load test, it is desirable to leave open connections, especially "long polling" and WebSocket requests that should maintain connectivity between transactions.
params
- A JavaScript object that contains key/value pairs for specific options when stopping the transactionTransaction getActiveTransaction()
TransactionStep getActiveStep()
void closeBrowser()
void setDownstreamKbps(long downstreamKbps)
If setBandwidthLimitMode()
is not set, the setting "perSocket"
will be used.
downstreamKbps
- the kilobits-per-second to simulate.void setUpstreamKbps(long upstreamKbps)
If setBandwidthLimitMode()
is not set, the setting "perSocket"
will be used.
upstreamKbps
- the kilobits-per-second to simulate.void setLatency(long latency)
For high speed connections, such as FIOS, 1ms is fine. To simulate a slower ADSL connection, 50ms might make sense. For really slow connections, 200ms or more may be appropriate.
If setBandwidthLimitMode()
is not set, the setting "perSocket"
will be used.
latency
- the latency in milliseconds.void setBandwidthLimitMode(java.lang.String mode)
setDownstreamKbps()
or setUpstreamKbps()
will
enable bandwidth limiting if it is not already.
Possible values are "on"
, "perSocket"
or "off"
.
"on"
limits the overall bandwidth used by the script. Default speeds are 10Mb per second download
and 5Mb per second upload. Call setDownstreamKbps()
and setUpstreamKbps()
to adjust
the default values.
"perSocket"
limits the bandwidth used on a per socket basis.
"off"
turns off the bandwidth limiting.
mode
- Either "on"
, "perSocket"
or "off"
.
By default is bandwidth limiting is "off"
.int getUserNum()
int getTxCount()
Note for load testing: This count is not the total transaction count currently completed in the load test. It is only the number of transaction recorded in the currently executing script.
java.lang.String filePath(java.lang.String name)
name
- the name of the file used by the script - usually a simple name like "foo.txt".
void log(java.lang.String log)
log
- the text to log.boolean isValidation()
pause(long)
function to a shorter time period
when validating the script.
Note: this method will return true also if isScriptValidator()
returns true!
boolean isScriptValidator()
pause(long)
function to a shorter time period when validating the script.
boolean isLoadTesting()
nextSequence(String)
long nextSequence(java.lang.String sequenceName)
The number is guaranteed to be unique within the current load test.
The first number returned by this method for a particular sequence is 1. Subsequent calls return 2, 3, 4, etc.
This function is only available when the isLoadTesting()
function returns true. A common use-case for this
function is to use it in conjunction with an external data file. If you have a data file with username/password records
and each pair can only be used once, you could use this function to guarantee that
each user within the load test retrieves a unique record in the data file.
A load test may have multiple sequences. A sequence is no longer valid after the load test ends.
If this method is invoked outside of the context of a load test, then the implementation will return 0 although this behaviour should not be relied upon as it may change in the future.
sequenceName
- the name of the sequence (up to 255 characters), must not be null
void stopExpected()
quickStop()
.
void quickStop()
stopExpected()
.
CSVTable getCSV(java.lang.String name)
name
- the name of the CSV file, which you will be asked to upload upon saving your script.
CSVTable fileToCSV(DataFile dataFile)
dataFile
- the name of the CSV file, which you will be asked to upload upon saving your script.
DataFile datafile(java.lang.String name)
HttpRequest.addFileUpload(String, DataFile)
.
name
- the filename, which you will be asked to upload upon saving your script.
void startTimeoutTimer(long timeoutInMs) throws java.lang.Exception
timeoutInMs
- the total time the script should wait before timing out
java.lang.Exception
- thrown when timeoutInMs is non-positive.void stopTimeoutTimer()
void waitForNetworkTrafficToStop(long quietPeriodInMs, long timeoutInMs)
This function will generally, but not always, result in a pause time equal to the length of quietPeriodInMs, since it will need to wait that long to determine if no network traffic has taken place.
quietPeriodInMs
- the period of time that we expect to not see any HTTP traffic.timeoutInMs
- the total time the function should wait before giving up and throwing an error.java.lang.String[] findRegexMatches(java.lang.String content, java.lang.String regex)
var matches = test.findRegexMatches(resp.getBody(), '<span class="foo">([^<]*)<span>');
content
- the content to match against.regex
- the regular expression to match, which should include one and only one regular expression group.
void time(java.lang.String name)
name
- the name of the timerlong timeEnd(java.lang.String name)
name
- the timer's name
boolean waitFor(NativeFunction callback, long timeoutInMs)
test.waitFor(function() { // Determine if the waitFor condition is satisfied and return a boolean value return isConditionSatisfied; }, 3000);
callback
- the javascript function, in the form of function() { ... }timeoutInMs
- the total time the function should wait before giving up and returning false.
void assertTrue(boolean assertion, java.lang.String message)
test.assertTrue(webElement != null, "Element not found");
assertion
- the conditionmessage
- the script error message to be logged when the condition is false.void assertTrue(boolean assertion)
test.assertTrue(webElement != null);
assertion
- the conditionvoid assertFalse(boolean assertion, java.lang.String message)
test.assertFalse(webElement != null, "Element found when it should be removed");
assertion
- the conditionmessage
- the script error message to be logged when the condition is false.void assertFalse(boolean assertion)
test.assertFalse(webElement != null);
assertion
- the conditionvoid fail()
test.fail();
Similar to:
throw "script failed!";
void fail(java.lang.String message)
message
- the script error message to be logged.
test.fail("cache is not cleared!");
Similar to:
throw "script failed! cache is not cleared!";
void assertEquals(java.lang.Object expected, java.lang.Object actual)
test.assertEquals("Swiss Cheese", webElement.getText());The order of the expected and actual values matter for the error message:
test.assertEquals("hello1", "hello2");Produces the error message:
assertEquals() failed! Expected 'hello1' but was 'hello2'.
expected
- the expected valueactual
- the actual valuevoid assertEquals(java.lang.Object expected, java.lang.Object actual, java.lang.String message)
test.assertEquals("Swiss Cheese", webElement.getText(), "They're not equal!");
expected
- the expected valueactual
- the actual valuemessage
- the script error message to be logged.void assertNotEquals(java.lang.Object expected, java.lang.Object actual)
expected
- the expected valueactual
- the actual valuevoid assertNotEquals(java.lang.Object expected, java.lang.Object actual, java.lang.String message)
expected
- the expected valueactual
- the actual valuemessage
- the script error message to be logged.void assertThrown(NativeFunction function)
test.assertThrown(function() { throw "foo bar baz" });
function
- the js functionvoid assertThrown(java.lang.String exceptionMessage, NativeFunction function)
test.assertThrown(function() { throw "foo bar baz" }, "bar");
exceptionMessage
- the message to check in the exceptionfunction
- the js functionLoadTestSettings loadTestSettings()
void globalPause(long minTimeInMS, long maxTimeInMS)
minTimeInMS
- minimum pause duration in msmaxTimeInMS
- maximum pause duration in msvoid globalPause(long pauseTimeInMs)
pauseTimeInMs
- duration in msjava.lang.String getVersion()
void forEach(java.lang.Object o, NativeFunction function)
test.forEach([1, 2, 3], function(element) { // do something with each element });
test.forEach({hello: 'world', foo: 'bar'}, function(key, value) { // do something with each key/value });
o
- the list/mapfunction
- the functionBrowserchromeOptions chromeOptions()
BrowserfirefoxOptions firefoxOptions()
|
© 2023 Vercara, LLC. All Rights Reserved. | |||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |