Package com.facilita.fc.web

The eggPlant Performance Web Java API makes it easy to perform load tests against web sites and services, by providing classes and methods that simulate the behaviour of a web browser sending/receiving HTTP(s) requests to/from a web server. The API also provides mechanisms for:
  • handling cookies
  • HTTP caching and authentication
  • HTML parsing
  • HTML form handling

Creating scripts

When eggPlant Performance runs a test containing Web Java Virtual Users, a WebBrowserVirtualUser object is created by the eggPlant Performance engine for every Virtual User in the test. These WebBrowserVirtualUser objects runs scripts/workflows by creating instances of script classes (which are subclasses of the WebBrowserScript class) and calling the script() method on them.

Create scripts through eggPlant Performance Studio by right-clicking the Scripts folder in a workspace, and either recording a web trace or creating a blank script. A template is used to fill in the basic structure of the script:

e.g.

 
 package com.testplant.testing;

 public class MyNewWebJavaScript extends com.facilita.fc.web.WebBrowserScript
 {
     public void pre() throws Exception
     {
         super.pre();
         // Put any code that needs to execute at the start of the test here

     }

     public void script() throws Exception
     {
         // Place your iterated script code here.

     }
 }
 
 
The name you give your script in eggPlant Performance Studio will be used as the name of the class in the script file, and the class will be a subclass of WebBrowserScript.

In eggPlant Performance Studio you can define a test which includes a Virtual User Group running your Web Java Virtual User script (or a workflow containing your script). When the test is run in Test Controller, then code written in the script() method will execute. The script() method will be called a number of times, relating to the number of iterations that the VU Group is set to perform. This may be a fixed number per test, or a VU Group can be set to run continuously for a certain period of time.

Sending web requests

A Request object is created by calling WebBrowser.createRequest(HttpMethod, Url). The Request object can be modified by adding HTTP request headers, or setting POST data. The request can then be sent to the web server using the Request.send() or Request.sendTopLevel() methods.

e.g.

 
 Request request = getWebBrowser().createRequest(HttpMethod.GET, new Url("http://www.testplant.com/"), 1);
 Response response = request.send();
 
 

Handling HTTP responses

The return value of Request.send() is a Response object. The Response object contains information about the response from the web server, such as the HTTP status code, the HTTP response headers, and the actual content of the response. Methods such as Response.verifyContains(String), Response.verifyTitle(String) and Response.extract(ExtractionCursor, String, String) can be used to check that the response from the web server is correct.

The response may also contain sub-responses, such as images, CSS files, or javascript. These can be accessed using the Response.getSubResponses() method.

Using HTML forms

Usually, a Form object is obtained by extracting it from a Response, using the Response.extractForm(int) method.

Each element in the form is represented by an HtmlElement object. You can access the elements in the form by using the various "get" methods (e.g. Form.getHtmlElement(String), Form.getInputElement(String, String)). The HtmlElement objects can be modified to change the data that will be sent back to the server. More elements can be added to the form using Form.addElement(HtmlElement), and removed using Form.removeElement(String, String).

e.g.

 
 Response response = request.send();
 Form form = response.extractForm(0);
 
 
The form is then usually modified by changing the HtmlElement objects it contains, and sent back to the server on a later request. This could be either as query data on a GET request:
 
 // Create a Url with the form data as a query string
 Url url1 = new Url("http://localhost/");
 url1 = url1.withQuery(form);

 // Create a GET request based on the url and send it to the web server.
 Request request1 = getWebBrowser().createRequest(HttpMethod.GET, url1, 1);

 // Send the request to the web server
 Response response1 = request1.send();
 
 
Or, as the message body on a POST request:
 
 // Create a Url
 Url url2 = new Url("http://localhost/");

 // Create a POST request and set the form data as the message body
 Request request2 = getWebBrowser().createRequest(HttpMethod.POST, url2, 2);
 request2.setMessageBody(form);

 // Send the the request to the web server
 Response response2 = request2.send();
 
 

Customising Virtual Users

When developing Web scripts, it is recommended to customise the Web Java Virtual User type by performing the following actions in eggPlant Performance Studio:
  • Under Workspace/Virtual Users, right-click Web Java Virtual User, and then click New VU Type
  • Type a name (which will be used as the class name) and a description
  • Expand the newly-created custom Virtual User type, and the Source folder
  • Edit the VU source file (the class which extends WebBrowserVirtualUser) by adding methods that can be called from any script which is based on your custom Virtual User, and/or overriding the methods in WebBrowserVirtualUser.
The other source file created for the custom Virtual User type is a VU Script source file, which is a class that extends WebBrowserScript and will be the base class of any script files you create with the custom Virtual User type.