forked from grails/geb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature grails#79 - add recording support
- Loading branch information
1 parent
c459b49
commit 5cf4414
Showing
7 changed files
with
172 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/testFixtures/groovy/grails/plugin/geb/ContainerGebConfiguration.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package grails.plugin.geb | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.transform.Memoized | ||
import groovy.util.logging.Slf4j | ||
import org.testcontainers.containers.BrowserWebDriverContainer | ||
import org.testcontainers.containers.VncRecordingContainer | ||
|
||
@Slf4j | ||
@CompileStatic | ||
class ContainerGebConfiguration { | ||
String recordingDirectoryName | ||
|
||
boolean recording | ||
|
||
BrowserWebDriverContainer.VncRecordingMode recordingMode | ||
|
||
VncRecordingContainer.VncRecordingFormat recordingFormat | ||
|
||
ContainerGebConfiguration() { | ||
recording = Boolean.parseBoolean(System.getProperty('grails.geb.recording.enabled', true as String)) | ||
recordingDirectoryName = System.getProperty('grails.geb.recording.directory', 'build/recordings') | ||
recordingMode = BrowserWebDriverContainer.VncRecordingMode.valueOf(System.getProperty('grails.geb.recording.mode', BrowserWebDriverContainer.VncRecordingMode.RECORD_FAILING.name())) | ||
recordingFormat = VncRecordingContainer.VncRecordingFormat.valueOf(System.getProperty('grails.geb.recording.format', VncRecordingContainer.VncRecordingFormat.MP4.name())) | ||
} | ||
|
||
@Memoized | ||
File getRecordingDirectory() { | ||
if(!recording) { | ||
return null | ||
} | ||
|
||
File recordingDirectory = new File(recordingDirectoryName) | ||
if(!recordingDirectory.exists()) { | ||
log.info("Could not find `${recordingDirectoryName}` directory for recording. Creating...") | ||
recordingDirectory.mkdir() | ||
} | ||
else if(!recordingDirectory.isDirectory()) { | ||
throw new IllegalStateException("Recording Directory name expected to be `${recordingDirectoryName}, but found file instead.") | ||
} | ||
|
||
recordingDirectory | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/testFixtures/groovy/grails/plugin/geb/ContainerGebRecordingExtension.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package grails.plugin.geb | ||
|
||
import groovy.transform.TailRecursive | ||
import groovy.util.logging.Slf4j | ||
import org.spockframework.runtime.extension.IGlobalExtension | ||
import org.spockframework.runtime.model.SpecInfo | ||
|
||
import java.time.LocalDateTime | ||
|
||
@Slf4j | ||
class ContainerGebRecordingExtension implements IGlobalExtension { | ||
ContainerGebConfiguration configuration | ||
|
||
@Override | ||
void start() { | ||
configuration = new ContainerGebConfiguration() | ||
} | ||
|
||
@Override | ||
void visitSpec(SpecInfo spec) { | ||
if (isContainerizedGebSpec(spec)) { | ||
ContainerGebTestListener listener = new ContainerGebTestListener(spec, LocalDateTime.now()) | ||
// TODO: We should initialize the web driver container once for all geb tests so we don't have to spin it up & down. | ||
spec.addSetupInterceptor { | ||
ContainerGebSpec gebSpec = it.instance as ContainerGebSpec | ||
gebSpec.initialize() | ||
if(configuration.recording) { | ||
listener.webDriverContainer = gebSpec.webDriverContainer.withRecordingMode(configuration.recordingMode, configuration.recordingDirectory, configuration.recordingFormat) | ||
} | ||
} | ||
|
||
spec.addListener(listener) | ||
} | ||
} | ||
|
||
@TailRecursive | ||
boolean isContainerizedGebSpec(SpecInfo spec) { | ||
if(spec != null) { | ||
if(spec.filename.startsWith('ContainerGebSpec.')) { | ||
return true | ||
} | ||
|
||
return isContainerizedGebSpec(spec.superSpec) | ||
} | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/testFixtures/groovy/grails/plugin/geb/ContainerGebTestDescription.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package grails.plugin.geb | ||
|
||
import org.spockframework.runtime.model.IterationInfo | ||
import org.testcontainers.lifecycle.TestDescription | ||
|
||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
class ContainerGebTestDescription implements TestDescription { | ||
String testId | ||
String filesystemFriendlyName | ||
|
||
ContainerGebTestDescription(IterationInfo testInfo, LocalDateTime runDate) { | ||
testId = testInfo.displayName | ||
|
||
String safeName = testId.replaceAll("\\W+", "") | ||
filesystemFriendlyName = "${DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss").format(runDate)}_${safeName}" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/testFixtures/groovy/grails/plugin/geb/ContainerGebTestListener.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package grails.plugin.geb | ||
|
||
import org.spockframework.runtime.AbstractRunListener | ||
import org.spockframework.runtime.model.ErrorInfo | ||
import org.spockframework.runtime.model.IterationInfo | ||
import org.spockframework.runtime.model.SpecInfo | ||
import org.testcontainers.containers.BrowserWebDriverContainer | ||
|
||
import java.time.LocalDateTime | ||
|
||
class ContainerGebTestListener extends AbstractRunListener { | ||
BrowserWebDriverContainer webDriverContainer | ||
ErrorInfo errorInfo | ||
SpecInfo spec | ||
LocalDateTime runDate | ||
|
||
ContainerGebTestListener(SpecInfo spec, LocalDateTime runDate) { | ||
this.spec = spec | ||
this.runDate = runDate | ||
} | ||
|
||
@Override | ||
void afterIteration(IterationInfo iteration) { | ||
webDriverContainer.afterTest(new ContainerGebTestDescription(iteration, runDate), Optional.of(errorInfo?.exception)) | ||
errorInfo = null | ||
} | ||
|
||
@Override | ||
void error(ErrorInfo error) { | ||
errorInfo = error | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ixtures/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
grails.plugin.geb.ContainerGebRecordingExtension |