我正在使用Selenide框架和Java编写一些自动化测试。我的测试之一是文件下载。点击下载按钮后Chrome给出消息“这种类型的文件会伤害您的计算机。你想保留file. xml吗?”。是否可以禁用此功能?我尝试了下面的代码,但它不适合我。
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("safebrowsing.enabled", "true");
options.setExperimentalOption("prefs", prefs);
ChromeDriver chromeDriver = new ChromeDriver(options);
Configuration.browser = "chrome";
我找到了解决方案。我只是创建了实现WebDriverProvider的Class
public class CustomWebDriverProviderChrome implements WebDriverProvider {
public WebDriver createDriver(DesiredCapabilities capabilities) {
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.prompt_for_download", "true");
chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(cap);
}
}