Simulating Data for Selected Endpoints through the API

Use the API to simulate data that is transmitted through selected endpoints in a web application.

Before you can simulate data that is transmitted through an endpoint, learn which data is transmitted through the endpoint. For additional information, see Learning which Data is Transmitted Through an Endpoint.

Note: With Silk4J, you can simulate up to three endpoints at the same time. If you require additional endpoints, you have to use an SV Lab license. You can specify a license server for SV Lab by setting the SVLicenseServerUrl property in the svlab.properties file. The file is located in the Silk Test application data directory, under %APPDATA%/Silk/SilkTest/conf/.

To add service virtualization functionality to an existing Silk4J script:

  1. Add the SV Lab Client Library to the project that contains the Silk4J script.
    1. In the Package Explorer, right-click on the project node.
    2. Select Silk4J Tools > Add Service Virtualization Capability.
  2. Change the data for the endpoint in the JavaScript file of the endpoint to the new data that you want to simulate. Silk4J saves new scenarios in the Scenarios subfolder of the project. The JavaScript file has the suffix jsonModel and is located in a subfolder of the Scenarios folder with the name of the scenario.
  3. Open the Silk4J script.
  4. Change the desktop to static.
    private static Desktop desktop = new Desktop();
  5. Add a SvClient member variable.
    private static SvClient sv;
  6. Start the SV Lab and use the simulated data.
    1. Add the following imports to the start of the script.
      import org.junit.BeforeClass;
      import com.microfocus.silktest.jtf.sv.SvLabIntegration;
      import org.microfocus.sv.api.SvClient;
      import org.microfocus.sv.model.project.Module;
    2. Add the following code before the test to simulate the endpoint data.
      @BeforeClass
      public static void startSVLabSimulation() {
        String scenarioName = "PopularProducts";
        SvLabIntegration svLabIntegration = desktop.getSvLabIntegration();
        String endpoint = svLabIntegration.getServerEndpoint();
        sv = SvClient.newInstance(endpoint);
        Module module = sv.compileModuleFromSources("classpath:/" + scenarioName + "/*");
        sv.loadActiveVirtualLab("classpath:/" + scenarioName + "/sv-lab.json", module, true);
        sv.startActiveVirtualLab();
        sv.runSimulation(scenarioName);
      }
      Note: The scenarioName is the name of a scenario in the current project.
  7. Ensure that the proxy is specified in the base state.
  8. Ensure that the browser has not cached any data about the AUT.
  9. Run the test script.
  10. Stop the SV Lab.
    1. Add the following imports to the start of the script.
      import org.junit.AfterClass;
    2. Add the following code after the test.
      @AfterClass
      public static void stopSVLab() {
        if (sv != null) {
          sv.close();
        }
      }
  11. Replay the test. For information about replaying tests from the command line, see Replaying a Test from the Command Line.

Example: Using simulated data in a test

The entire script that simulates the data for the endpoint should look similar to the following.
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.microfocus.sv.api.SvClient;
import org.microfocus.sv.model.project.Module;

import com.borland.silktest.jtf.BrowserBaseState;
import com.borland.silktest.jtf.Desktop;
import com.borland.silktest.jtf.xbrowser.DomElement;
import com.microfocus.silktest.jtf.sv.SvLabIntegration;

public class MyTest {

  private static Desktop desktop = new Desktop();
  private static SvClient sv;

  @BeforeClass
  public static void startSVLabSimulation() {
    String scenarioName = "PopularProducts";
    SvLabIntegration svLabIntegration = desktop.getSvLabIntegration();
    String endpoint = svLabIntegration.getServerEndpoint();
    sv = SvClient.newInstance(endpoint);
    Module module = sv.compileModuleFromSources("classpath:/" + scenarioName + "/*");
    sv.loadActiveVirtualLab("classpath:/" + scenarioName + "/sv-lab.json", module, true);
    sv.startActiveVirtualLab();
    sv.runSimulation(scenarioName);
  }

  @Before
  public void baseState() {
    // Go to web page 'http://advantageonlineshopping.com'
    BrowserBaseState baseState = new BrowserBaseState();
    baseState.execute(desktop);
  }

  @Test
  public void testSimulatedProduct() {
    DomElement firstPopularItem = desktop.<DomElement> find("//BrowserApplication//BrowserWindow//P[@name='popular_item_16_name']");
    Assert.assertEquals("SIMULATED: HP ELITEPAD 1000 G2 TABLET", firstPopularItem.getText());
  }

  @AfterClass
  public static void stopSVLab() {
    if (sv != null) {
      sv.close();
    }
  }

}