Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getHtmlElementById()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getHtmlElementById() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlUnitXPathTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if test fails
 */
@Test
public void changingAttributes() throws Exception {
    final String content = "<html><head><title>foo</title></head>\n"
        + "<body>\n"
        + "<div id='testDiv' title='foo'></div>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(content);
    final HtmlDivision div = page.getHtmlElementById("testDiv");

    assertSame(div, page.getFirstByXPath("//*[@title = 'foo']"));
    assertNull(page.getFirstByXPath("//*[@class = 'design']"));

    div.setAttribute("class", "design");
    assertSame(div, page.getFirstByXPath("//*[@class = 'design']"));
}
 
Example 2
Source File: HTMLFormElement2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void formSubmitDoesntCallOnSubmit() throws Exception {
    final String html
        = "<html><head><title>first</title></head><body>\n"
        + "<form name='form1' method='get' action='" + URL_SECOND + "' onsubmit=\"this.action = 'foo.html'\">\n"
        + "  <input type='submit' />\n"
        + "</form>\n"
        + "<a href='javascript:document.form1.submit()' id='link1'>Click me</a>\n"
        + "</body></html>";
    final String secondContent
        = "<html><head><title>second</title></head><body>\n"
        + "<p>hello world</p>\n"
        + "</body></html>";

    getMockWebConnection().setDefaultResponse(secondContent);

    final HtmlPage page = loadPageWithAlerts(html);
    final HtmlAnchor link = page.getHtmlElementById("link1");
    final HtmlPage page2 = link.click();
    assertEquals("second", page2.getTitleText());
}
 
Example 3
Source File: Event3Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private void testEventOnClick_Shift_Ctrl_Alt(final boolean shiftKey,
        final boolean ctrlKey, final boolean altKey, final String[] expectedAlerts) throws Exception {
    final String htmlContent
        = "<html><head><title>foo</title></head><body>\n"
        + "<form id='form1'>\n"
        + "  <button name='button' type='button' id='button'>Push me</button>\n"
        + "</form>\n"
        + "<script>\n"
        + "function handler(_e) {\n"
        + "  var e = _e ? _e : window.event;\n"
        + "  alert(e.shiftKey + ',' + e.ctrlKey + ',' + e.altKey);\n"
        + "}\n"
        + "document.getElementById('button').onclick = handler;\n"
        + "</script>\n"
        + "</body></html>";
    final List<String> collectedAlerts = new ArrayList<>();
    final HtmlPage page = loadPage(htmlContent, collectedAlerts);
    final HtmlButton button = page.getHtmlElementById("button");

    final HtmlPage secondPage = button.click(shiftKey, ctrlKey, altKey);

    assertEquals(expectedAlerts, collectedAlerts);

    assertSame(page, secondPage);
}
 
Example 4
Source File: MockMvcWebClientCreateTaskTests.java    From spring4-sandbox with Apache License 2.0 6 votes vote down vote up
@Test
	public void testCreateTasks() throws Exception {

		HtmlPage createTaskPage = webClient.getPage("http://localhost:8080/tasks/new");

		HtmlForm form = createTaskPage.getHtmlElementById("form");
		HtmlTextInput nameInput = createTaskPage.getHtmlElementById("name");
		nameInput.setValueAttribute("My first task");
		HtmlTextArea descriptionInput = createTaskPage.getHtmlElementById("description");
		descriptionInput.setText("Description of my first task");
		HtmlButton submit = form.getOneHtmlElementByAttribute("button", "type", "submit");
		HtmlPage taskListPage = submit.click();

		Assertions.assertThat(taskListPage.getUrl().toString()).endsWith("/tasks");
//		String id = taskListPage.getHtmlElementById("todolist").getTextContent();
//		assertThat(id).isEqualTo("123");
//		String summary = newMessagePage.getHtmlElementById("summary").getTextContent();
//		assertThat(summary).isEqualTo("Spring Rocks");
//		String text = newMessagePage.getHtmlElementById("text").getTextContent();
//		assertThat(text).isEqualTo("In case you didn't know, Spring Rocks!");

	}
 
Example 5
Source File: HTMLFormElement2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("onchange")
public void fileInput_fireOnChange() throws Exception {
    final String html = "<html><body>\n"
        + "<form>\n"
        + "  <input type='file' name='myFile' id='myFile' onchange='alert(\"onchange\")'/>\n"
        + "</form>\n"
        + "</body></html>";

    final List<String> collectedAlerts = new ArrayList<>();
    final HtmlPage page = loadPage(html, collectedAlerts);
    final HtmlFileInput fileInput = page.getHtmlElementById("myFile");
    fileInput.focus();
    fileInput.setAttribute("value", "dummy.txt");
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 6
Source File: HTMLDocumentWriteTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void write2() throws Exception {
    final String html
        = "<html><head><title>First</title></head><body>\n"
        + "<script>\n"
        + "document.write(\"<div id='div1'></div>\");\n"
        + "document.write('<div', \" id='div2'>\", '</div>');\n"
        + "document.writeln('<div', \" id='div3'>\", '</div>');\n"
        + "</script>\n"
        + "</form></body></html>";

    final HtmlPage page = loadPageWithAlerts(html);
    assertEquals("First", page.getTitleText());

    page.getHtmlElementById("div1");
    page.getHtmlElementById("div2");
    page.getHtmlElementById("div3");
}
 
Example 7
Source File: LocationTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that location.reload() works correctly.
 * @throws Exception if the test fails
 */
@Test
public void reload() throws Exception {
    final String content =
          "<html>\n"
        + "  <head><title>test</title></head>\n"
        + "  <body>\n"
        + "    <a href='javascript:window.location.reload();' id='link1'>reload</a>\n"
        + "  </body>\n"
        + "</html>";

    final HtmlPage page1 = loadPage(content);
    final HtmlAnchor link = page1.getHtmlElementById("link1");
    final HtmlPage page2 = link.click();

    assertEquals(page1.getTitleText(), page2.getTitleText());
    assertNotSame(page1, page2);
}
 
Example 8
Source File: HtmlUnitXPathTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if test fails
 */
@Test
public void id() throws Exception {
    final String content = "<html><head><title>foo</title></head>\n"
        + "<body>\n"
        + "<div>\n"
        + "  <a href='link.html' id='test'>\n"
        + "</div>\n"
        + "</body></html>";

    final HtmlPage page = loadPage(content);

    final HtmlAnchor anchor = page.getHtmlElementById("test");
    assertSame(anchor, page.getFirstByXPath("//a[@id='test']"));
    assertSame(anchor, page.getFirstByXPath("//*[@id='test']"));

    assertNull(page.getFirstByXPath("//div[@id='doesNotExist']"));
    assertNull(page.getFirstByXPath("id('doesNotExist')"));
}
 
Example 9
Source File: HTMLTextAreaElement2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Method type(...) should not trigger onchange!
 * @throws Exception if the test fails
 */
@Test
public void type_onchange() throws Exception {
    final String content
        = "<html><head><title>foo</title>\n"
        + "<script>\n"
        + "  function changed(e) {\n"
        + "    log('changed: ' + e.value);\n"
        + "  }\n"
        + "  function keypressed(e) {\n"
        + "    log('keypressed: ' + e.value);\n"
        + "  }\n"
        + "  function log(msg) {\n"
        + "    document.getElementById('log').value += msg + '; ';\n"
        + "  }\n"
        + "</script></head>\n"
        + "<body>\n"
        + "<form id='form1'>\n"
        + "<textarea id='textArea1' onchange='changed(this)' onkeypress='keypressed(this)'></textarea>\n"
        + "<textarea id='log'></textarea>\n"
        + "</form>\n"
        + "</body></html>";
    final HtmlPage page = loadPageWithAlerts(content);
    final HtmlTextArea textArea = page.getHtmlElementById("textArea1");
    textArea.type("hello");
    page.setFocusedElement(null); // remove focus on textarea to trigger onchange

    final HtmlTextArea log = page.getHtmlElementById("log");
    final String expectation = "keypressed: ; "
        + "keypressed: h; "
        + "keypressed: he; "
        + "keypressed: hel; "
        + "keypressed: hell; "
        + "changed: hello;";
    assertEquals(expectation, log.asText());
}
 
Example 10
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static String getClientIdByName(String clientName, HtmlPage registeredClientsPage) {
    final HtmlTable table = registeredClientsPage.getHtmlElementById("registered_clients");
    for (final HtmlTableRow row : table.getRows()) {
        if (clientName.equals(row.getCell(0).asText())) {
            final String clientId = row.getCell(1).asText();
            assertNotNull(clientId);
            return clientId;
        }
    }
    throw new IllegalArgumentException("Client '" + clientName + "' not found");
}
 
Example 11
Source File: HTMLElement3Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"onfocus text1", "onfocus text2", "onfocus text1", "onfocus text2"})
public void onFocusOnWindowFocusGain() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    final List<String> collectedAlerts = new ArrayList<>();

    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final String firstHtml = "<html><head><title>First</title></head>\n"
        + "<body><form name='form1'>\n"
        + "<input id='text1' onfocus='alert(\"onfocus text1\")'>\n"
        + "<button type='button' id='clickme' onClick='window.open(\"" + URL_SECOND + "\");'>Click me</a>\n"
        + "</form></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><head><title>Second</title></head>\n"
        + "<body onLoad='doTest()'>\n"
        + "<input id='text2' onfocus='alert(\"onfocus text2\")'>\n"
        + "<script>\n"
        + "  function doTest() {\n"
        + "    opener.document.getElementById('text1').focus();\n"
        + "    document.getElementById('text2').focus();\n"
        + "  }\n"
        + "</script></body></html>";

    webConnection.setResponse(URL_SECOND, secondHtml);
    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage(URL_FIRST);
    assertEquals("First", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage secondPage = buttonA.click();
    assertEquals("Second", secondPage.getTitleText());
    webClient.setCurrentWindow(firstPage.getEnclosingWindow());
    webClient.setCurrentWindow(secondPage.getEnclosingWindow());
    assertEquals(getExpectedAlerts(), collectedAlerts);
}
 
