0

I have a Windows based application (.exe based) which upon launch comes up with Win form for login. Upon login, it loads the application and also loads the JxBrowser libs which launches an embedded chromium browser (I see in my task manager that Chromium.exe processes come up at this time). I plan to automate this embedded browser using selenium. So, I read about --remote-debugging-port option, so I could access it in Chrome using selenium using localhost:<remote-debugging-port> on which my application is launched.

I tried the following things to set the remote-debugging-port to my Windows application.

Option 1

In my Windows application exe path, I start it up with --remote-debugging-port=9222. When I launch the application, and then it comes up with embedded browsers, I then open a chrome window and try navigating to localhost:9222, and I get a Connection Refused.

I understand that somehow, within the application, when it launches the Chromium.exe processes, I need to send the remote-debugging-port in that Chromium.exe file.

Option 2

So, I create a shortcut for Chromium.exe, and add the --remote-debugging-port in the Target option of the shortcut. Then I again launch my Windows based application (this time without remote debugging port at Win application level). However, once the application is fully loaded, and the embedded JxBrowser based Chromium windows come up (and task manager launches the Chromium.exe) process, when I launch Chrome window and navigate to localhost:9222, again I get Connection Refused.

So, from the task manager, I then right-click on Chromium.exe process, and click on "Open File Location". It takes me to the Chromium.exe path. However, I had set the remote-debugging-port on the shortcut I created out of this Chromium.exe path, so I am not sure the application takes that (whether it needs to honor the remote-debugging-port from the shortcut of that actual exe path) into effect when it launches

After trying multiple different things, I am really stuck as to how I can make my Windows application chromium based process start up automatically with remote-debugging on so I can perform the automation activities using Selenium Chromedriver using localhost:9222

Since I am unable to inspect anything on the JxBrowser based Chromium embedded browser in my Windows application, I really need to have this working.

I went through the following article Automation for "Chrome Legacy Window" (Chromium) using Winium - however, this also does not give clarity as to how I can start my application with remote-debugging-port for the chromium process which comes up after the login to my windows process.

Has anybody encountered scenario like this, and been able to perform remote-debugging? Any help/advice would be really appreciated.

Please let me know if any specific information is required?

1 Answers1

0

The thing is that when Selenium launches your app, it passes the --remote-debugging-port=<port> command line argument to your program. You need to forward this port to the Chromium engine launched by JxBrowser in your app. The following example demonstrates how to do it:

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.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public final class App {

    private static final String REMOTE_DEBUGGING_PORT_ARG = "--remote-debugging-port=";

    public static void main(String[] args) {
        // Create a builder for EngineOptions.
        EngineOptions.Builder builder = EngineOptions.newBuilder(HARDWARE_ACCELERATED);

        // Pass the remote debugging port from the command line if it presents.
        remoteDebuggingPortFromCommandLine(args).ifPresent(builder::remoteDebuggingPort);

        // Creating and running 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.
            final BrowserView view = BrowserView.newInstance(browser);

            // Creating and displaying Swing app frame.
            JFrame frame = new JFrame("Hello World");
            // Close Engine and onClose app window
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    engine.close();
                }
            });
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            JTextField addressBar = new JTextField("https://google.com");
            addressBar.addActionListener(e ->
                    browser.navigation().loadUrl(addressBar.getText()));
            frame.add(addressBar, BorderLayout.NORTH);
            frame.add(view, BorderLayout.CENTER);
            frame.setSize(800, 500);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            browser.navigation().loadUrl(addressBar.getText());
        });
    }

    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();
    }
}

I hope it helps )

Vladimir
  • 1
  • 1
  • 23
  • 30