Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi.browser;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetDownloadBehaviorParameters {
private final Map<String, Object> map = new HashMap<>();

public SetDownloadBehaviorParameters(Boolean allowed, String destinationFolder) {
this(allowed, destinationFolder != null ? Paths.get(destinationFolder) : null);
}

public SetDownloadBehaviorParameters(Boolean allowed, Path destinationFolder) {
if (allowed == null) {
map.put("downloadBehavior", null);
} else if (allowed) {
if (destinationFolder == null) {
throw new IllegalArgumentException("destinationFolder is required when allowed is true");
}
Map<String, String> behavior = new HashMap<>();
behavior.put("type", "allowed");
behavior.put("destinationFolder", destinationFolder.toAbsolutePath().toString());
map.put("downloadBehavior", behavior);
} else {
if (destinationFolder != null) {
throw new IllegalArgumentException(
"destinationFolder should not be provided when allowed is false");
}
Map<String, String> behavior = new HashMap<>();
behavior.put("type", "denied");
map.put("downloadBehavior", behavior);
}
}

public SetDownloadBehaviorParameters userContexts(List<String> userContexts) {
map.put("userContexts", userContexts);
return this;
}

public Map<String, Object> toMap() {
return map;
}
}
5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.browser.ClientWindowInfo;
import org.openqa.selenium.bidi.browser.SetDownloadBehaviorParameters;
import org.openqa.selenium.json.JsonInput;

public class Browser {
Expand Down Expand Up @@ -90,4 +91,8 @@ public void removeUserContext(String userContext) {
public List<ClientWindowInfo> getClientWindows() {
return bidi.send(new Command<>("browser.getClientWindows", Map.of(), clientWindowsInfoMapper));
}

public void setDownloadBehavior(SetDownloadBehaviorParameters parameters) {
bidi.send(new Command<>("browser.setDownloadBehavior", parameters.toMap()));
}
}
173 changes: 172 additions & 1 deletion java/test/org/openqa/selenium/bidi/browser/BrowserCommandsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,28 @@

package org.openqa.selenium.bidi.browser;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters;
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
import org.openqa.selenium.bidi.module.Browser;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.NeedsFreshDriver;
import org.openqa.selenium.testing.NotYetImplemented;

class BrowserCommandsTest extends JupiterTestBase {

Expand Down Expand Up @@ -91,4 +105,161 @@ void canGetClientWindows() {
assertThat(windowInfo.getHeight()).isGreaterThan(0);
assertThat(windowInfo.isActive()).isIn(true, false);
}

@Test
@NeedsFreshDriver
@NotYetImplemented(FIREFOX)
void canSetDownloadBehaviorAllowed() throws Exception {
Path tmpDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("downloads", "test").toPath();

try {
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(true, tmpDir));

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
String url = appServer.whereIs("downloads/download.html");
context.navigate(url, ReadinessState.COMPLETE);

driver.findElement(By.id("file-1")).click();

new WebDriverWait(driver, Duration.ofSeconds(5))
.until(
d -> {
try {
return Files.list(tmpDir)
.anyMatch(path -> path.getFileName().toString().equals("file_1.txt"));
} catch (Exception e) {
return false;
}
});

List<String> fileNames =
Files.list(tmpDir)
.map(path -> path.getFileName().toString())
.collect(Collectors.toList());
assertThat(fileNames).contains("file_1.txt");
} finally {
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(null, (Path) null));
TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(tmpDir.toFile());
}
}

@Test
@NeedsFreshDriver
@NotYetImplemented(FIREFOX)
void canSetDownloadBehaviorDenied() throws Exception {
Path tmpDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("downloads", "test").toPath();

try {
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(false, (Path) null));

BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle());
String url = appServer.whereIs("downloads/download.html");
context.navigate(url, ReadinessState.COMPLETE);

driver.findElement(By.id("file-1")).click();

// Try to wait for file to be downloaded - should timeout
try {
new WebDriverWait(driver, Duration.ofSeconds(3), Duration.ofMillis(200))
.until(
d -> {
try {
return Files.list(tmpDir).findAny().isPresent();
} catch (Exception e) {
return false;
}
});

List<String> files =
Files.list(tmpDir)
.map(path -> path.getFileName().toString())
.collect(Collectors.toList());
throw new AssertionError("A file was downloaded unexpectedly: " + files);
} catch (TimeoutException ignored) {
}
} finally {
browser.setDownloadBehavior(new SetDownloadBehaviorParameters(null, (Path) null));
TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(tmpDir.toFile());
}
}

@Test
@NeedsFreshDriver
@NotYetImplemented(FIREFOX)
void canSetDownloadBehaviorWithUserContext() throws Exception {
Path tmpDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("downloads", "test").toPath();
String userContext = browser.createUserContext();

try {
BrowsingContext bc =
new BrowsingContext(
driver, new CreateContextParameters(WindowType.WINDOW).userContext(userContext));
String contextId = bc.getId();

try {
driver.switchTo().window(contextId);

browser.setDownloadBehavior(
new SetDownloadBehaviorParameters(true, tmpDir).userContexts(List.of(userContext)));

String url = appServer.whereIs("downloads/download.html");
bc.navigate(url, ReadinessState.COMPLETE);

driver.findElement(By.id("file-1")).click();

new WebDriverWait(driver, Duration.ofSeconds(5))
.until(
d -> {
try {
return Files.list(tmpDir)
.anyMatch(path -> path.getFileName().toString().equals("file_1.txt"));
} catch (Exception e) {
return false;
}
});

List<String> files =
Files.list(tmpDir)
.map(path -> path.getFileName().toString())
.collect(Collectors.toList());
assertThat(files).contains("file_1.txt");

int initialFileCount = files.size();

browser.setDownloadBehavior(
new SetDownloadBehaviorParameters(false, (Path) null)
.userContexts(List.of(userContext)));

driver.findElement(By.id("file-2")).click();

try {
new WebDriverWait(driver, Duration.ofSeconds(3), Duration.ofMillis(200))
.until(
d -> {
try {
long fileCount = Files.list(tmpDir).count();
return fileCount > initialFileCount;
} catch (Exception e) {
return false;
}
});

List<String> filesAfter =
Files.list(tmpDir)
.map(path -> path.getFileName().toString())
.collect(Collectors.toList());
throw new AssertionError("A file was downloaded unexpectedly: " + filesAfter);
} catch (TimeoutException ignored) {
}
} finally {
browser.setDownloadBehavior(
new SetDownloadBehaviorParameters(null, (Path) null)
.userContexts(List.of(userContext)));
bc.close();
}
} finally {
browser.removeUserContext(userContext);
TemporaryFilesystem.getDefaultTmpFS().deleteTempDir(tmpDir.toFile());
}
}
}