Geb Programmer : Obtain current page html source?

Geb manual:

Geb builds on the WebDriver browser automation library, which means that Geb can work with any browser that WebDriver can.

With WebDriver is easy to obtain the page source. Lets do a test to verify it.

Lets start by creating a Gradle project with Groovy

$ gradle --version

------------------------------------------------------------
Gradle 3.1
------------------------------------------------------------
$ mkdir pagesource
$ cd page source
$ touch <i>build.gradle</i>

Add Groovy dependency to build.gradle

apply plugin: 'groovy'

Add Gradle wrapper

$ gradle wrapper --gradle-version 3.1

add Geb and Spock depependencies to build.gradle

apply plugin: 'groovy'

repositories {
    jcenter()
}
dependencies {
    compile "org.gebish:geb-core:1.0-rc-1"
    compile "org.seleniumhq.selenium:selenium-firefox-driver:2.52.0"
    compile "org.seleniumhq.selenium:selenium-support:2.52.0"
    compile 'org.spockframework:spock-core:1.0-groovy-2.4'
}

Create a test

$ vi <i>src/test/groovy/PageSourceSpec.groovy</i>
import spock.lang.Specification

import geb.Browser

class PageSourceSpec extends Specification {
   def "test page source of sergiodeamo.es has a closing </body> tag"() {
	when:
        def browser = new Browser()
        browser.go 'http://sergiodelamo.es'

        then:
        browser.driver.pageSource.contains('</body>')
   }
}

If you run the test, it will pass:

$ ./gradlew test

Do you like to read about Grails / Groovy development? If the answer is yes, subscribe to Groovy Calamari. A weekly curated email newsletter about the Groovy ecosystem. Curated by me 🎉

Leave a Reply

Your email address will not be published. Required fields are marked *