|
17 | 17 |
|
18 | 18 | package org.openqa.selenium;
|
19 | 19 |
|
| 20 | +import static java.util.concurrent.TimeUnit.SECONDS; |
20 | 21 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
21 | 22 | import static org.assertj.core.api.AssertionsForClassTypes.fail;
|
| 23 | +import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf; |
22 | 24 |
|
23 |
| -import java.util.concurrent.CompletableFuture; |
24 |
| -import java.util.concurrent.ExecutionException; |
25 |
| -import java.util.concurrent.TimeUnit; |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.concurrent.*; |
26 | 27 | import java.util.concurrent.TimeoutException;
|
| 28 | +import java.util.concurrent.atomic.AtomicReference; |
27 | 29 | import java.util.function.Consumer;
|
| 30 | +import org.assertj.core.api.Assertions; |
28 | 31 | import org.junit.jupiter.api.AfterEach;
|
29 | 32 | import org.junit.jupiter.api.BeforeEach;
|
30 | 33 | import org.junit.jupiter.api.Test;
|
|
33 | 36 | import org.openqa.selenium.bidi.log.LogLevel;
|
34 | 37 | import org.openqa.selenium.environment.webserver.AppServer;
|
35 | 38 | import org.openqa.selenium.environment.webserver.NettyAppServer;
|
| 39 | +import org.openqa.selenium.remote.DomMutation; |
36 | 40 | import org.openqa.selenium.remote.RemoteWebDriver;
|
| 41 | +import org.openqa.selenium.support.ui.WebDriverWait; |
37 | 42 | import org.openqa.selenium.testing.JupiterTestBase;
|
38 | 43 |
|
39 | 44 | class WebScriptTest extends JupiterTestBase {
|
@@ -186,4 +191,58 @@ void canAddMultipleHandlers() throws ExecutionException, InterruptedException, T
|
186 | 191 | assertThat(logEntry2.getType()).isEqualTo("javascript");
|
187 | 192 | assertThat(logEntry2.getLevel()).isEqualTo(LogLevel.ERROR);
|
188 | 193 | }
|
| 194 | + |
| 195 | + @Test |
| 196 | + void canAddDomMutationHandler() throws InterruptedException { |
| 197 | + AtomicReference<DomMutation> seen = new AtomicReference<>(); |
| 198 | + CountDownLatch latch = new CountDownLatch(1); |
| 199 | + |
| 200 | + ((RemoteWebDriver) driver) |
| 201 | + .script() |
| 202 | + .addDomMutationHandler( |
| 203 | + mutation -> { |
| 204 | + seen.set(mutation); |
| 205 | + latch.countDown(); |
| 206 | + }); |
| 207 | + |
| 208 | + driver.get(pages.dynamicPage); |
| 209 | + |
| 210 | + WebElement reveal = driver.findElement(By.id("reveal")); |
| 211 | + reveal.click(); |
| 212 | + WebElement revealed = driver.findElement(By.id("revealed")); |
| 213 | + |
| 214 | + new WebDriverWait(driver, Duration.ofSeconds(10)).until(visibilityOf(revealed)); |
| 215 | + |
| 216 | + Assertions.assertThat(latch.await(10, SECONDS)).isTrue(); |
| 217 | + assertThat(seen.get().getAttributeName()).isEqualTo("style"); |
| 218 | + assertThat(seen.get().getCurrentValue()).isEmpty(); |
| 219 | + assertThat(seen.get().getOldValue()).isEqualTo("display:none;"); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + void canRemoveDomMutationHandler() throws InterruptedException { |
| 224 | + AtomicReference<DomMutation> seen = new AtomicReference<>(); |
| 225 | + CountDownLatch latch = new CountDownLatch(1); |
| 226 | + |
| 227 | + long id = |
| 228 | + ((RemoteWebDriver) driver) |
| 229 | + .script() |
| 230 | + .addDomMutationHandler( |
| 231 | + mutation -> { |
| 232 | + seen.set(mutation); |
| 233 | + latch.countDown(); |
| 234 | + }); |
| 235 | + |
| 236 | + driver.get(pages.dynamicPage); |
| 237 | + |
| 238 | + ((RemoteWebDriver) driver).script().removeDomMutationHandler(id); |
| 239 | + |
| 240 | + WebElement reveal = driver.findElement(By.id("reveal")); |
| 241 | + reveal.click(); |
| 242 | + WebElement revealed = driver.findElement(By.id("revealed")); |
| 243 | + |
| 244 | + new WebDriverWait(driver, Duration.ofSeconds(10)).until(visibilityOf(revealed)); |
| 245 | + |
| 246 | + Assertions.assertThat(latch.await(10, SECONDS)).isFalse(); |
| 247 | + } |
189 | 248 | }
|
0 commit comments