Example 12
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain an element with the specified ID.
 *
 * @param page the page to check
 * @param id the ID of an element which expected to not exist on the page
 */
public static void assertElementNotPresent(final HtmlPage page, final String id) {
    try {
        page.getHtmlElementById(id);
    }
    catch (final ElementNotFoundException e) {
        return;
    }
    final String msg = "The page contains an element with ID '" + id + "'.";
    throw new AssertionError(msg);
}
 
Example 13
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page does not contain an element with the specified ID.
 *
 * @param page the page to check
 * @param id the ID of an element which expected to not exist on the page
 */
public static void assertElementNotPresent(final HtmlPage page, final String id) {
    try {
        page.getHtmlElementById(id);
    }
    catch (final ElementNotFoundException e) {
        return;
    }
    final String msg = "The page contains an element with ID '" + id + "'.";
    throw new AssertionError(msg);
}
 
Example 14
Source File: HTMLDocumentWriteTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Regression test for Bug #71.
 * @throws Exception if the test fails
 */
@Test
public void write_script() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();
    webClient.setWebConnection(webConnection);

    final String mainHtml
        = "<html><head><title>Main</title></head><body>\n"
        + "<iframe name='iframe' id='iframe' src='http://first'></iframe>\n"
        + "<script type='text/javascript'>\n"
        + "document.write('<script type=\"text/javascript\" src=\"http://script\"></' + 'script>');\n"
        + "</script></body></html>";
    webConnection.setResponse(new URL("http://main/"), mainHtml);

    final String firstHtml = "<html><body><h1 id='first'>First</h1></body></html>";
    webConnection.setResponse(URL_FIRST, firstHtml);

    final String secondHtml = "<html><body><h1 id='second'>Second</h1></body></html>";
    webConnection.setResponse(URL_SECOND, secondHtml);

    final String script = "document.getElementById('iframe').src = '" + URL_SECOND + "';\n";
    webConnection.setResponse(new URL("http://script/"), script, MimeType.APPLICATION_JAVASCRIPT);

    final List<String> collectedAlerts = new ArrayList<>();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final HtmlPage mainPage = webClient.getPage("http://main");
    assertEquals("Main", mainPage.getTitleText());

    final HtmlInlineFrame iFrame = mainPage.getHtmlElementById("iframe");

    assertEquals(URL_SECOND.toExternalForm(), iFrame.getSrcAttribute());

    final HtmlPage enclosedPage = (HtmlPage) iFrame.getEnclosedPage();
    // This will blow up if the script hasn't been written to the document
    // and executed so the second page has been loaded.
    enclosedPage.getHtmlElementById("second");
}
 
