Embracing Open Source: Maven and Silk4J

Micro Focus Expert
Micro Focus Expert
0 0 3,534

In addition to the great functionality Silk4J  provides when used on its own, you can leverage your automated testing by using Silk4J in combination with many highly useful open source solutions. With the "Embracing Open Source" series of blog posts, we will show you some examples of how you can use open source projects to enhance the execution-, reporting-, and management-capabilities of your existing Silk4J tests.

In this blog entry, we will focus on the great features that Apache Maven can provide, for example the supreme integration of Maven with CI tools like Jenkins. A simple workflow describes how you can use Silk4J with Maven.

About Maven

Maven is a build-management and dependency-management solution for large Java projects. It relies on a set of conventions that make development easier, one of those conventions being that specific folders should be used for code and tests. Another convention is the standardized way in which external JAR files are referenced in Maven. These conventions might make the integration of Maven and Silk4J a bit tricky, therefore we will try to give you an easy starting point in this blog entry.

Creating a Maven project

Create a simple Maven project in Eclipse, and call the project silktest.maven.demo.

When you create a Maven project, an empty project that follows the general project structure of Maven is created in Eclipse.

Attention: Maven heavily relies on this project structure, especially on any automated tests being stored in the src/test/java folder of the project.

Converting the Maven project into a Silk4J project

To convert the Maven project into a Silk4J project: perform the following actions:

  1. In Eclipse, right-click on the Maven project in the Package Explorer.
  2. Select Silk4J Tools > Make Silk4J Project.

Recording a test case with Silk4J

You can now record a simple test case, which just logs in to the Insurance sample web application, and then logs out again.

  1. Select the new Silk4J project in the Package Explorer.
  2. Click Record in the toolbar.
  3. In the Select Application dialog box, select the Web tab.
  4. Select Internet Explorer.
  5. Type http://demo.borland.com/InsuranceWebExtJS into the Enter URL to navigate field.

  6. Record the actions for logging in and out. Log in as john.smith@gmail.com, and with the password john.
  7. Click Stop in the Recording window. The Record Complete dialog box displays.
  8. Click Select and choose the src/test/java folder in your Silk4J project as the location for the new test.

  9. Click OK. The Record Complete dialog box should now look like the following:

The following code sample shows the recorded script for the test:

@org.junit.Test
public void test()
  desktop.<DomTextField> find("demo_borland_com.BrowserWindow.login-form email").click(MouseButton.LEFT, new Point(48, 12));
  desktop.<DomTextField> find("demo_borland_com.BrowserWindow.login-form email").typeKeys("john.smith@gmail.com");
  desktop.<DomTextField> find("demo_borland_com.BrowserWindow.login-form password").click(MouseButton.LEFT, new Point(42, 8));
  desktop.<DomTextField> find("demo_borland_com.BrowserWindow.login-form password").typePasswordKeys("7092G6g=");
  desktop.<DomButton> find("demo_borland_com.BrowserWindow.login-form login").click(MouseButton.LEFT, new Point(34, 10));
  desktop.<DomButton> find("demo_borland_com.BrowserWindow.logout-form logout").click(MouseButton.LEFT, new Point(30, 16));
}

Note: By default, Maven uses the following naming conventions when looking for tests to run:

  • Test*
  • *Test
  • *TestCase

 Ensure that your test class follows one of the naming conventions to enable Maven to pick the tests up later.

Creating the silk4j.settings file

 

The silk4j.settings file specifies the application under test and the Silk Test options that Silk4J uses. According to the Maven conventions, such files have to reside in the folder resources, which has to be a direct sub-folder of the test folder in your project.

 

If the folder resources does not exist in the project, use the Package Explorer in Eclipse to create the folder.

Right-click on the new folder and select Build Path > Use as source folder to specify the folder as a source folder.

To be able to replay the recorded actions, open a file editor and create the silk4j.settings file with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<recorderProject version="16.5">
  <applicationConfiguration>
    <browserType>InternetExplorer</browserType>
    <baseStateInfo>
      <locator>//BrowserApplication</locator>
      <url>http://demo.borland.com/InsuranceWebExtJS</url>
    </baseStateInfo>
  </applicationConfiguration>
  <agentOptions>
    <entry>
      <string>OPT_ASSET_NAMESPACE</string>
      <string>silktest.maven.demo</string>
    </entry>
  </agentOptions>
