Skip to content

Commit

Permalink
Update Geb Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetbehl committed Jul 6, 2023
1 parent da889b5 commit 690965e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.grails.forge.application.generator.GeneratorContext;
import org.grails.forge.build.dependencies.Coordinate;
import org.grails.forge.build.dependencies.CoordinateResolver;
import org.grails.forge.build.dependencies.LookupFailedException;
import org.grails.forge.build.gradle.GradleBuild;
import org.grails.forge.build.gradle.GradleBuildCreator;
import org.grails.forge.build.gradle.GradlePlugin;
Expand All @@ -37,6 +38,7 @@
import org.grails.forge.template.URLTemplate;

import java.util.Set;
import java.util.function.Function;

@Singleton
public class Gradle implements BuildFeature {
Expand Down Expand Up @@ -79,9 +81,12 @@ public void apply(GeneratorContext generatorContext) {
build
)));


final Function<String, Coordinate> coordinateResolver = (artifactId) -> { return resolver.resolve(artifactId).orElseThrow(() -> new LookupFailedException(artifactId)); };
generatorContext.addTemplate("build", new RockerTemplate(buildTool.getBuildFileName(), buildGradle.template(
generatorContext.getApplicationType(),
generatorContext.getProject(),
coordinateResolver,
generatorContext.getFeatures(),
build
)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@import java.util.function.Function;
@import org.grails.forge.application.ApplicationType
@import org.grails.forge.build.dependencies.CoordinateResolver
@import org.grails.forge.application.Project
@import org.grails.forge.build.dependencies.Coordinate
@import org.grails.forge.build.gradle.GradleBuild
@import org.grails.forge.build.gradle.GradleDsl
@import org.grails.forge.build.gradle.GradlePlugin
Expand All @@ -10,10 +13,13 @@
@args (
ApplicationType applicationType,
Project project,
Function<String, Coordinate> coordinateResolver,
Features features,
GradleBuild gradleBuild
)

@seleniumVersion => { @coordinateResolver.apply("selenium-api").getVersion() }

@for (String importLine : gradleBuild.getPluginsImports()) {
@(importLine)
}
Expand All @@ -38,6 +44,13 @@ repositories {
configurations {
all {
resolutionStrategy.force 'org.codehaus.groovy:groovy-xml:3.0.13'
@if (features.contains("geb")) {
resolutionStrategy.eachDependency { DependencyResolveDetails details->
if (details.requested.group == 'org.seleniumhq.selenium') {
details.useVersion('@seleniumVersion')
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@
import org.grails.forge.feature.*;
import org.grails.forge.feature.test.template.groovyJunit;
import org.grails.forge.feature.test.template.webdriverBinariesPlugin;
import org.grails.forge.options.DefaultTestRockerModelProvider;
import org.grails.forge.options.Options;
import org.grails.forge.options.TestFramework;
import org.grails.forge.options.TestRockerModelProvider;
import org.grails.forge.options.*;
import org.grails.forge.template.RockerTemplate;
import org.grails.forge.template.RockerWritable;
import org.grails.forge.feature.test.template.gebConfig;

import java.util.Set;
import java.util.stream.Stream;
Expand Down Expand Up @@ -123,6 +121,10 @@ public void apply(GeneratorContext generatorContext) {
.groupId("org.seleniumhq.selenium")
.lookupArtifactId("selenium-firefox-driver")
.testRuntime());
generatorContext.addDependency(Dependency.builder()
.groupId("org.seleniumhq.selenium")
.lookupArtifactId("selenium-safari-driver")
.testRuntime());

TestFramework testFramework = generatorContext.getTestFramework();
String integrationTestSourcePath = generatorContext.getIntegrationTestSourcePath("/{packagePath}/{className}");
Expand All @@ -132,6 +134,8 @@ public void apply(GeneratorContext generatorContext) {
generatorContext.addTemplate("applicationTest",
new RockerTemplate(integrationTestSourcePath, provider.findModel(generatorContext.getLanguage(), testFramework))
);
generatorContext.addTemplate("gebConfig",
new RockerTemplate("src/integration-test/resources/GebConfig.groovy", gebConfig.template(project)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@import org.grails.forge.application.Project

@args (
Project project
)

import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions
import org.openqa.selenium.safari.SafariDriver

environments {

// You need to configure in Safari -> Develop -> Allowed Remote Automation
safari {
driver = { new SafariDriver() }
}

// run via “./gradlew -Dgeb.env=chrome iT”
chrome {
driver = { new ChromeDriver() }
}

// run via “./gradlew -Dgeb.env=chromeHeadless iT”
chromeHeadless {
driver = {
ChromeOptions o = new ChromeOptions()
o.addArguments('headless')
new ChromeDriver(o)
}
}

// run via “./gradlew -Dgeb.env=firefoxHeadless iT”
firefoxHeadless {
driver = {
FirefoxOptions o = new FirefoxOptions()
o.addArguments('-headless')
new FirefoxDriver(o)
}
}

// run via “./gradlew -Dgeb.env=firefox iT”
firefox {
driver = { new FirefoxDriver() }
}
}
5 changes: 5 additions & 0 deletions grails-forge-core/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
<artifactId>selenium-chrome-driver</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-safari-driver</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ import org.grails.forge.application.ApplicationType
import org.grails.forge.application.OperatingSystem
import org.grails.forge.application.Project
import org.grails.forge.application.generator.GeneratorContext
import org.grails.forge.build.dependencies.Coordinate
import org.grails.forge.build.dependencies.CoordinateResolver
import org.grails.forge.build.dependencies.LookupFailedException
import org.grails.forge.build.gradle.GradleBuild
import org.grails.forge.build.gradle.GradleBuildCreator
import org.grails.forge.feature.Features
import org.grails.forge.feature.build.gradle.templates.buildGradle
import org.grails.forge.feature.build.gradle.templates.buildSrcBuildGradle
import org.grails.forge.fixture.ContextFixture
import org.grails.forge.fixture.ProjectFixture
import org.grails.forge.options.BuildTool
import org.grails.forge.options.GormImpl
import org.grails.forge.options.JdkVersion
import org.grails.forge.options.Language
import org.grails.forge.options.Options
import org.grails.forge.options.ServletImpl
import org.grails.forge.options.TestFramework
import org.grails.forge.options.*

import java.util.function.Function

class BuildBuilder implements ProjectFixture, ContextFixture {

Expand Down Expand Up @@ -115,7 +113,9 @@ class BuildBuilder implements ProjectFixture, ContextFixture {

if (buildTool.isGradle()) {
GradleBuild build = gradleBuild(options, features, project, type)
return buildGradle.template(type, project, features, build).render().toString()
CoordinateResolver resolver = ctx.getBean(CoordinateResolver);
Function<String, Coordinate> coordinateResolver = (artifactId) -> resolver.resolve(artifactId).orElseThrow(() -> new LookupFailedException(artifactId))
return buildGradle.template(type, project, coordinateResolver, features, build).render().toString()
}
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ class GebSpec extends ApplicationContextSpec implements CommandOutputFixture {
buildGradle.contains("testRuntimeOnly(\"org.seleniumhq.selenium:selenium-firefox-driver:4.10.0\")")
}

void "test GebConfig.groovy file is present"() {
given:
final def output = generate(ApplicationType.WEB, new Options(Language.GROOVY, TestFramework.SPOCK, BuildTool.GRADLE, JdkVersion.JDK_11))

expect:
output.containsKey('src/integration-test/resources/GebConfig.groovy')
}

void "test build.gradle contains logic to force selenium version"() {
given:
final def output = generate(ApplicationType.WEB, new Options(Language.GROOVY, TestFramework.SPOCK, BuildTool.GRADLE, JdkVersion.JDK_11))
final def buildGradle = output["build.gradle"]

expect:
buildGradle.contains("if (details.requested.group == 'org.seleniumhq.selenium') {\n" +
" details.useVersion('4.10.0')\n" +
" }")
}

@Unroll
void "test feature geb is not supported for #applicationType application"(ApplicationType applicationType) {
when:
Expand Down

0 comments on commit 690965e

Please sign in to comment.