URL Mock APIs

Mocks are used to mock out responses to urls which match a particular pattern.

Add Mock

Forces the proxy to process certain patterns of urls differently. If class_method is not specified, sends back a simple HTML blank page.

Parameters
$patternstring URL regex pattern as a string.
$class_methodstring optional Combination of class and method names which will process this URL pattern.
Return Value

Modes Supported :
Raw Script

Sahi Pro Classic API :_addMock


Mock Image

Similar to _addMock, but by default sends back an image.

Parameters
$patternstring URL regex pattern as a string.
$class_methodstring optional Combination of class and method names which will process this URL pattern.
Return Value

Modes Supported :
Raw Script

Sahi Pro Classic API :_mockImage


Remove Mock


Parameters
$patternstring URL regex pattern as a string. Removes the mock behavior added via _addMock or _mockImage for that pattern.
Return Value

Modes Supported :
Raw Script

Sahi Pro Classic API :_removeMock


Example: A web page has embedded advertisements in an iframe and we do not wish to test these advertisements everytime we test our web page.

We can add a mock by specifying
_addMock("www[.]adserver[.]com", "sahi.ext.MyCustomMock_filterAds");
to our script.

Whenever Sahi encounters a URL request which matches www[.]adserver[.]com It will delegate the response handling to sahi.ext.MyCustomMock's filterAds method. One can call any class's any method, as long as the method has one of these two signatures.

public net.sf.sahi.response.HttpResponse methodName (net.sf.sahi.request.HttpRequest request);
public void methodName(net.sf.sahi.request.HttpRequest request);


If _addMock("pattern") is called without a second parameter, net.sf.sahi.command.MockResponder.simple(HttpRequest) is used to process the request. This response uses template at sahi/htdocs/spr/simpleMock.htm. One can modify this to show a generic mock response. By default this response just shows the mocked url.

User can create the custom class file and methods at their end by following below steps:
    • Create the sample java class and method as below.
package example;
import net.sf.sahi.response.HttpFileResponse;
import net.sf.sahi.response.HttpModifiedResponse2;
import net.sf.sahi.response.HttpResponse;

public class AddMockTest {
	public net.sf.sahi.response.HttpResponse sendCustomResponse(net.sf.sahi.request.HttpRequest request){

	/*Sample code for loading custom html file*/

	HttpResponse mockResponse
		= new HttpFileResponse("C:/SahiInstallationPath/htdocs/spr/sampleMockPage.htm", null, false, false);
	HttpResponse response
		= new HttpModifiedResponse2(mockResponse, request.isSSL(), request.fileExtension(), request.session().getInjectHeaders());

	return response;

	}
}

/*Modify the code inside the method accordingly as per your requirement*/
    • Add thesahi.jar file from Sahi\lib\ to the current project and add to build path.
    • Export the above *.java file as executable JAR file and place it in Sahi\userdata\extlib\folder. Create the extlib folder inside the userdata if not present.
    • Restart Sahi and call the class_method in the script as below.
_navigateTo("http://sahitest.com/demo/index.htm",true);
_addMock(".*sahitest[.]com.*","example.AddMockTest_sendCustomResponse");
//package.className_methodName
_navigateTo("http://sahitest.com/demo/index.htm",true);
_removeMock(".*sahitest[.]com.*");
_navigateTo("http://sahitest.com/demo/index.htm",true);


For images, one can use _mockImage, which is the same as _addMock except that it by default calls net.sf.sahi.command.MockResponder.mockImage(HttpRequest) which returns a simple image at sahi/htdocs/spr/mock.gif

This script may explain mocks better:

_navigateTo("http://sahitest.com/demo/index.htm");
// assert a particular link exists on that page
_assertNotNull(_link("Link Test"));
// --
// add mock
_addMock(".*sahitest[.]com.*");
// --
_navigateTo("http://sahitest.com/demo/index.htm", true);
// link should not exist
_assertNull(_link("Link Test"));
// Instead the page contains the mocked url
_assert(_containsText(document.body, "http://sahitest.com/demo/index.htm"));
// --
// remove mock
_removeMock(".*sahitest[.]com.*");
// --
_navigateTo("http://sahitest.com/demo/index.htm", true);
// Mock removed. The link is visible again on the page.
_assertNotNull(_link("Link Test"));