How to import custom libraries in Java projects


#1

Doing everything by yourself, although very appealing to an engineer, might not always be the best use of your precious time. From time to time there might be the case to leverage an existing library or dependency to get your job done. The Ranorex Webtestit Java scaffold makes use of Maven. Therefore, adding new dependencies such as custom libraries is just a matter of installing these via Maven and recompiling your project.

Install Log4j

As an example let us take a look at how to install Log4j, a popular logging library. In order to install log4j you would have to do the following steps:

  • Create a new project with Ranorex Webtestit
  • Once created, open the generated file pom.xml and add the following dependencies inside the dependencies section
         <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.1</version>
        </dependency>
  • Save pom.xml file --> Ranorex Webtestit will automatically download the additional dependencies
  • Create a testfile named DemoTest.java
  • Use the Logger as in the following example
//RxSeTestFile

package uitest.tests;

import uitest.TestNgTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

class DemoTest extends TestNgTestBase {

  private static final Logger LOGGER = LogManager.getLogger("ABC");
  @Test
  public void SampleTestCase() {
    LOGGER.error("This is a demo message from Log4j");
  }
}
  • Create an endpoint and execute the test

Now inside the log panel, you should see the default console output of Log4j.