Overview of the C# Web APIC# API

The eggPlant Performance Web C# 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 C# 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.

namespace com.testplant.testing
{
    public class MyNewCSWebScript : Facilita.Web.WebBrowserScript
    {
        public override void Pre()
        {
            //do not remove following line 
            base.Pre();

            // put any code that needs to execute at the start of the test here
        }

        public override void Script()
        {
            //TODO Place your script variables here. 

            //TODO 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 C# 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 = WebBrowser.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 SubResponses property.

Using HTML forms

Usually, a Form object is obtained by extracting it from a Response, using the Response ExtractForm(Int32) 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 = WebBrowser.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 = WebBrowser.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 C# Virtual User type by performing the following actions in eggPlant Performance Studio:

  • Under Workspace/Virtual Users, right-click Web C# 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.