Modifying the Base State in a Script

You can edit the base state in a script to specify how Silk4J starts an application under test (AUT) during replay. You can specify the executable location of the AUT, the working directory, the URL and the connection string for a web application, and so on. For example, if you want to execute tests on a production web site, which have already been executed on a staging web site, you can simply change the URL in the base state and the tests are executed against the new web site.

Note: To specify how Silk4J starts an application under test (AUT) during recording and replay, edit the base state from the user interface. For additional information, see Modifying the Base State from the User Interface.

To edit the base state in a script:

  1. Open the script.
  2. Change the baseState method. You can add your code between creating the base state and executing it:
    // Go to web page 'demo.borland.com/InsuranceWebExtJS'
    BrowserBaseState baseState = new BrowserBaseState();
    // <-- Insert your changes here!
    baseState.execute();
  3. Use the following code to specify the executable name and file path of the application that you want to test:
    baseState.setExecutable(executable);
    For example, to specify the Calculator, type the following:
    baseState.setExecutable("C:\\Windows\\SysWOW64\\calc.exe");
    To specify Mozilla Firefox, type the following:
    baseState.setExecutable("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
  4. Use the following code to specify command line arguments:
    baseState.setCommandLineArguments(commandLineArguments);
    For example, to start Mozilla Firefox with the profile myProfile, type the following:
    baseState.setCommandLineArguments("-p myProfile");
  5. You can use the following code to specify a different working directory:
    baseState.setWorkingDirectory(workingDirectory);
  6. If you want to use an executable pattern, use the following code:
    baseState.setExecutablePattern(executablePattern);
    For example, if you want to specify an executable pattern for the Calculator, type:
    baseState.setExecutablePattern("*\\calc.exe");
  7. If you want to use a command line pattern in combination with the executable file, use the following code:
    baseState.setCommandLinePattern(commandLinePattern);
    Using the command line is especially useful for Java applications because most Java programs run by using javaw.exe. This means that when you create an application configuration for a typical Java application, the executable pattern, *\javaw.exe is used, which matches any Java process. Use the command line pattern in such cases to ensure that only the application that you want is enabled for testing. For example, if the command line of the application ends with com.example.MyMainClass you might want to use *com.example.MyMainClass as the command line pattern:
    baseState.setCommandLinePattern("*com.example.MyMainClass");
  8. If you are testing a web application or a mobile web application, and you have not set an application configuration for the current project, specify one of the installed browsers or mobile browsers. For example, to specify Google Chrome, type the following:
    baseState.setBrowserType(BrowserType.GoogleChrome);
  9. If you are testing a web application or a mobile web application, and you have not set an application configuration for the current project, specify the address of the web application that you want to test:
    baseState.setUrl(url);
    For example, type the following:
    baseState.setUrl("demo.borland.com/InsuranceWebExtJS/");
  10. If you want to test a web application on a desktop browser, you can specify the height and width of the browser window:
    baseState.setViewportHeight(viewportHeight);
    baseState.setViewportWidth(viewportWidth);
  11. If you want to test a web application or a mobile native application on a remote location, specify the connection string:
    new MobileBaseState(connectionString);
    For information on the connection string, see Connection String for a Remote Desktop Browser or Connection String for a Mobile Device.
  12. To edit the capabilities for Mozilla Firefox or Google Chrome , you can also use the connection string. For example, to set the download folder for Mozilla Firefox, type the following:
    baseState.setConnectionString(
      "moz:firefoxOptions="
      + "{"
      + " \"prefs\": {"
      + "        \"browser.download.dir\":\"C:\\\\Download\\\\\""
      + "     }"
      + "};");
    For additional information, see Setting Options and Capabilities for Web-Driver Based Browsers.