Skip to content

Commit 45db0c0

Browse files
authored
[java] Fix failing java tests (#17278)
* enable BrowserCommandsTest for Firefox Seems that `setDownloadBehavior` has been implemented in Firefox meanwhile. * ignore SetNetworkConditionsTest for FireFox beta Seems that `setNetworkConditions` has been enabled in FireFox beat meanwhile (but not in Firefox yet). But we don't have annotation like `@NotYetImplemented(FIREFOX-BETA)`. * enable showing alerts in Firefox Seems that Firefox behavior with alerts was changed. Without "unhandledPromptBehavior" capability, command `wait.until(alertIsPresent())` is always failing in FireFox. * improve `WebScriptTest` tests for `unpinScript` and `addDomMutationHandler` instead of just waiting to latch, collect the logs to a list and assert this list. It gives much clearer assertion error. * fixup: browser behavior in ff * fixup: alerts * ignore SetNetworkConditionsTest on "firefox-beta-remote"
1 parent 3f8e8c2 commit 45db0c0

File tree

5 files changed

+77
-61
lines changed

5 files changed

+77
-61
lines changed

.skipped-tests

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
-//java/test/org/openqa/selenium/bidi/browsingcontext:BrowsingContextInspectorTest-firefox-beta-remote
2525
-//java/test/org/openqa/selenium/bidi/browsingcontext:BrowsingContextTest-firefox-beta
2626
-//java/test/org/openqa/selenium/bidi/browsingcontext:BrowsingContextTest-firefox-beta-remote
27+
-//java/test/org/openqa/selenium/bidi/emulation:SetNetworkConditionsTest-firefox-beta
28+
-//java/test/org/openqa/selenium/bidi/emulation:SetNetworkConditionsTest-firefox-beta-remote
2729
-//rb/spec/integration/selenium/webdriver/bidi:browsing_context-firefox-beta-bidi

java/test/org/openqa/selenium/UnexpectedAlertBehaviorTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
package org.openqa.selenium;
1919

2020
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21+
import static org.openqa.selenium.UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY;
2122
import static org.openqa.selenium.UnexpectedAlertBehaviour.IGNORE;
2223
import static org.openqa.selenium.WaitingConditions.elementTextToEqual;
2324
import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR;
2425
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
2526
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
27+
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
2628
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2729

2830
import java.time.Duration;
@@ -58,7 +60,7 @@ public void canSilentlyAcceptUnhandledAlert() {
5860
@Ignore(value = EDGE, reason = "Unstable Chrome behavior")
5961
@NoDriverBeforeTest
6062
public void canDismissUnhandledAlert() {
61-
runScenarioWithUnhandledAlert(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY, "null", false);
63+
runScenarioWithUnhandledAlert(DISMISS_AND_NOTIFY, "null", false);
6264
}
6365

6466
@Test
@@ -72,6 +74,7 @@ public void canSilentlyDismissUnhandledAlert() {
7274
@Test
7375
@Ignore(value = CHROME, reason = "Chrome uses IGNORE mode by default")
7476
@Ignore(value = EDGE, reason = "Edge uses IGNORE mode by default")
77+
@Ignore(value = FIREFOX, reason = "Browser#FIREFOX sets IGNORE mode by default")
7578
@NoDriverBeforeTest
7679
public void canDismissUnhandledAlertsByDefault() {
7780
runScenarioWithUnhandledAlert(null, "null", false);

java/test/org/openqa/selenium/WebScriptTest.java

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,37 @@
1717

1818
package org.openqa.selenium;
1919

20+
import static java.time.Instant.ofEpochMilli;
21+
import static java.time.ZoneId.systemDefault;
2022
import static java.util.concurrent.TimeUnit.SECONDS;
2123
import static org.assertj.core.api.Assertions.assertThat;
2224
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2325
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
2426

2527
import java.time.Duration;
28+
import java.time.format.DateTimeFormatter;
29+
import java.util.List;
2630
import java.util.concurrent.CompletableFuture;
31+
import java.util.concurrent.CopyOnWriteArrayList;
2732
import java.util.concurrent.CountDownLatch;
2833
import java.util.concurrent.ExecutionException;
2934
import java.util.concurrent.TimeUnit;
3035
import java.util.concurrent.TimeoutException;
31-
import java.util.concurrent.atomic.AtomicReference;
3236
import java.util.function.Consumer;
33-
import org.assertj.core.api.Assertions;
3437
import org.junit.jupiter.api.Test;
3538
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
3639
import org.openqa.selenium.bidi.log.JavascriptLogEntry;
3740
import org.openqa.selenium.bidi.log.LogLevel;
3841
import org.openqa.selenium.remote.DomMutation;
3942
import org.openqa.selenium.remote.RemoteWebDriver;
43+
import org.openqa.selenium.remote.Script;
4044
import org.openqa.selenium.support.ui.WebDriverWait;
4145
import org.openqa.selenium.testing.JupiterTestBase;
4246
import org.openqa.selenium.testing.NeedsFreshDriver;
4347

4448
class WebScriptTest extends JupiterTestBase {
4549

46-
String page;
50+
private String page;
4751

4852
@Test
4953
@NeedsFreshDriver
@@ -182,58 +186,52 @@ void canAddMultipleHandlers() throws ExecutionException, InterruptedException, T
182186

183187
@Test
184188
@NeedsFreshDriver
185-
void canAddDomMutationHandler() throws InterruptedException {
186-
AtomicReference<DomMutation> seen = new AtomicReference<>();
187-
CountDownLatch latch = new CountDownLatch(1);
189+
void canAddDomMutationHandler() {
190+
List<String> mutations = new CopyOnWriteArrayList<>();
188191

189-
((RemoteWebDriver) driver)
190-
.script()
191-
.addDomMutationHandler(
192-
mutation -> {
193-
seen.set(mutation);
194-
latch.countDown();
195-
});
192+
Script script = ((RemoteWebDriver) driver).script();
193+
script.addDomMutationHandler(mutationHandler(mutations));
196194

197195
driver.get(pages.dynamicPage);
196+
triggerDomMutation();
198197

199-
WebElement reveal = driver.findElement(By.id("reveal"));
200-
reveal.click();
201-
WebElement revealed = driver.findElement(By.id("revealed"));
202-
203-
new WebDriverWait(driver, Duration.ofSeconds(10)).until(visibilityOf(revealed));
204-
205-
Assertions.assertThat(latch.await(10, SECONDS)).isTrue();
206-
assertThat(seen.get().getAttributeName()).isEqualTo("style");
207-
assertThat(seen.get().getCurrentValue()).isEmpty();
208-
assertThat(seen.get().getOldValue()).isEqualTo("display:none;");
198+
assertThat(mutations).isNotEmpty();
199+
assertThat(lastOf(mutations)).isEqualTo("style: 'display:none;' -> ''");
209200
}
210201

211202
@Test
212203
@NeedsFreshDriver
213-
void canRemoveDomMutationHandler() throws InterruptedException {
214-
AtomicReference<DomMutation> seen = new AtomicReference<>();
215-
CountDownLatch latch = new CountDownLatch(1);
216-
217-
long id =
218-
((RemoteWebDriver) driver)
219-
.script()
220-
.addDomMutationHandler(
221-
mutation -> {
222-
seen.set(mutation);
223-
latch.countDown();
224-
});
204+
void canRemoveDomMutationHandler() {
205+
List<String> mutations = new CopyOnWriteArrayList<>();
206+
Script script = ((RemoteWebDriver) driver).script();
207+
long id = script.addDomMutationHandler(mutationHandler(mutations));
225208

226209
driver.get(pages.dynamicPage);
210+
triggerDomMutation();
211+
assertThat(mutations).isNotEmpty();
212+
213+
script.removeDomMutationHandler(id);
227214

228-
((RemoteWebDriver) driver).script().removeDomMutationHandler(id);
215+
mutations.clear();
216+
driver.get(pages.dynamicPage);
217+
triggerDomMutation();
218+
assertThat(mutations).isEmpty();
219+
}
229220

221+
private void triggerDomMutation() {
230222
WebElement reveal = driver.findElement(By.id("reveal"));
231223
reveal.click();
232224
WebElement revealed = driver.findElement(By.id("revealed"));
233-
234225
new WebDriverWait(driver, Duration.ofSeconds(10)).until(visibilityOf(revealed));
226+
}
235227

236-
Assertions.assertThat(latch.await(10, SECONDS)).isFalse();
228+
private static Consumer<DomMutation> mutationHandler(List<String> mutations) {
229+
return mutation -> {
230+
mutations.add(
231+
String.format(
232+
"%s: '%s' -> '%s'",
233+
mutation.getAttributeName(), mutation.getOldValue(), mutation.getCurrentValue()));
234+
};
237235
}
238236

239237
@Test
@@ -257,27 +255,46 @@ void canPinScript() throws ExecutionException, InterruptedException, TimeoutExce
257255

258256
@Test
259257
@NeedsFreshDriver
260-
void canUnpinScript() {
261-
CountDownLatch latch = new CountDownLatch(2);
258+
void canUnpinScript() throws InterruptedException {
259+
List<String> logs = new CopyOnWriteArrayList<>();
260+
CountDownLatch latch = new CountDownLatch(1);
261+
262+
Script script = ((RemoteWebDriver) driver).script();
263+
String pinnedScript = script.pin("() => { console.log('Hello!'); }");
262264

263-
String pinnedScript =
264-
((RemoteWebDriver) driver).script().pin("() => { console.log('Hello!'); }");
265+
DateTimeFormatter formatter =
266+
DateTimeFormatter.ofPattern("HH:mm:ss:SSS").withZone(systemDefault());
265267

266268
long id =
267-
((RemoteWebDriver) driver)
268-
.script()
269-
.addConsoleMessageHandler(consoleLogEntry -> latch.countDown());
269+
script.addConsoleMessageHandler(
270+
log -> {
271+
String time = formatter.format(ofEpochMilli(log.getTimestamp()));
272+
String message = String.format("%s %s", log.getText(), time);
273+
logs.add(message);
274+
latch.countDown();
275+
});
270276

271-
page = appServer.whereIs("/bidi/logEntryAdded.html");
277+
try {
278+
page = appServer.whereIs("/bidi/logEntryAdded.html");
279+
assertThat(logs).hasSize(0);
272280

273-
driver.get(page);
281+
driver.get(page);
282+
assertThat(latch.await(10, SECONDS)).isTrue();
274283

275-
((RemoteWebDriver) driver).script().unpin(pinnedScript);
284+
assertThat(logs).as("Chrome logs once, FireFox logs twice").isNotEmpty();
285+
assertThat(logs.get(0)).startsWith("Hello!");
276286

277-
driver.get(page);
287+
script.unpin(pinnedScript);
278288

279-
assertThat(latch.getCount()).isEqualTo(1L);
289+
logs.clear();
290+
driver.get(page);
291+
assertThat(logs).as("Script has been unpinned, no logs anymore.").isEmpty();
292+
} finally {
293+
script.removeConsoleMessageHandler(id);
294+
}
295+
}
280296

281-
((RemoteWebDriver) driver).script().removeConsoleMessageHandler(id);
297+
private static <T> T lastOf(List<T> list) {
298+
return list.get(list.size() - 1);
282299
}
283300
}

java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static org.assertj.core.api.Assertions.assertThat;
2323
import static org.openqa.selenium.bidi.browser.DownloadBehavior.allowed;
2424
import static org.openqa.selenium.bidi.browser.DownloadBehavior.denied;
25-
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
26-
import static org.openqa.selenium.testing.drivers.Browser.detect;
2725

2826
import java.io.IOException;
2927
import java.io.UncheckedIOException;
@@ -47,7 +45,6 @@
4745
import org.openqa.selenium.support.ui.WebDriverWait;
4846
import org.openqa.selenium.testing.JupiterTestBase;
4947
import org.openqa.selenium.testing.NeedsFreshDriver;
50-
import org.openqa.selenium.testing.NotYetImplemented;
5148

5249
class BrowserCommandsTest extends JupiterTestBase {
5350

@@ -62,9 +59,7 @@ final void setUp() {
6259

6360
@AfterEach
6461
final void resetDownloadBehavior() {
65-
if (detect() != FIREFOX) {
66-
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(null));
67-
}
62+
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(null));
6863
}
6964

7065
@AfterEach
@@ -133,7 +128,6 @@ void canGetClientWindows() {
133128

134129
@Test
135130
@NeedsFreshDriver
136-
@NotYetImplemented(FIREFOX)
137131
void canSetDownloadBehaviorAllowed() {
138132
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(allowed(tmpDir)));
139133

@@ -148,7 +142,6 @@ void canSetDownloadBehaviorAllowed() {
148142

149143
@Test
150144
@NeedsFreshDriver
151-
@NotYetImplemented(FIREFOX)
152145
void canSetDownloadBehaviorDenied() throws InterruptedException {
153146
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(denied()));
154147

@@ -168,7 +161,6 @@ void canSetDownloadBehaviorDenied() throws InterruptedException {
168161

169162
@Test
170163
@NeedsFreshDriver
171-
@NotYetImplemented(FIREFOX)
172164
void canSetDownloadBehaviorWithUserContext() throws InterruptedException {
173165
String userContext = browser.createUserContext();
174166

java/test/org/openqa/selenium/testing/drivers/Browser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium.testing.drivers;
1919

20+
import static org.openqa.selenium.UnexpectedAlertBehaviour.IGNORE;
2021
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;
2122
import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR;
2223

@@ -135,6 +136,7 @@ public Capabilities getCapabilities() {
135136
@Override
136137
public Capabilities getCapabilities() {
137138
FirefoxOptions options = new FirefoxOptions().configureFromEnv();
139+
options.setCapability(UNHANDLED_PROMPT_BEHAVIOUR, IGNORE);
138140

139141
resolveDriverPath("webdriver.gecko.driver");
140142
String binary = InProject.resolveRunfilesPath(System.getProperty("webdriver.firefox.bin"));

0 commit comments

Comments
 (0)