Example 15
Source File: WindowTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void setOpenerLocationHrefRelative() throws Exception {
    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();

    final String aContent
        = "<html><head><title>A</title></head><body>\n"
        + "<button id='clickme' onClick='window.open(\"b/b.html\");'>Click me</a>\n"
        + "</body></html>";
    final String bContent
        = "<html><head><title>B</title></head><body>\n"
        + "<button id='clickme' onClick='opener.location.href=\"../c.html\";'>Click me</a>\n"
        + "</body></html>";
    final String cContent
        = "<html><head><title>C</title></head><body></body></html>";
    final String failContent
        = "<html><head><title>FAILURE!!!</title></head><body></body></html>";

    webConnection.setResponse(new URL("http://opener/test/a.html"), aContent);
    webConnection.setResponse(new URL("http://opener/test/b/b.html"), bContent);
    webConnection.setResponse(new URL("http://opener/test/c.html"), cContent);
    webConnection.setResponse(new URL("http://opener/c.html"), failContent);

    webClient.setWebConnection(webConnection);

    final HtmlPage firstPage = webClient.getPage("http://opener/test/a.html");
    assertEquals("A", firstPage.getTitleText());

    final HtmlButton buttonA = firstPage.getHtmlElementById("clickme");
    final HtmlPage pageB = buttonA.click();
    assertNotNull("B", pageB);
    assertEquals("B", pageB.getTitleText());

    final HtmlButton buttonB = pageB.getHtmlElementById("clickme");
    final HtmlPage thirdPage = buttonB.click();
    assertSame("Page B has lost focus", pageB, thirdPage);
    assertEquals("C", ((HtmlPage) firstPage.getEnclosingWindow().getEnclosedPage()).getTitleText());
}
 
Example 16
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the specified page contains an element with the specified ID.
 *
 * @param page the page to check
 * @param id the expected ID of an element in the page
 */
public static void assertElementPresent(final HtmlPage page, final String id) {
    try {
        page.getHtmlElementById(id);
    }
    catch (final ElementNotFoundException e) {
        final String msg = "The page does not contain an element with ID '" + id + "'.";
        throw new AssertionError(msg);
    }
}
 
Example 17
Source File: WebClientTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the JavaScript parent scope is set correctly when shuffling windows around.
 * @throws Exception if test fails
 */
@Test
public void maintainJavaScriptParentScope() throws Exception {
    final String basicContent = "<html><head>\n"
            + "<title>basicContentTitle</title>\n"
            + "</head><body>\n"
            + "<p>Hello World</p>\n"
            + "</body></html>";

    final String jsContent = "<html><head>\n"
            + "<title>jsContentTitle</title>\n"
            + "<script>function foo() {alert('Ran Here')}</script>\n"
            + "<script>function bar() {}</script>\n"
            + "</head><body onload='bar()'>\n"
            + "<input type='button' id='button' onclick='foo()'/>"
            + "</body></html>";

    final HtmlPage jsPage = loadPage(jsContent);
    final WebClient webClient = jsPage.getWebClient();
    final WebWindow firstWindow = webClient.getCurrentWindow();
    getMockConnection(jsPage).setResponse(URL_SECOND, basicContent);

    final CollectingAlertHandler alertHandler = new CollectingAlertHandler();
    webClient.setAlertHandler(alertHandler);

    final HtmlButtonInput buttonBefore = jsPage.getHtmlElementById("button");

    final WebWindow secondWindow = webClient.openWindow(null, "second window");
    webClient.setCurrentWindow(secondWindow);
    webClient.getPage(URL_SECOND);

    webClient.setCurrentWindow(firstWindow);

    final HtmlPage currentPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
    final HtmlButtonInput buttonAfter = currentPage.getHtmlElementById("button");
    assertSame(buttonBefore, buttonAfter);

    buttonAfter.click();

    assertEquals(1, alertHandler.getCollectedAlerts().size());
    assertEquals("Ran Here", alertHandler.getCollectedAlerts().get(0));
}
 
Example 18
Source File: WindowTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Test for the <tt>showModalDialog</tt> method.
 * This tests blocking until the window gets closed.
 * Can not currently be tested with WebDriver
 * https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/284
 *
 * To fix this, we need to allow user to interact with the opened dialog before showModalDialog() returns
 * @throws Exception if an error occurs
 */
@Test
@Alerts(DEFAULT = {"undefined", "result", "finished"},
        CHROME = {"undefined", "not available"},
        FF = {"undefined", "not available"},
        FF68 = {"undefined", "not available"},
        FF60 = {"undefined", "not available"})