</recorderProject>

 Note: If you are using the object map functionality of Silk Test, make sure that the object map files are also located in the resources folder.

Adding the JTF to your local Maven repository

Besides standardizing the folder structure of Java projects, Maven also provides a convenient way of declaring dependencies to required libraries. This mechanism is explained in detail in the Maven documentation: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

To leverage this mechanism for managing the dependency to the silktest-jtf-nodeps.jar (which contains the Silk Test Java APIs), this JAR file has to be registerd in the local package manager before it can be used:

  1. In Eclipse, right-click on the project in the Package Explorer.
  2. Select Run As > Run Configurations.
  3. In the Run Configurations dialog box, create a new Maven Buildrun configuration with the following parameters:

  4. Launching this run configuration displays the Eclipse console view, which displays a message that installing the silktest-jtf-nodeps.jarinto the local maven repository was successful:

    [INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ silktest.demo.maven ---
    [INFO] Installing C:\Program Files (x86)\Silk\SilkTest\ng\JTF\silktest-jtf-nodeps.jar to C:\Users\ralphm\.m2\repository\com\borland\silktest\jtf\16.5\jtf-16.5.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------

Declaring a Maven dependency to the JTF

After the JAR file has been registered, you can declare a dependency to the JAR file in your Maven project:

  1. In the Package Explorer, right-click on the new project.
  2. Select Maven > Add Dependency.
  3. Type com.borland.silktest into the Group Id field.
  4. Type jtf into the Artifact Id field.
  5. Type 16.5 into the Version field.
  6. Click OK.

These are the same values that we have used to register the silktest-jtf-nodeps.jar.

Furthermore, ensure that JUnit 4.4 is also declared as a dependency:

  1. In the Package Explorer, expand the node of the new project.
  2. Open the file pom.xml.
  3. Select the tab Dependencies.
  4. If JUnit 4.4 is not declared as a dependency, add a dependency with junit as the Group Id , junit as the Artifact Id, and 4.4 as the Version.

The Dependencies tab should look like the following:

Note: Delete the  auto-generated Java file AppTest.java from the Package Explorer.

Now that you have a Maven dependency to Silk Test, you can remove the explicit dependency that the Silk4J project has to the Silk Test JTF JAR.

  1. Right-click on the project in the Package Explorer.
  2. Select Properties.
  3. Select Java Build Path.
  4. Select the tab Libraries.
  5. Select Silk Test JTF 16.5.0 Library.
  6. Click Remove.

At this point you have a fully-functional Silk4J Maven project!

Running Silk4J tests through Maven

To run all tests in the project from Eclipse, you can use either the existing Silk4J tooling, or Maven. To run the tests through Maven, perform the following actions:

  1. Right-click on the project in the Package Explorer.
  2. Select Run as -> Maven build…
  3.  Type test into the Goals field.

 

 

On the JRE tab of the launch configuration, select a Java Development Kit (JDK).

Since test is specified as the Maven goal, all tests in the src/test/java folder of your project are executed. To run specific tests, see Running a single test.

Running keyword-driven tests through Maven

In addition to executing Junit-based SIlk4J tests with Maven, you can also execute keyword-driven tests.

  1. Register the com.borland.silk.keyworddriven.engine.jar with Maven.

  2. Add a dependency to the artifact.

  3. Add the class KeywordTest.java to your src/test/java source folder. For details on populating the class, see Running a keyword-driven test from command line.

    package com.borland.silktest.maven.demo;

    import org.junit.runner.RunWith;

    import com.borland.silktest.jtf.keyworddriven.KeywordTestSuite;
    import com.borland.silktest.jtf.keyworddriven.KeywordTests;

    @RunWith(KeywordTestSuite.class)
    @KeywordTests({"LoginTest"})
    public class KeywordTest {

    }

  4. Ensure that your Keyword-Driven tests are stored in the src/test/resources folder of your project, as the tests will not be picked up by Maven otherwise.

     

 

Conclusion and Outlook

You should now have a fully functioning Silk4J/Maven hybrid project. But what can you do with that project?

Here's a couple of ideas:

  • Leverage thousands of open source projects available from the Maven Central repository within your test code, for example Apache CommonsGoogle Guava, and so on.
  • Use the Maven command line to build your test project and to run your tests in a standalone environment.
  • Use Jenkins to build your test project and to run your tests through the built-in Maven support.
  • Use the Allure Framework to generate beautiful HTML reports for your test runs.
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.