geb all-in-one standalone script configuration without GebConfig

48 views
Skip to first unread message

Rob Powell

unread,
Apr 23, 2019, 12:22:55 AM4/23/19
to Geb User Mailing List
Hello;

Is it possible to have a complete all-in-one geb script that includes browser driver configuration?

I have the following as GebExample.groovy:
@Grapes([
    @Grab("org.gebish:geb-core:2.3.1"),
    @Grab(group='io.github.bonigarcia', module='webdrivermanager', version='3.4.0'),
    @Grab(group='org.seleniumhq.selenium', module='selenium-chrome-driver', version='3.141.0'),
    @Grab(group='org.seleniumhq.selenium', module='selenium-support', version='3.141.0')
])

import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.DesiredCapabilities
import geb.Browser

WebDriverManager.chromedriver().setup();

Browser.drive {

    assert title == "Geb - Very Groovy Browser Automation" 

    $("div.menu a.manuals").click() 
    waitFor { !$("#manuals-menu").hasClass("animating") } 

    $("#manuals-menu a")[0].click() 

    assert title.startsWith("The Book Of Geb") 
}

If I have a GebConfig.groovy file in the same directory that has the following:
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
driver = {
    ChromeOptions o = new ChromeOptions()
    o.addArguments('headless')
    new ChromeDriver(o)
}

Then the script runs as headless chrome.

How do I get the config info into the single GebExample.groovy?

----

I was exploring using the following, but it did not run as headless chrome:
@Grapes([
    @Grab("org.gebish:geb-core:2.3.1"),
    @Grab(group='io.github.bonigarcia', module='webdrivermanager', version='3.4.0'),
    @Grab(group='org.seleniumhq.selenium', module='selenium-chrome-driver', version='3.141.0'),
    @Grab(group='org.seleniumhq.selenium', module='selenium-support', version='3.141.0')
])

import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.DesiredCapabilities
import geb.Browser

WebDriverManager.chromedriver().setup();

driver = {
    ChromeOptions o = new ChromeOptions()
    o.addArguments('headless')
    new ChromeDriver(o)
}

Browser.drive {

    assert title == "Geb - Very Groovy Browser Automation" 

    $("div.menu a.manuals").click() 
    waitFor { !$("#manuals-menu").hasClass("animating") } 

    $("#manuals-menu a")[0].click() 

    assert title.startsWith("The Book Of Geb") 
}

Thoughts?

jeremy...@gmail.com

unread,
Apr 23, 2019, 11:59:43 AM4/23/19
to Geb User Mailing List
Rob, you can refer to the geb-example-gradle project here: https://github.com/geb/geb-example-gradle

This should provide you with your answer.

Marcin Erdmann

unread,
Apr 24, 2019, 1:18:05 PM4/24/19
to geb-...@googlegroups.com
The easiest way to achieve what you're after would probably be the following:

import geb.Configuration

def configuration = new Configuration(
    driver: {
        ChromeOptions o = new ChromeOptions()
        o.addArguments('headless')
        new ChromeDriver(o)
    }
)

Browser.drive(configuration) {
    ....
}

--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/26f740de-6432-4606-b56c-fedf4acb06be%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Powell

unread,
Apr 24, 2019, 9:39:10 PM4/24/19
to Geb User Mailing List
Thank you Marcin!

Here is the complete example that works for me:
@Grapes([
    @Grab("org.gebish:geb-core:2.3.1"),
    @Grab(group='io.github.bonigarcia', module='webdrivermanager', version='3.4.0'),
    @Grab(group='org.seleniumhq.selenium', module='selenium-chrome-driver', version='3.141.0'),
    @Grab(group='org.seleniumhq.selenium', module='selenium-support', version='3.141.0')
])

import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.DesiredCapabilities
import geb.Configuration
import geb.Browser

WebDriverManager.chromedriver().setup();

def configuration = new Configuration(
    driver: {
        ChromeOptions o = new ChromeOptions()
        o.addArguments('headless')
        new ChromeDriver(o)
    }
)

Browser.drive(configuration) {
To unsubscribe from this group and stop receiving emails from it, send an email to geb-...@googlegroups.com.

Michael Kutz

unread,
Apr 26, 2019, 11:49:41 AM4/26/19
to Geb User Mailing List
I really liked the idea of a standalone script running Geb. My main use case would be to automate initial steps for a manual test (e.g. register a user account in a certain way, fill test shopping cart etc.).
So I took today to refine the code a bit. Mixed in some PicoCLI goodness et voilà: https://gist.github.com/mkutz/58d3ae2f07c8c519c8900ab5b25f5e87

To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.

To post to this group, send email to geb-...@googlegroups.com.

Rob Powell

unread,
Apr 27, 2019, 11:40:10 AM4/27/19
to geb-...@googlegroups.com
I like this too;

You reminded me that I should make a gist about this!

-Rob

Reply all
Reply to author
Forward
0 new messages