JxBrowser API provides a lot of features that can be used for writing automated tests.

Using JxBrowser API you can:

  • Load URL, HTML string, local file and wait until web page is loaded completely.
  • Access JavaScript Console to read all log messages.
  • Access JavaScript on the loaded web page and execute any JavaScript code.
  • Access DOM of the loaded web page. Read and modify DOM HTML elements.
  • Access and modify cookies.
  • Access and modify HTTP request/response headers on the fly.
  • Using remote debugging port (--remote-debugging-port=9222) you can attach to JxBrowser from Google Chrome to debug JavaScript on the loaded in JxBrowser web page.
  • Get notifications from web browser control about title, status, navigation, etc. events.
  • Handle JavaScript dialogs such as alert, confirmation, prompt.
  • Configure Proxy settings per Engine instance and more.

For example, the following example demonstrates how to programmatically login to Facebook using specific credentials:

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.dom.Document;
import com.teamdev.jxbrowser.dom.Element;
import com.teamdev.jxbrowser.dom.FormControlElement;
import com.teamdev.jxbrowser.dom.Node;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.frame.Frame;
import com.teamdev.jxbrowser.view.swing.BrowserView;
import java.util.Optional;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class FacebookLogin {

    public static void main(String[] args) {
        // Create and initialize the Engine
        EngineOptions options =
                EngineOptions.newBuilder(HARDWARE_ACCELERATED).build();
        Engine engine = Engine.newInstance(options);
        
        // Create the Browser
        Browser browser = engine.newBrowser();

        SwingUtilities.invokeLater(() -> {
            // Create the Swing BrowserView
            BrowserView view = BrowserView.newInstance(browser);

            JFrame frame = new JFrame();
            frame.add(view);
            frame.setSize(800, 600);
            frame.setVisible(true);

            // Load Facebook login page and wait until it is loaded completely
            browser.navigation().loadUrlAndWait(
                    "https://www.facebook.com/login.php");

            // Find and enter email
            findElementById(browser, "email").ifPresent(emailElement ->
                    ((FormControlElement) emailElement).value("user@gmail.com"));
            // Find and enter password
            findElementById(browser, "pass").ifPresent(passElement ->
                    ((FormControlElement) passElement).value("123"));
            // Find and click the Login button
            findElementById(browser, "loginbutton").ifPresent(Node::click);
        });
    }

    private static Optional<Element> findElementById(Browser browser, String id) {
        Frame mainFrame = browser.mainFrame()
                .orElseThrow(IllegalStateException::new);
        Document document = mainFrame.document()
                .orElseThrow(IllegalStateException::new);
        Element documentElement = document.documentElement()
                .orElseThrow(IllegalStateException::new);
        return documentElement.findElementById(id);
    }
}
Go Top