From f3a138cd74282a049065aa0abad548b71a114cc1 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Fri, 21 Jun 2024 14:16:22 +0200 Subject: [PATCH] Implement a basic PoC of an injectable WebDriver (#30378) Closes #30376 Signed-off-by: Jon Koops --- .../NoAdminUserKeycloakTestServerConfig.java | 19 +++++++ .../keycloak/test/base/WelcomePageTest.java | 24 +++++++++ test-poc/framework/pom.xml | 12 +++++ .../test/framework/page/WelcomePage.java | 51 +++++++++++++++++++ .../server/EmbeddedKeycloakTestServer.java | 4 +- .../server/KeycloakTestServerConfig.java | 9 ++++ .../webdriver/FirefoxWebDriverSupplier.java | 43 ++++++++++++++++ .../framework/webdriver/TestWebDriver.java | 10 ++++ ...keycloak.test.framework.injection.Supplier | 5 +- 9 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 test-poc/base/src/test/java/org/keycloak/test/base/NoAdminUserKeycloakTestServerConfig.java create mode 100644 test-poc/base/src/test/java/org/keycloak/test/base/WelcomePageTest.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/page/WelcomePage.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/FirefoxWebDriverSupplier.java create mode 100644 test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/TestWebDriver.java diff --git a/test-poc/base/src/test/java/org/keycloak/test/base/NoAdminUserKeycloakTestServerConfig.java b/test-poc/base/src/test/java/org/keycloak/test/base/NoAdminUserKeycloakTestServerConfig.java new file mode 100644 index 0000000000..bf2cf27512 --- /dev/null +++ b/test-poc/base/src/test/java/org/keycloak/test/base/NoAdminUserKeycloakTestServerConfig.java @@ -0,0 +1,19 @@ +package org.keycloak.test.base; + +import org.keycloak.test.framework.server.KeycloakTestServerConfig; + +import java.util.Optional; + +public class NoAdminUserKeycloakTestServerConfig implements KeycloakTestServerConfig { + + @Override + public Optional adminUserName() { + return Optional.empty(); + } + + @Override + public Optional adminUserPassword() { + return Optional.empty(); + } + +} diff --git a/test-poc/base/src/test/java/org/keycloak/test/base/WelcomePageTest.java b/test-poc/base/src/test/java/org/keycloak/test/base/WelcomePageTest.java new file mode 100644 index 0000000000..7259249f05 --- /dev/null +++ b/test-poc/base/src/test/java/org/keycloak/test/base/WelcomePageTest.java @@ -0,0 +1,24 @@ +package org.keycloak.test.base; + +import org.junit.jupiter.api.Test; +import org.keycloak.test.framework.KeycloakIntegrationTest; +import org.keycloak.test.framework.page.WelcomePage; +import org.keycloak.test.framework.webdriver.TestWebDriver; +import org.openqa.selenium.WebDriver; + +@KeycloakIntegrationTest(config = NoAdminUserKeycloakTestServerConfig.class) +public class WelcomePageTest { + + @TestWebDriver + WebDriver driver; + + @Test + public void testCreateUser() { + final var welcomePage = new WelcomePage(driver); + welcomePage.navigateTo(); + welcomePage.fillRegistration("admin", "admin"); + welcomePage.submit(); + welcomePage.assertUserCreated(); + } + +} diff --git a/test-poc/framework/pom.xml b/test-poc/framework/pom.xml index 6bf3cd3f79..08cb7f1234 100755 --- a/test-poc/framework/pom.xml +++ b/test-poc/framework/pom.xml @@ -44,6 +44,18 @@ org.keycloak keycloak-junit5 + + org.seleniumhq.selenium + selenium-java + 4.21.0 + pom + + + org.seleniumhq.selenium + selenium-firefox-driver + 4.21.0 + pom + diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/page/WelcomePage.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/page/WelcomePage.java new file mode 100644 index 0000000000..48a821dbdf --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/page/WelcomePage.java @@ -0,0 +1,51 @@ +package org.keycloak.test.framework.page; + +import org.junit.jupiter.api.Assertions; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +public class WelcomePage { + + private final WebDriver driver; + + @FindBy(id = "username") + private WebElement usernameInput; + + @FindBy(id = "password") + private WebElement passwordInput; + + @FindBy(id = "password-confirmation") + private WebElement passwordConfirmationInput; + + @FindBy(css = "[type=submit]") + private WebElement submitButton; + + @FindBy(css = ".pf-v5-c-alert") + private WebElement pageAlert; + + public WelcomePage(WebDriver driver) { + this.driver = driver; + PageFactory.initElements(driver, this); + } + + public void navigateTo() { + driver.get("http://localhost:8080"); + } + + public void fillRegistration(String username, String password) { + usernameInput.sendKeys(username); + passwordInput.sendKeys(password); + passwordConfirmationInput.sendKeys(password); + } + + public void submit() { + submitButton.click(); + } + + public void assertUserCreated() { + Assertions.assertTrue(pageAlert.getText().contains("User created")); + } + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java index 3966a552d1..98f54299bf 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/EmbeddedKeycloakTestServer.java @@ -13,8 +13,8 @@ public class EmbeddedKeycloakTestServer implements KeycloakTestServer { @Override public void start(KeycloakTestServerConfig serverConfig) { - System.setProperty("keycloakAdmin", "admin"); - System.setProperty("keycloakAdminPassword", "admin"); + serverConfig.adminUserName().ifPresent(username -> System.setProperty("keycloakAdmin", username)); + serverConfig.adminUserPassword().ifPresent(password -> System.setProperty("keycloakAdminPassword", password)); List rawOptions = new LinkedList<>(); rawOptions.add("start-dev"); diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerConfig.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerConfig.java index fa87552fb8..009620be26 100644 --- a/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerConfig.java +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/server/KeycloakTestServerConfig.java @@ -2,6 +2,7 @@ package org.keycloak.test.framework.server; import java.util.Collections; import java.util.Map; +import java.util.Optional; import java.util.Set; public interface KeycloakTestServerConfig { @@ -14,4 +15,12 @@ public interface KeycloakTestServerConfig { return Collections.emptySet(); } + default Optional adminUserName() { + return Optional.of("admin"); + } + + default Optional adminUserPassword() { + return Optional.of("admin"); + } + } diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/FirefoxWebDriverSupplier.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/FirefoxWebDriverSupplier.java new file mode 100644 index 0000000000..eeb5a9bf62 --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/FirefoxWebDriverSupplier.java @@ -0,0 +1,43 @@ +package org.keycloak.test.framework.webdriver; + +import org.keycloak.test.framework.injection.InstanceWrapper; +import org.keycloak.test.framework.injection.LifeCycle; +import org.keycloak.test.framework.injection.Registry; +import org.keycloak.test.framework.injection.Supplier; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +public class FirefoxWebDriverSupplier implements Supplier { + + @Override + public Class getAnnotationClass() { + return TestWebDriver.class; + } + + @Override + public Class getValueType() { + return WebDriver.class; + } + + @Override + public InstanceWrapper getValue(Registry registry, TestWebDriver annotation) { + final var driver = new FirefoxDriver(); + return new InstanceWrapper<>(this, annotation, driver); + } + + @Override + public LifeCycle getLifeCycle() { + return LifeCycle.GLOBAL; + } + + @Override + public boolean compatible(InstanceWrapper a, InstanceWrapper b) { + return true; + } + + @Override + public void close(WebDriver instance) { + instance.close(); + } + +} diff --git a/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/TestWebDriver.java b/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/TestWebDriver.java new file mode 100644 index 0000000000..d1f67cabfe --- /dev/null +++ b/test-poc/framework/src/main/java/org/keycloak/test/framework/webdriver/TestWebDriver.java @@ -0,0 +1,10 @@ +package org.keycloak.test.framework.webdriver; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface TestWebDriver { } diff --git a/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier b/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier index cba6f65800..eb9d46618b 100644 --- a/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier +++ b/test-poc/framework/src/main/resources/META-INF/services/org.keycloak.test.framework.injection.Supplier @@ -1,4 +1,5 @@ org.keycloak.test.framework.admin.KeycloakAdminClientSupplier -org.keycloak.test.framework.server.KeycloakTestServerSupplier +org.keycloak.test.framework.realm.ClientSupplier org.keycloak.test.framework.realm.RealmSupplier -org.keycloak.test.framework.realm.ClientSupplier \ No newline at end of file +org.keycloak.test.framework.server.KeycloakTestServerSupplier +org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier \ No newline at end of file