فهرست منبع

Use Playwright for CAS tests

This is a try to fix the tests that are not reliable when running on CI
Marcus Hert Da Coregio 1 سال پیش
والد
کامیت
4ab8b843b8

+ 1 - 5
servlet/spring-boot/java/cas/login/build.gradle

@@ -18,14 +18,10 @@ dependencies {
     implementation 'org.springframework.security:spring-security-cas'
     implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
 
-    testImplementation 'net.sourceforge.htmlunit:htmlunit'
     testImplementation 'org.springframework.boot:spring-boot-starter-test'
     testImplementation 'org.springframework.security:spring-security-test'
     testImplementation "org.testcontainers:junit-jupiter"
-    testImplementation 'com.codeborne:selenide:6.12.4'
-    testImplementation 'org.seleniumhq.selenium:selenium-chrome-driver'
-    testImplementation 'org.seleniumhq.selenium:selenium-java'
-    testImplementation 'io.github.bonigarcia:webdrivermanager:5.6.4'
+    testImplementation 'com.microsoft.playwright:playwright:1.42.0'
 }
 
 tasks.withType(Test).configureEach {

+ 32 - 29
servlet/spring-boot/java/cas/login/src/test/java/cas/example/CasLoginApplicationTests.java

@@ -16,13 +16,12 @@
 
 package cas.example;
 
-import com.codeborne.selenide.Configuration;
-import com.codeborne.selenide.Selenide;
-import io.github.bonigarcia.wdm.WebDriverManager;
+import com.microsoft.playwright.Browser;
+import com.microsoft.playwright.Page;
+import com.microsoft.playwright.Playwright;
 import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.openqa.selenium.By;
 import org.testcontainers.containers.BindMode;
 import org.testcontainers.containers.GenericContainer;
 import org.testcontainers.containers.wait.strategy.Wait;
@@ -54,6 +53,10 @@ class CasLoginApplicationTests {
 				BindMode.READ_WRITE)
 		.waitingFor(Wait.forLogMessage(".*Ready to process requests.*", 1));
 
+	Playwright playwright;
+
+	Browser browser;
+
 	@DynamicPropertySource
 	static void casProperties(DynamicPropertyRegistry registry) {
 		String casUrl = String.format("http://%s:%s/cas", casServer.getHost(), casServer.getMappedPort(8080));
@@ -62,42 +65,42 @@ class CasLoginApplicationTests {
 		registry.add("cas.logout.url", () -> casUrl + "/logout");
 	}
 
-	@BeforeAll
-	static void setUp() {
-		WebDriverManager.chromedriver()
-			.clearDriverCache()
-			.clearResolutionCache()
-			.browserInDocker()
-			.browserVersion("114")
-			.setup();
-		Configuration.headless = true;
+	@BeforeEach
+	void setUp() {
+		this.playwright = Playwright.create();
+		this.browser = this.playwright.chromium().launch();
 	}
 
 	@AfterEach
 	void setup() {
-		Selenide.closeWindow();
+		this.browser.close();
+		this.playwright.close();
 	}
 
 	@Test
 	void login() {
-		doLogin();
-		String lead = Selenide.$(By.className("lead")).text();
-		assertThat(lead).isEqualTo("You are successfully logged in as casuser");
-	}
-
-	private void doLogin() {
-		Selenide.open("http://localhost:" + this.port);
-		Selenide.$(By.name("username")).setValue("casuser");
-		Selenide.$(By.name("password")).setValue("Mellon");
-		Selenide.$(By.name("submitBtn")).click();
+		try (Page page = doLogin()) {
+			String lead = page.locator(".lead").textContent();
+			assertThat(lead).isEqualTo("You are successfully logged in as casuser");
+		}
 	}
 
 	@Test
 	void loginAndLogout() {
-		doLogin();
-		Selenide.$(By.id("rp_logout_button")).click();
-		String logoutMsg = Selenide.$(By.id("logout-msg")).text();
-		assertThat(logoutMsg).isEqualTo("You are successfully logged out of the app, but not CAS");
+		try (Page page = doLogin()) {
+			page.click("#rp_logout_button");
+			String logoutMsg = page.locator("#logout-msg").textContent();
+			assertThat(logoutMsg).isEqualTo("You are successfully logged out of the app, but not CAS");
+		}
+	}
+
+	private Page doLogin() {
+		Page page = this.browser.newPage();
+		page.navigate("http://localhost:" + this.port);
+		page.fill("//input[@name='username']", "casuser");
+		page.fill("//input[@name='password']", "Mellon");
+		page.click("//button[@name='submitBtn']");
+		return page;
 	}
 
 }