Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)

JxBrowser API provides a lot of features that can be used for writing automated tests. You can:(JxBrowser API提供了许多可用于编写自动化测试的功能。您可以:)

  • Load URL, HTML string, local file and wait until web page is loaded completely.(加载URL,HTML字符串,本地文件,等待网页加载完成)
  • Access JavaScript Console to read all messages.(访问JavaScript控制台以读取所有消息)
  • Access JavaScript on the loaded web page and execute any JavaScript code.(在加载的网页上访问JavaScript并执行所有JavaScript代码)
  • Access DOM of the loaded web page. Read and modify DOM HTML elements.(访问已加载网页的DOM,读取和修改DOM HTML元素)
  • Access and modify Cookies.(访问和修改Cookies)
  • Access and modify HTTP request/response headers on the fly.(快速访问和修改HTTP请求/响应标头)
  • 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.(使用远程调试端口(--remote-debugging-port = 9222),您可以从Google Chrome连接到JxBrowser,以调试JxBrowser网页上加载的JavaScript)
  • Get notifications from web browser control about title, status, navigation, etc. events.(从Web浏览器控件获取标题,状态,导航等事件的通知)
  • Handle JavaScript dialogs such as alert, confirmation, prompt. Configure Proxy settings per Browser instance and more.(处理JavaScript对话框,例如警报,确认,提示。 为每个浏览器实例配置代理设置等……)

For example, let’s test Facebook Login form and validate the use case when user enters invalid login and password. Expected behavior is that Login form must display an error message:(例如,让我们测试Facebook登录表单,并在用户输入无效的登录名和密码时验证用例。预期的行为是“登录”表单必须显示错误消息:)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.Callback;
import com.teamdev.jxbrowser.chromium.dom.By;
import com.teamdev.jxbrowser.chromium.dom.DOMDocument;
import com.teamdev.jxbrowser.chromium.dom.DOMFormControlElement;

public class LoginFacebookTest {
    public static void main(String[] args) {
        final Browser browser = new Browser();

        // Load and wait until web page is loaded completely.
        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            public void invoke(Browser browser) {
        // Access DOM document of the loaded web page.
        final DOMDocument document = browser.getDocument();
        // Find and enter email.
        ((DOMFormControlElement) document.findElement("email"))).setValue("");
        // Find and enter password.
        ((DOMFormControlElement) document.findElement("pass"))).setValue("123");
        // Find and click Login button and wait until a new web page is loaded completely.
        Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
            public void invoke(Browser browser) {

        // Find Login form and get its inner HTML.
        String loginFormHTML = browser.getDocument().findElement("login_form")).getTextContent();
        // Check whether inner HTML has the required error message.
        boolean hasErrorMessage = loginFormHTML.contains("Please re-enter your password");
        if (!hasErrorMessage) {
            System.err.println("Test Failed: No error message is displayed.");
        System.out.println("hasErrorMessage = " + hasErrorMessage);

Using JxBrowser API you can create your own test scenarios for different web pages, services, intranet resources, for extracting information from any web page, etc.(使用JxBrowser API,您可以为不同的网页,服务,Intranet资源,从任何网页中提取信息并创建自己的测试方案。)