Chris
Chris

Reputation: 99

Can the Geb baseUrl navigation be used with subdomains?

I'm using Geb to write some browser automation tests. It allows you to configure a baseUrl and specify browser actions relative to this, as detailed in The Book of Geb. This works nicely for paths within the site but I can't see any syntax for dealing with subdomains.

Is there a simple way of going from baseUrl = http://myapp.com/ to http://sub.myapp.com using the Geb DSL or am I going to have to grab the property that defines the baseUrl in code and use it to generate the subdomain?

Upvotes: 4

Views: 3061

Answers (3)

twinj
twinj

Reputation: 2039

In Geb the Browser class has this method:

    /**
 * Changes the base url used for resolving relative urls.
 * <p>
 * This method delegates to {@link geb.Configuration#setBaseUrl}.
 */
void setBaseUrl(String baseUrl) {
    config.baseUrl = baseUrl
}

Geb Javadoc

I successfully use it to switch server contexts of the same application.

E.g: browser.setBaseUrl('http://int/app/pages/') browser.setBaseUrl('http://ci/sameapp/pages/')

If you are running tests using Spock this needs to be done before each feature as it gets reset.

Upvotes: 1

Chris
Chris

Reputation: 99

As stated by erdi there seems to be no way to currently do it. In the end we added an overridden version of getPageUrl() to our subclass of Page.

String getPageUrl() {
    def subdomainPresent = this.class.declaredFields.find {
        it.name == 'subdomain' && isStatic(it.modifiers)
    }
    if( subdomainPresent ) {
        def baseURL = getBrowser().getConfig().getBaseUrl()
        def splicePoint = baseURL.indexOf('//') + 1

        pageUrl = baseURL[0..splicePoint] + this.class.subdomain + "." + baseURL[splicePoint+1..-1] + pageUrl
    }
    pageUrl
}

Used like this for account.{baseUrl}/login

class MyPage extends MyPageBase{
    static subdomain = "account"
    static url = "login"
}

Documented here as a pull request https://github.com/geb/geb/pull/37/files

Upvotes: 1

erdi
erdi

Reputation: 6954

As far as I know there is no way to modify baseUrl during test execution other than by directly setting it in the config:

browser.config.baseUrl = 'http://sub.myapp.com'

Upvotes: 0

Related Questions