Executing Selenium Scripts

You can execute Selenium WebDriver scripts with Silk4J to use synchronization and to create a detailed TrueLog during test execution. If a browser type is specified in the capabilities on a machine on which the Open Agent is running, and you create a RemoteWebDriver and connect it to the Selenium server which is running on the Open Agent, Silk4J automatically launches the corresponding browser. If the browser type is not specified, the Open Agent tries to reuse an existing browser instance that has been launched in advance for example by executing a Silk4J base state.

To generate a visual execution TrueLog file during the execution of a Selenium script, you can use the Silk Test TrueLog API for Selenium WebDriver. This API is implemented as a REST interface, and the endpoint host and port used by this API are identical to the ones used by the Selenium server, for example http://localhost:4444/silktest/truelog.

The REST API files are located in the Silk Test installation folder under \ng\TrueLogAPI\:
  • The file SilkTestTrueLogService-doc.html contains the REST API documentation.
  • The file SilkTestTrueLogService.yaml contains the REST API declaration.
  • The file trueLogApiClient.jar contains a Java client for the REST API.

By importing the REST API declaration file into the Swagger Editor and then using the Generate Client functionality of the editor, without making any changes to the declaration file, you can generate the client API library for a vast amount of languages like Python, Ruby, JavaScript, C#, and others. You can then download the TrueLog API in the selected programming language.

You can execute Selenium scripts with Silk4J against the following browsers:
  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Apple Safari
  1. Start the Open Agent on the machine on which Silk4J is installed.
  2. Start the browser against which you want to test your application.
    • For Google Chrome, Microsoft Edge, and Mozilla Firefox, you can use the default capabilities from WebDriver to start the browser. For example, you can start Google Chrome by using the java bindings as shown in the following code:
      RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.chrome());
    • For Apple Safari, you have to specify a custom browser name as a capability, as shown in the following code:
      DesiredCapabilities safari = new DesiredCapabilities();
       safari.setCapability("browserName", "SilkSafari");
  3. Optional: Pass additional options as capabilities. The custom Silk4J options are passed as a map and have the capability name silkTestOptions. For example, the following code sample shows how you can activate the automatic synchronization for Selenium, by setting the syncEnabled option:
    Map<String, Object> options = new HashMap<>();
    options.put( "syncEnabled" , true);
    capabilities.setCapability( "silkTestOptions" , options);
    The following options are allowed:
    Option Type Description
    commandLineArguments String Passes command line arguments to the browser that is launched.
    connectionString String Specifies a connection string to a browser running on a remote machine.
    startUrl String The URL that the browser navigates to when the browser is launched.
    syncEnabled boolean Turns the Silk Test AJAX synchronization on or off. The default value is false.
    trueLogEnabled boolean You can use this option to enable or disable Truelog. The default value is true.
    trueLogId String The identifier of the TrueLog session that is returned when calling the StartTrueLog method of the TrueLog API. This identifier is required if you want to specify the TrueLog that should include the WebDriver actions for the Open Agent.
    trueLogPath String The custom Truelog file path. By default, Truelogs are written to the Silk Test log directory under %LOCALAPPDATA%\Silk\SilkTest\logs.
    trueLogScreenshotMode String Specifies when screenshots are added to Truelogs.
    OnError
    A screenshot is added to the Truelog when an error occurs.
    always
    A screenshot is added to the Truelog for each action.
  4. Optional: To specify the port that is used by the Selenium server, perform the following:
    1. Navigate to %APPDATA%\Silk\SilkTest\conf.
    2. Create a new properties file with the name selenium.properties.
    3. Type selenium.server.port=<port name> into the new file.
The following code sample turns on synchronization and starts Google Chrome with the Selenium Java bindings:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Map<String, Object> options = new HashMap<>();
options.put("syncEnabled", true);
capabilities.setCapability("silkTestOptions", options);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
The following code sample specifies the TrueLog file for the Open Agent and starts Google Chrome with the Selenium Java bindings:
TrueLogAPI truelogAPI = new TrueLogAPI();
truelogAPI.startTrueLog("C:/temp/myTrueLogFile.tlz");
String truelogId = truelogAPI.getTrueLogId();

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Map<String, Object> options = new HashMap<>();
options.put("trueLogId", trueLogId);
capabilities.setCapability("silkTestOptions", options);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
You can now use the RemoteWebDriver object in the same way as when you are using standalone Selenium. For example:
driver.get("http://demo.borland.com/InsuranceWebExtJS/");
driver.findElementById("login-form:login");
When you execute the script, all Selenium and Silk4J actions, along with any screenshots and parameters, are logged to the TrueLog.