Implement a basic PoC of an injectable WebDriver (#30378)

Closes #30376

Signed-off-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
Jon Koops 2024-06-21 14:16:22 +02:00 committed by GitHub
parent 7af186b3b6
commit f3a138cd74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 173 additions and 4 deletions

View file

@ -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<String> adminUserName() {
return Optional.empty();
}
@Override
public Optional<String> adminUserPassword() {
return Optional.empty();
}
}

View file

@ -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();
}
}

View file

@ -44,6 +44,18 @@
<groupId>org.keycloak</groupId> <groupId>org.keycloak</groupId>
<artifactId>keycloak-junit5</artifactId> <artifactId>keycloak-junit5</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>4.21.0</version>
<type>pom</type>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -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"));
}
}

View file

@ -13,8 +13,8 @@ public class EmbeddedKeycloakTestServer implements KeycloakTestServer {
@Override @Override
public void start(KeycloakTestServerConfig serverConfig) { public void start(KeycloakTestServerConfig serverConfig) {
System.setProperty("keycloakAdmin", "admin"); serverConfig.adminUserName().ifPresent(username -> System.setProperty("keycloakAdmin", username));
System.setProperty("keycloakAdminPassword", "admin"); serverConfig.adminUserPassword().ifPresent(password -> System.setProperty("keycloakAdminPassword", password));
List<String> rawOptions = new LinkedList<>(); List<String> rawOptions = new LinkedList<>();
rawOptions.add("start-dev"); rawOptions.add("start-dev");

View file

@ -2,6 +2,7 @@ package org.keycloak.test.framework.server;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
public interface KeycloakTestServerConfig { public interface KeycloakTestServerConfig {
@ -14,4 +15,12 @@ public interface KeycloakTestServerConfig {
return Collections.emptySet(); return Collections.emptySet();
} }
default Optional<String> adminUserName() {
return Optional.of("admin");
}
default Optional<String> adminUserPassword() {
return Optional.of("admin");
}
} }

View file

@ -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<WebDriver, TestWebDriver> {
@Override
public Class<TestWebDriver> getAnnotationClass() {
return TestWebDriver.class;
}
@Override
public Class<WebDriver> getValueType() {
return WebDriver.class;
}
@Override
public InstanceWrapper<WebDriver, TestWebDriver> 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<WebDriver, TestWebDriver> a, InstanceWrapper<WebDriver, TestWebDriver> b) {
return true;
}
@Override
public void close(WebDriver instance) {
instance.close();
}
}

View file

@ -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 { }

View file

@ -1,4 +1,5 @@
org.keycloak.test.framework.admin.KeycloakAdminClientSupplier org.keycloak.test.framework.admin.KeycloakAdminClientSupplier
org.keycloak.test.framework.server.KeycloakTestServerSupplier
org.keycloak.test.framework.realm.RealmSupplier
org.keycloak.test.framework.realm.ClientSupplier org.keycloak.test.framework.realm.ClientSupplier
org.keycloak.test.framework.realm.RealmSupplier
org.keycloak.test.framework.server.KeycloakTestServerSupplier
org.keycloak.test.framework.webdriver.FirefoxWebDriverSupplier