Skip to content

Commit

Permalink
Make the examples in the section on configuring navigator factory exe…
Browse files Browse the repository at this point in the history
…cutable.

Part of geb/issues#350
  • Loading branch information
erdi committed Jun 10, 2015
1 parent d053629 commit 057b60f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 19 deletions.
14 changes: 8 additions & 6 deletions doc/new-manual/src/docs/asciidoc/060-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,19 @@ If none of these classes can be found, a link:api/geb/error/UnableToLoadAnyDrive

=== Navigator factory

It is possible to specify your own implementation of link:api/geb/navigator/factory/NavigatorFactory.html[`NavigatorFactory`] via configuration. This is useful if you want to extend the link:api/geb/navigator/Navigator.html[`Navigator`] class to provide your own behaviour extensions.
It is possible to specify your own implementation of link:api/geb/navigator/factory/NavigatorFactory.html[`NavigatorFactory`] via configuration.
This is useful if you want to extend the `{navigator-api}` class to provide your own behaviour extensions.

Rather than inject your own `NavigatorFactory`, it is simpler to inject a custom link:api/geb/navigator/factory/InnerNavigatorFactory.html[`InnerNavigatorFactory`] which is a much simpler interface. To do this, you can specify a closure for the config key `innerNavigatorFactory`…
Rather than inject your own `NavigatorFactory`, it is simpler to inject a custom link:api/geb/navigator/factory/InnerNavigatorFactory.html[`InnerNavigatorFactory`] which is a much simpler interface.
To do this, you can specify a closure for the config key `innerNavigatorFactory`…

[source,groovy]
----
innerNavigatorFactory = { Browser browser, List<org.openqa.selenium.WebElement> elements
elements ? new MyCustomNavigator(browser, elements) : new geb.navigator.EmptyNavigator()
}
include::{test-dir}/configuration/NavigatorFactoryConfigSpec.groovy[tag=config,indent=0]
----

This is a rather advanced use case. If you need to do this, check out the source code or get in touch via the mailing list if you need help.
This is a rather advanced use case.
If you need to do this, check out the source code or get in touch via the mailing list if you need help.

[[driver-caching-configuration]]
=== Driver caching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package configuration

import geb.Configuration
import geb.ConfigurationLoader
import geb.driver.CachingDriverFactory
import geb.test.WebDriverServer
import org.junit.Rule
Expand All @@ -27,25 +25,15 @@ import org.openqa.selenium.remote.RemoteWebDriver
import spock.lang.Specification
import spock.lang.Unroll

class DriverConfigSpec extends Specification {
class DriverConfigSpec extends Specification implements InlineConfigurationLoader {

@Rule
TemporaryFolder temporaryFolder

Configuration config

def setupSpec() {
CachingDriverFactory.clearCacheAndQuitDriver()
}

void configScript(String env = null, String script) {
def classLoader = new GroovyClassLoader(getClass().classLoader)
classLoader.addClasspath(temporaryFolder.root.absolutePath)
def configFile = temporaryFolder.newFile()
configFile << script
config = new ConfigurationLoader(env, null, classLoader).getConf(configFile.name)
}

def "configuring driver using closure"() {
when:
configScript """
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration

import geb.Configuration
import geb.ConfigurationLoader

trait InlineConfigurationLoader implements TemporaryFolderProvider {
Configuration config

void configScript(String script) {
configScript(null, script)
}

void configScript(String env, String script) {
def classLoader = new GroovyClassLoader(getClass().classLoader)
classLoader.addClasspath(temporaryFolder.root.absolutePath)
def configFile = temporaryFolder.newFile()
configFile << script
config = new ConfigurationLoader(env, null, classLoader).getConf(configFile.name)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration

import geb.Configuration
import geb.navigator.EmptyNavigator
import geb.navigator.NonEmptyNavigator
import geb.test.GebSpecWithCallbackServer
import groovy.transform.InheritConstructors
import org.junit.ClassRule
import org.junit.rules.TemporaryFolder
import spock.lang.Shared

class NavigatorFactoryConfigSpec extends GebSpecWithCallbackServer implements InlineConfigurationLoader {

@ClassRule
@Shared
TemporaryFolder tempDir

TemporaryFolder getTemporaryFolder() {
tempDir
}

@Override
Configuration createConf() {
configScript """
import configuration.MyCustomNavigator
import configuration.MyCustomEmptyNavigator
// tag::config[]
import geb.Browser
import org.openqa.selenium.WebElement
innerNavigatorFactory = { Browser browser, List<WebElement> elements ->
elements ? new MyCustomNavigator(browser, elements) : new MyCustomEmptyNavigator()
}
// end::config[]
reportsDir = "${super.createConf().reportsDir.absolutePath}"
"""
config
}

def "specifying custom navigator implementation"() {
when:
html ""

then:
$() instanceof MyCustomNavigator
$("idontexist") instanceof MyCustomEmptyNavigator
}
}

@InheritConstructors
class MyCustomNavigator extends NonEmptyNavigator {
}

@InheritConstructors
class MyCustomEmptyNavigator extends EmptyNavigator {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configuration

import org.junit.rules.TemporaryFolder

interface TemporaryFolderProvider {
TemporaryFolder getTemporaryFolder()
}

0 comments on commit 057b60f

Please sign in to comment.