List icon Contents

Automated tests

JxBrowser API provides a lot of features that can be used for writing automated tests and simulating user activity on a web page.

Overview

Using JxBrowser API you can:

  • Load URL, HTML from string, local file including PDF documents.
  • 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 currently loaded web page.
  • Handle JavaScript dialogs such as alert, confirmation, prompt.
  • Configure Proxy settings per Engine instance and more.

Example

The following example demonstrates how to programmatically log in 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.frame.Frame;
import com.teamdev.jxbrowser.view.swing.BrowserView;
import java.util.Optional;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public final class FacebookLogin {

    public static void main(String[] args) {
        var engine = Engine.newInstance(HARDWARE_ACCELERATED);
        var browser = engine.newBrowser();

        SwingUtilities.invokeLater(() -> {
            var view = BrowserView.newInstance(browser);

            var 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) {
        var mainFrame = browser.mainFrame()
                .orElseThrow(IllegalStateException::new);
        var document = mainFrame.document()
                .orElseThrow(IllegalStateException::new);
        var documentElement = document.documentElement()
                .orElseThrow(IllegalStateException::new);
        return documentElement.findElementById(id);
    }
}

UI test automation

JxBrowser is compatible with different UI software testing tools that let you build a test suite for your entire application UI.

QF-Test

If you prefer a GUI solution including Record & Replay functionality, we recommend using the professional UI software testing tool QF-Test (by QFS). With QF-Test’s intuitive user interface you can build a test suite for your entire application UI including any embedded JxBrowser instances.

QF-Test Automated Tool

QF-Test handles all the communication with the JxBrowser API for you, leaving you to concentrate on meeting your testing requirements.