@NotYetImplemented(IE)
public void showModalDialogWithButton() throws Exception {
    final String html1
        = "<html><head>\n"
        + "  <script>\n"
        + "    function test() {\n"
        + "      alert(window.returnValue);\n"
        + "      if (!window.showModalDialog) {alert('not available'); return; }\n"
        + "      var res = showModalDialog('myDialog.html', null, 'dialogHeight:300px; dialogLeft:200px;');\n"
        + "      alert(res);\n"
        + "      alert('finished');\n"
        + "    }\n"
        + "  </script>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <button onclick='test()' id='openDlg'>Test</button>\n"
        + "</body></html>";

    final String html2
        = "<html><head>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <button id='closeDlg' onclick='window.returnValue = \"result\"; window.close();'></button>\n"
        + "</body>\n"
        + "</html>";

    final WebClient client = getWebClient();
    final List<String> actual = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(actual));

    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, html1);
    conn.setResponse(new URL(URL_FIRST, "myDialog.html"), html2);
    client.setWebConnection(conn);

    final HtmlPage page = getWebClient().getPage(URL_FIRST);
    final HtmlElement button = page.getHtmlElementById("openDlg");
    button.click();

    // TODO: <button id='closeDlg'> should be clicked
    assertEquals(getExpectedAlerts(), actual);
}
 
Example 19
Source File: Selection2Test.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private void test(final String action, final String x, final String alert)
    throws Exception {

    final String html = "<html>\n"
        + "<body onload='test()'>\n"
        + "  <span id='s1'>abc</span><span id='s2'>xyz</span><span id='s3'>foo</span>\n"
        + "  <input type='button' id='b' onclick=\"" + action + ";test();\" value='click'></input>\n"
        + "<script>\n"
        + "  var selection = document.selection; // IE\n"
        + "  if(!selection) selection = window.getSelection(); // FF\n"
        + "  var s1 = document.getElementById('s1');\n"
        + "  var s2 = document.getElementById('s2');\n"
        + "  function test() {\n"
        + "    try {\n"
        + "      var x = " + x + ";\n"
        + "      alert(" + alert + ");\n"
        + "    } catch (e) {\n"
        + "      alert('unsupported action');\n"
        + "    }\n"
        + "  }\n"
        + "</script>\n"
        + "</body></html>";

    final WebClient client = getWebClient();

    final MockWebConnection conn = new MockWebConnection();
    conn.setDefaultResponse(html);
    client.setWebConnection(conn);

    final List<String> actual = new ArrayList<>();
    final AlertHandler alertHandler = new CollectingAlertHandler(actual);
    client.setAlertHandler(alertHandler);

    final HtmlPage page = client.getPage(URL_FIRST);
    final DomNode s1Text = page.getHtmlElementById("s1").getFirstChild();
    final DomNode s2Text = page.getHtmlElementById("s2").getFirstChild();
    final HtmlInput input = page.getHtmlElementById("b");

    final org.w3c.dom.ranges.Range range = new SimpleRange();
    range.setStart(s1Text, 2);
    range.setEnd(s2Text, 1);
    page.setSelectionRange(range);
    input.click();

    assertEquals(getExpectedAlerts(), actual);
}
 
Example 20
Source File: HTMLObjectElementTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Simple hack to proof, that a test driver can manipulate
 * a activeX mock at runtime.
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = {},
        IE = {"Javascript called this method!", "ActiveX is still alive"})
public void activeXInteraction() throws Exception {
    final String clsid = "clsid:TESTING-CLASS-ID";
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "<script>\n"
        + "  function test() {\n"
        + "    var obj = document.all.id1;\n"
        + "    if (obj.GetMessage) {\n"
        + "      alert(obj.GetMessage());\n"
        + "    }\n"
        + "  }\n"
        + "</script>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <object id='id1' classid='" + clsid + "'></object>\n"
        + "  <button id='myButton' onClick='test()'>Click Me</button>\n"
        + "</body></html>";

    final WebClient client = getWebClientWithMockWebConnection();
    final Map<String, String> activeXObjectMap = new HashMap<>();
    activeXObjectMap.put(clsid, "com.gargoylesoftware.htmlunit.javascript.MockActiveXObject");
    client.setActiveXObjectMap(activeXObjectMap);

    final List<String> collectedAlerts = new ArrayList<>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    final HtmlPage page = loadPage(html);

    page.getHtmlElementById("myButton").click();

    final HtmlElement elem = page.getHtmlElementById("id1");
    final HTMLObjectElement jsElem = (HTMLObjectElement) elem.getScriptableObject();
    final MockActiveXObject activeX = (MockActiveXObject) jsElem.unwrap();
    if (null != activeX) {
        activeX.setMessage("ActiveX is still alive");
        page.getHtmlElementById("myButton").click();
    }

    assertEquals(getExpectedAlerts(), collectedAlerts);
}