URL Mock Functions
Mocks are used to mock out responses to urls which match a particular pattern.
_addMock
Section titled “_addMock”- Since
- Sahi Pro 3.5
Modes Browser
_addMock($pattern[, $class_method])Arguments
| Name | Type | Description |
|---|---|---|
$pattern | string | URL regex pattern as a string. |
$class_method | stringoptional | Combination of class and method names which will process this URL pattern. |
Returns
null |
Forces the proxy to process certain patterns of urls differently. If class_method is not specified, sends back a simple HTML blank page.
_mockImage
Section titled “_mockImage”- Since
- Sahi Pro 3.5
Modes Browser
_mockImage($pattern[, $class_method])Arguments
| Name | Type | Description |
|---|---|---|
$pattern | string | URL regex pattern as a string. |
$class_method | stringoptional | Combination of class and method names which will process this URL pattern. |
Returns
null |
Similar to _addMock, but by default sends back an image.
_removeMock
Section titled “_removeMock”- Since
- Sahi Pro 3.5
Modes Browser
_removeMock($pattern)Arguments
| Name | Type | Description |
|---|---|---|
$pattern | string | URL regex pattern as a string. Removes the mock behavior added via _addMock or _mockImage for that pattern. |
Returns
null |
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 the
sahi.jarfile fromSahi\lib\to thecurrent projectand add tobuild path. - Export the above *.java file as executable
JARfile and place it inSahi\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.
- Add the
_navigateTo("https://sahitest.com/demo/index.htm",true);_addMock(".*sahitest[.]com.*","example.AddMockTest_sendCustomResponse");//package.className_methodName_navigateTo("https://sahitest.com/demo/index.htm",true);_removeMock(".*sahitest[.]com.*");_navigateTo("https://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("https://sahitest.com/demo/index.htm");// assert a particular link exists on that page_assertNotNull(_link("Link Test"));// --// add mock_addMock(".*sahitest[.]com.*");// --_navigateTo("https://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, "https://sahitest.com/demo/index.htm"));// --// remove mock_removeMock(".*sahitest[.]com.*");// --_navigateTo("https://sahitest.com/demo/index.htm", true);// Mock removed. The link is visible again on the page._assertNotNull(_link("Link Test"));
