MachinePublishers / jBrowserDriver

A programmable, embeddable web browser driver compatible with the Selenium WebDriver spec -- headless, WebKit-based, pure Java
Other
809 stars 143 forks source link

NoSuchElement exception when mocking response #191

Closed jonyt closed 7 years ago

jonyt commented 7 years ago

I have the following test which uses okhttp3.mockwebserver.MockWebServer:

String body = "<!doctype html>\n" +
                "<html lang=en>\n" +
                "<head>\n" +
                "<meta charset=utf-8>\n" +
                "<title>blah</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<p>I'm the content</p>\n" +
                "</body>\n" +
                "</html>";
        MockWebServer server = new MockWebServer();
        server.enqueue(new MockResponse().setBody(body));
        server.start();

        String url = server.url("").toString();
        JBrowserDriver j = new JBrowserDriver(Settings.builder().build());
        j.get(url);

        String actual = j.getPageSource();
        server.shutdown();

        assertEquals(body, actual);

And I get the following exception:

org.openqa.selenium.NoSuchElementException: Element not found or does not exist.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'host-1', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.2.0-35-generic', java.version: '1.8.0_101'
Driver info: driver.version: unknown

    at com.machinepublishers.jbrowserdriver.ElementServer.validate(ElementServer.java:202)
    at com.machinepublishers.jbrowserdriver.ElementServer.access$000(ElementServer.java:62)
    at com.machinepublishers.jbrowserdriver.ElementServer$1.perform(ElementServer.java:138)
    at com.machinepublishers.jbrowserdriver.AppThread$Runner.run(AppThread.java:94)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.machinepublishers.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:94)
    at com.machinepublishers.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:53)
    at java.lang.Thread.run(Thread.java:745)

What element is it looking for? I can't seem to be able to trace it.

hollingsworthd commented 7 years ago

The stack trace should be friendlier.

I'd guess it's looking for the HTML tag and it doesn't exist in the page. getPageSource basically gets the outer HTML of that element. If that's the case, the next question is why isn't the HTML element there. Not sure on that.

jonyt commented 7 years ago

It's strange because when I capture the packets with tcpdump I can see the HTML getting transferred.

hollingsworthd commented 7 years ago

You could turn on headed mode and see what it looks like. And also debug the http traffic and headers. Settings.builder().headless(false).logTrace(true).logWire(true)

jonyt commented 7 years ago

Thanks, that solved it! It turns out MockWebServer doesn't set Content-Type unless told to explicitly and jBrowser doesn't like that.