How to use Java Selenium in Combination with UiPath

Based on my approach about the using of JShell (Java REPL) here, an integration scenario of Java source code into UiPath, now an use case to integrate Java Selenium regression automation tests. Selenium is a portable framework for testing web applications. In business exists many sources which uses Selenium client and web driver language bindings in Java language. With this approach the existing automations can be used furthermore.

Preparation

Besides the OpenJDK I built two other packages.

  • The first package contains the Chrome web browser and the related web driver.
  • The second package contains the Selenium Java libraries.

I add both packages to my project.

image

Side View

The package concept of UiPath is incredible. It is not only possible to distribute the libraries belonging to a workflow, but applications can also be distributed in the context of a workflow on this way. Referring to this example, different versions of OpenJDK or Chrome could also be distributed on this way and the test automation can be performed with these different versions, with one workflow. This does not replace software distribution, but it makes it much easier to deal with version diversity in the context of test automation.
Have I already said that the package concept of UiPath is incredible? :wink:
Almost a unique selling point.

Environment Variables

With a Invoke Code activity I set a few environment variables. These environment variables are necessary for the execution of the Java source code.

  • JAVA_HOME = Path to the OpenJDK
  • CHROME_HOME = Path to the Chrome browser and Chrome web driver
  • CLASSPATH = Path to the Java Selenium libraries

//-UiPath Begin---------------------------------------------------------

try {

  string UserProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

  //-Set JAVA_HOME----------------------------------------------------
  string JDKPath = UserProfile + "\\.nuget\\packages\\openjdk\\16.0.1";
  Environment.SetEnvironmentVariable("JAVA_HOME", JDKPath);

  //-Set CHROME_HOME-----------------------------------------------
  string ChromePath = UserProfile + "\\.nuget\\packages\\chrome\\90.0.4430.93";
  Environment.SetEnvironmentVariable("CHROME_HOME", ChromePath);
	
  //-Set Java CLASSPATH-----------------------------------------------
  string ClassPath = UserProfile + "\\.nuget\\packages\\selenium\\3.141.59\\*";
  Environment.SetEnvironmentVariable("CLASSPATH", ClassPath);

} catch (Exception e) {

}

//-UiPath End-----------------------------------------------------------

Java Code

In the Java source I get the CHROME_HOME path, set a property and options and start the Chrome browser via the Chrome driver. I open a local file, wait a few seconds and close all.

//-Begin----------------------------------------------------------------

String FilePath = System.getProperty("FilePath");

//-Import---------------------------------------------------------------
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;

//-Sub Main-------------------------------------------------------------
void Main() {
  WebDriver driver = null;
  try {
    String CHROME_HOME = System.getenv("CHROME_HOME");
    System.setProperty("webdriver.chrome.driver", CHROME_HOME + "/chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.setBinary(CHROME_HOME + "/Chrome/chrome.exe");
    driver = new ChromeDriver(options);
    driver.get("file://" + FilePath + "/Documents/UiPath/JShell/SeleniumExample01.html");
    Thread.sleep(2500);
  } catch (Exception e) {
    System.err.println(e.toString());
    return;
  } finally {
    if(driver != null) {
      driver.quit();
	}
  }
}

//-Main-----------------------------------------------------------------
Main();
/exit

//-End------------------------------------------------------------------

Workflow

That is all, now a look at the workflow.

image

Conclusion

This example shows how Java source code can be seamlessly embedded into UiPath via JShell. Even complex dependencies to applications and libraries can be solved via package management. Selenium is a very common test framework, this example shows a good approach to use existing test cases with UiPath, so that duplication of effort can be avoided. Certainly, adjustments will be necessary, but we see here that it is basically possible.

2 Likes