Selenium multiple desired capabilities settings - java

I am trying to set multiple desired capability,for an instance of a chrome driver object in selenium.
I would like the browser to first
set the download location of files, then to disable the pdf viewer plugin in Chrome browser. Can anyone assist?
Code snippet for disabling PDF viewer plugin:
DesiredCapabilities caps = DesiredCapabilities.chrome();
Map<String, Object> preferences = new HashMap<String, Object>();
preferences.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
ChromeOptions options1 = new ChromeOptions();
options1.setExperimentalOption("prefs", preferences);
caps.setCapability(ChromeOptions.CAPABILITY, options1);
Code snippet for setting the download location:
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "C:\\Users\\user\\Downloads\\");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);

Don't create two separate ChromeOptions just because you want multiple capabilities. That's why they take maps. Just put both key value pairs in a single map and then add that to the options object ... eg:
DesiredCapabilities caps = DesiredCapabilities.chrome();
Map<String, Object> preferences = new HashMap<>();
preferences.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
preferences.put("download.default_directory", "C:\\Users\\user\\Downloads\\");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
caps.setCapability(ChromeOptions.CAPABILITY, options);

Related

How to enable "ask where to save each file before downloading" in chrome settings using Chrome options or any other way in selenium java

I am in the process of downloading a zip file and an XML file..and i want to save it in separate folde(which i want to create in the project folder itself..in eclipse)of my choice... Is there a way to do it using ChromeOptions ...also how to disable saving in default directory....
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", false);
prefs.put("download.prompt_for_download", true);
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("safebrowsing.enabled", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("--safebrowsing-disable-download-protection");
options.addArguments("--safebrowsing-disable-extension-blacklist");
driver = new ChromeDriver(options);

How to download an excel spreadsheet without opening a windows popup window

There is a spreadsheet to download using java+selenium in headless mode.
The thing is I'm using to execute this test cases a Linux server(CI/CD). The code is working 100% correct, When I'm using my local computer(Windows) to execute this code.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-infobars");
chromeOptions.addArguments("--browser.helperApps.neverAsk.saveToDisk",
"application/vnd.ms-excel,
vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/excel");
chromeOptions.addArguments("--test-type");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--no-zygote");
chromeOptions.addArguments("--disable-dev-shm-usage");
Map<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.prompt_for_download", "false");
chromePrefs.put("cmd", "Page.setDownloadBehavior");
if (OS.startsWith("windows")) {
chromePrefs.put("download.default_directory", path+WIN_RESOURCES_PATH);
System.setProperty("webdriver.chrome.driver", ".\\src\\main\\resources\\driver\\chromedriver.exe");
if (Parameters.instance().getHeadless().toLowerCase().equals("true")) {
chromeOptions.addArguments("--headless");
}
} else if (OS.startsWith("linux")) {
chromePrefs.put("download.default_directory", path+LIN_RESOURCES_PATH);
System.setProperty("webdriver.chrome.driver", Parameters.instance().getChromeDriver());
if (Parameters.instance().getHeadless().toLowerCase().equals("true")) {
chromeOptions.addArguments("--headless");
}
}
chromeOptions.setExperimentalOption("prefs", chromePrefs);
chromeOptions.addArguments("window-size=1920,1080");
driver = new ChromeDriver(chromeOptions);
driver.get(getUrl());
driver.findElement(locator).click;
driver.switchTo().alert().accept();
My code is something like this. When Selenium test clicks the download button, opening another tab in browser and also a popup window, asking where to "Save" the file.
"I'm running these tests in headless chrome"
For me the following code works for pdf download (Windows)
With these option no promot and it will download your predefined path.
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
Path download_folder = Paths.get(System.getProperty("user.home") + "/Downloads", new String[0]);
HashMap<String, Object> prefs = new HashMap();
prefs.put("download.default_directory", download_folder.toAbsolutePath());
prefs.put("download.prompt_for_download", Boolean.valueOf(false));
prefs.put("download.directory_upgrade", Boolean.valueOf(true));
prefs.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
HashMap<String, Object> options = new HashMap();
options.put("prefs", prefs);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chromeOptions", options);
driver = new ChromeDriver(capabilities);

Chromedriver with options and proxy. Adding mobile emulation

Below is the settings I use for ChromeDriver
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("127.0.0.1:3128");
proxy.setSslProxy("127.0.0.1:3128");
capabilities.setCapability("proxy", proxy);
// Add ChromeDriver-specific capabilities through
// ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.addArguments("--disable-bundled-ppapi-flash");
options.addArguments("--disable-extensions");
options.addArguments("--profile-directory=Default");
options.addArguments("--disable-plugins-discovery");
options.addArguments("--start-maximized");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
But I also want to add mobile emulation. Based on what I read in the docs, Below is what I need
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
and then I would set it to capabilities like below
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
Then pass it to ChromeDriver. But if I set it to DesiredCapabilities , then my proxy and other options would no longer be applied to DesiredCapabilities.
How can I emulate a mobile while preserving my proxy and options settings?
You can pass in arguments into chromeOptions via key args and a List<String> arguments:
Combined with the mobileEmulation setting, this should preserve all your options settings.
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
// add this
chromeOptions.put("args", Arrays.asList("incognito", "disable-bundled-ppapi-flash",
"disable-extensions", "profile-directory=Default", "disable-plugins-discovery",
"start-maximized"));
Also your proxy settings should not be affected, you are calling setCapability on ChromeOptions.CAPABILITY, your "proxy" settings remain unchanged.

How to translate webpage language using Selenium ChromeDriver

When I run the scripts, webpage opened in Turkey language. I want to convert in English language,tried below piece of code using chrome options but it is setting browser language to English not webpage language.please help
static DesiredCapabilities setChromeCapabilities()
{
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 1);
prefs.put("download.default_directory", "C:\\dev\\tmpReport");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("chrome.switches","--disable-extensions");
options.addArguments("--start-maximized");
options.addArguments("--lang=en"); **//tried adding this line of code**
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
return capabilities;
}

Java. Selenium. Chromedriver. How can I enable and disable extensions?

I'm using this code to add own extension, but before load the page it automaticly disabels. How can I enable it?
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=...");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
To add an extension and disable the plugins with Chrome:
ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);
// add an extension
options.addExtensions(new File("C:\\extension.crx"));
// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[]{
"Adobe Flash Player", "Chrome PDF Viewer"});
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.co.uk");

Categories

Resources