From 690965e942bcf0dcf270b29a5f4f8a768de8fae3 Mon Sep 17 00:00:00 2001 From: Puneet Behl Date: Thu, 6 Jul 2023 22:10:57 +0530 Subject: [PATCH] Update Geb Feature --- .../forge/feature/build/gradle/Gradle.java | 5 ++ .../gradle/templates/buildGradle.rocker.raw | 13 +++++ .../org/grails/forge/feature/test/Geb.java | 12 +++-- .../test/template/gebConfig.rocker.raw | 47 +++++++++++++++++++ grails-forge-core/src/main/resources/pom.xml | 5 ++ .../org/grails/forge/BuildBuilder.groovy | 16 +++---- .../grails/forge/feature/test/GebSpec.groovy | 19 ++++++++ 7 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 grails-forge-core/src/main/java/org/grails/forge/feature/test/template/gebConfig.rocker.raw diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/Gradle.java b/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/Gradle.java index e8647dfa..34b78178 100644 --- a/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/Gradle.java +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/Gradle.java @@ -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; @@ -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 { @@ -79,9 +81,12 @@ public void apply(GeneratorContext generatorContext) { build ))); + + final Function 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 ))); diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/templates/buildGradle.rocker.raw b/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/templates/buildGradle.rocker.raw index 5f2f76f9..8aec0fb4 100644 --- a/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/templates/buildGradle.rocker.raw +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/build/gradle/templates/buildGradle.rocker.raw @@ -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 @@ -10,10 +13,13 @@ @args ( ApplicationType applicationType, Project project, +Function coordinateResolver, Features features, GradleBuild gradleBuild ) +@seleniumVersion => { @coordinateResolver.apply("selenium-api").getVersion() } + @for (String importLine : gradleBuild.getPluginsImports()) { @(importLine) } @@ -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') + } + } + } } } diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/test/Geb.java b/grails-forge-core/src/main/java/org/grails/forge/feature/test/Geb.java index 95c6743b..c72cc334 100644 --- a/grails-forge-core/src/main/java/org/grails/forge/feature/test/Geb.java +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/test/Geb.java @@ -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; @@ -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}"); @@ -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))); } } } diff --git a/grails-forge-core/src/main/java/org/grails/forge/feature/test/template/gebConfig.rocker.raw b/grails-forge-core/src/main/java/org/grails/forge/feature/test/template/gebConfig.rocker.raw new file mode 100644 index 00000000..4dc06ea9 --- /dev/null +++ b/grails-forge-core/src/main/java/org/grails/forge/feature/test/template/gebConfig.rocker.raw @@ -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() } + } +} \ No newline at end of file diff --git a/grails-forge-core/src/main/resources/pom.xml b/grails-forge-core/src/main/resources/pom.xml index 9a3db802..a5dfa6d4 100644 --- a/grails-forge-core/src/main/resources/pom.xml +++ b/grails-forge-core/src/main/resources/pom.xml @@ -112,6 +112,11 @@ selenium-chrome-driver 4.10.0 + + org.seleniumhq.selenium + selenium-safari-driver + 4.10.0 + org.seleniumhq.selenium selenium-firefox-driver diff --git a/grails-forge-core/src/test/groovy/org/grails/forge/BuildBuilder.groovy b/grails-forge-core/src/test/groovy/org/grails/forge/BuildBuilder.groovy index 72ce03f0..fa72fc6e 100644 --- a/grails-forge-core/src/test/groovy/org/grails/forge/BuildBuilder.groovy +++ b/grails-forge-core/src/test/groovy/org/grails/forge/BuildBuilder.groovy @@ -6,7 +6,9 @@ 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 @@ -14,13 +16,9 @@ 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 { @@ -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 coordinateResolver = (artifactId) -> resolver.resolve(artifactId).orElseThrow(() -> new LookupFailedException(artifactId)) + return buildGradle.template(type, project, coordinateResolver, features, build).render().toString() } null } diff --git a/grails-forge-core/src/test/groovy/org/grails/forge/feature/test/GebSpec.groovy b/grails-forge-core/src/test/groovy/org/grails/forge/feature/test/GebSpec.groovy index 879c8525..5caa11ca 100644 --- a/grails-forge-core/src/test/groovy/org/grails/forge/feature/test/GebSpec.groovy +++ b/grails-forge-core/src/test/groovy/org/grails/forge/feature/test/GebSpec.groovy @@ -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: