Search code examples
groovyphantomjsgeb

Geb Test Framework -- get raw page content


Is there a way to get the raw page content using Geb ?

For example the following test should work (but PhantomJS seems to bad the JSON response with HTML code):

def "Get page content example -- health check"() {
        given:
        go "https://status.github.com/api/status.json"

        expect:
        assert driver.pageSource.startsWith('{"status":"(good)"')
    }

Note that, YES I understand that I could just NOT use Geb, and simply just make a URL call in Groovy, but for a number of reasons I want to explicitly use Geb (one of the reasons is dealing with redirects).


Solution

  • What a web browser renders when it loads a URL depends on the browser itself, there is nothing you can do about it. PhantomJS uses the same engine as Chrome, thus the two of them render some HTML around the JSON. IE, Edge and Firefox do the same, by the way. HtmlUnit for a change renders the pure JSON. But why bother with exact matches like startsWith if you can just use a regular expression? It is much more flexible:

    expect:
    driver.pageSource =~ /"status":"good"/
    

    This should work in all browser engines.

    P.S.: You do not need assert in then: or expect: blocks, that is the beauty of Spock/Geb.