DOM
SWT
Integration
Logging
IDE
Chromium features
Configuring Selenium WebDriver
This tutorial shows how to create a simple Selenium WebDriver application that is configured to access a web page loaded in a desktop Java application using JxBrowser.
This tutorial is only applicable for Windows OS.
Prerequisites
To go through this tutorial we will need:
- Git.
- Java 8 or higher.
- A valid JxBrowser license. It can be either Evaluation or Commercial. For more information on licensing please see the licensing guide.
- Downloaded ChromeDriver for Selenium. Since JxBrowser is based on the Chromium engine we cannot use another web driver (e.g. Firefox WebDriver). In this tutorial ChromeDriver 88.0.4324.96 was used.
Getting the code
To see complete applications created in this tutorial, please check out our collection of examples:
$ git clone https://github.com/TeamDev-IP/JxBrowser-Examples
$ cd JxBrowser-Examples/tutorials/selenium
JxBrowser application
Creating application
Create a simple JxBrowser application.
Adding the license
To move forward, put the license key into the application.
Configuring the Chromium Engine
When Selenium launches our application, ChromeDriver passes the --remote-debugging-port=<port>
argument to the command line arguments.
In our application we first obtain the --remote-debugging-port
argument from the command line arguments:
private static Optional<Integer> remoteDebuggingPortFromCommandLine(String[] args) {
if (args.length > 0) {
for (String arg : args) {
if (arg.startsWith(REMOTE_DEBUGGING_PORT_ARG)) {
String port = arg.substring(REMOTE_DEBUGGING_PORT_ARG.length());
return Optional.of(Integer.parseInt(port));
}
}
}
return Optional.empty();
}
Then forward it to the Chromium engine launched by JxBrowser:
// Create a builder for EngineOptions.
EngineOptions.Builder builder = EngineOptions.newBuilder(HARDWARE_ACCELERATED);
// Configure Engine with the remote debugging port obtained from the command line args.
remoteDebuggingPortFromCommandLine(args).ifPresent(builder::remoteDebuggingPort);
Now we can create a Browser
instance and load a web page that Selenium WebDriver will detect and work with it through ChromeDriver.
import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.view.swing.BrowserView;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Optional;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
* This example demonstrates how to create a simple Swing application with a web page loaded in
* BrowserView, and connect JxBrowser's Chromium engine with Selenium via the remote debugging port
* obtained from the command line.
*/
public final class TargetApp {
private static final String REMOTE_DEBUGGING_PORT_ARG = "--remote-debugging-port=";
public static void main(String[] args) {
// Set your JxBrowser license key.
System.setProperty("jxbrowser.license.key", "your_license_key");
// Create a builder for EngineOptions.
EngineOptions.Builder builder = EngineOptions.newBuilder(HARDWARE_ACCELERATED);
// Configure Engine with the remote debugging port obtained from the command line args.
remoteDebuggingPortFromCommandLine(args).ifPresent(builder::remoteDebuggingPort);
// Creating Chromium engine.
Engine engine = Engine.newInstance(builder.build());
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
// Creating Swing component for rendering web content
// loaded in the given Browser instance.
BrowserView view = BrowserView.newInstance(browser);
// Creating and displaying Swing app frame.
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
engine.close();
}
});
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(800, 700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.navigation().loadUrl("https://google.com");
});
}
private static Optional<Integer> remoteDebuggingPortFromCommandLine(String[] args) {
if (args.length > 0) {
for (String arg : args) {
if (arg.startsWith(REMOTE_DEBUGGING_PORT_ARG)) {
String port = arg.substring(REMOTE_DEBUGGING_PORT_ARG.length());
return Optional.of(Integer.parseInt(port));
}
}
}
return Optional.empty();
}
}
Check out the complete application.
Creating the executable file
To create an executable file of our program, use the launch4j Gradle plugin or create it manually in any way convenient for you.
Selenium application
Installing Selenium dependencies
Create a separate project and install the dependencies for running Selenium tests with ChromeDriver.
Configuring Selenium WebDriver
To tell Selenium where to find the application, provide an absolute/relative path to an executable file of the application as shown below:
ChromeOptions options = new ChromeOptions();
// Set a path to your JxBrowser application executable.
options.setBinary(
new File("tutorials/selenium/target-app/build/executable/TargetApp.exe"));
Then specify the remote debugging port for communicating with the JxBrowser-based application:
// Set a port to communicate on.
options.addArguments("--remote-debugging-port=9222");
The port should not be used by other apps. In this tutorial we use 9222, but feel free to use any other available port.
The complete application to run:
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
/**
* An application that configures Selenium WebDriver (ChromeDriver) to run on the JxBrowser-based
* application binaries and get access to HTML content loaded in JxBrowser.
*/
public final class SeleniumLauncher {
public static void main(String[] args) {
// Set a path to the ChromeDriver executable.
System.setProperty("webdriver.chrome.driver",
"tutorials/selenium/launcher/src/main/resources/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
// Set a path to your JxBrowser application executable.
options.setBinary(
new File("tutorials/selenium/target-app/build/executable/TargetApp.exe"));
// Set a port to communicate on.
options.addArguments("--remote-debugging-port=9222");
WebDriver driver = new ChromeDriver(options);
// Now you can use WebDriver.
System.out.printf("Current URL: %s\n", driver.getCurrentUrl());
driver.quit();
}
}
Check out the complete application.
Running Selenium
Run Selenium and if everything configured properly, you will see a Java window with the Google web page in it:
In the console you should see the following output:
Current URL: https://www.google.com/
It means that Selenium WebDriver managed to successfully run our application, set up a connection with the JxBrowser’s Chromium engine, and access the loaded web page to print its URL.
When starting Selenium for the first time, the JxBrowser-based application itself might launch successfully,
but you might see the following ChromeDriver error message: The process started from chrome location path\to\application.exe
is no longer running, so ChromeDriver is assuming that Chrome has crashed. It indicates that ChromeDriver
did not manage to find the running application. We do not yet know how to fix this, but if you do not close the
launched application and run Selenium again, the driver will detect the running application binaries
and start successfully.
Summary
In this tutorial we:
- Create two applications: the application where we integrate JxBrowser to display a web page, and the application with Selenium ChromeDriver that connects to the first application and accesses the web page loaded in JxBrowser.
- Show how to make the application with JxBrowser “visible” to Selenium.
- Run the Selenium application on the JxBrowser-based application binaries to demonstrate how it works.