Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChromeDriver(Capabilities capabilities) is deprecated

I use ChromeDriver 2.33 with WebDriver 3.6.0 and try to set default directory for file download.

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", Vars.DOWNLOAD_FOLDER_ROOT);
DesiredCapabilities caps = DesiredCapabilities.chrome();

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);

I found this in docs:

Use ChromeDriver(ChromeOptions) instead. Creates a new ChromeDriver instance. The capabilities will be passed to the chromedriver service.

like image 871
plaidshirt Avatar asked Oct 17 '17 08:10

plaidshirt


People also ask

What is desired capabilities in Selenium deprecated?

DesiredCapabilities is a class that is used to set basic properties of browsers such as browser name, browser version, operating systems etc. to perform cross-browser testing. In Selenium 4, DesiredCapabilities is replaced with Options.

How do I set capabilities for Chrome in Selenium?

To declare Desired Capabilities in Selenium automation testing using Grid, we can use the setCapability method from the DesiredCapabilities class to set the different types of capabilities of the browser (Ex. Chrome, IE, Firefox, Edge) platform name (Ex. Windows, macOS, etc.).

Which capability is used for using an external ChromeDriver?

Automatic discovery of compatible Chromedriver This capability is the absolute path to the directory in which you have placed one or more Chromedriver executables. Since Appium 1.15.


2 Answers

I hope you wanted to ask about the workaround to avoid the deprecation.

The old method of just building with Capabilities is deprecated. Now, it takes a ChromeDriverService & Capabilities as parameters. So, just a build a ChromeDriverService and pass the same along with your Capabilities to remove the deprecation warning.

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

ChromeDriverService service = new ChromeDriverService.Builder()
                    .usingDriverExecutable(new File("/usr/local/chromedriver"))
                    .usingAnyFreePort()
                    .build();
ChromeDriver driver = new ChromeDriver(service, capabilities);

EDIT: Since ChromeDriver(service, capabilities) is deprecated now as well, you can use,

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

ChromeDriverService service = new ChromeDriverService.Builder()
                            .usingDriverExecutable(new File("/usr/local/chromedriver"))
                            .usingAnyFreePort()
                            .build();
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);    
ChromeDriver driver = new ChromeDriver(service, options);

However, You can completely skip DesiredCapabilities and use only ChromeOptions with setCapability method like,

ChromeOptions options = new ChromeOptions();
options.setCapability("capability_name", "capability_value");
driver = new ChromeDriver(options);
like image 101
Sridhar Avatar answered Oct 16 '22 16:10

Sridhar


The new way to use chrome capabilities is like this :

ChromeOptions options = new ChromeOptions();
    // Proxy proxy = new Proxy();
    // proxy.setHttpProxy("myhttpproxy:3337");
    // options.setCapability("proxy", proxy);
    // options.addArguments("--headless");
    // options.addArguments("--disable-gpu");
    // options.setAcceptInsecureCerts(true);
    // options.addArguments("--allow-insecure-localhost");
    // options.addArguments("--lang=fr-CA");
    options.addArguments("--start-maximized");
driver = new ChromeDriver(options);

You can get more options by looking at this site : https://sites.google.com/a/chromium.org/chromedriver/capabilities

like image 24
Jean-François Gagnon Avatar answered Oct 16 '22 17:10

Jean-François Gagnon