Thursday, September 12, 2019

Post#20.Chrome Firefox Options-SetBinary


Chrome / Firefox Options class in Selenium

ChromeOptions class extends MutableCapabilities Class and MutableCapabilities extends Object Class, ChromeOptions also implements Serializable, Capabilities.

We can use ChromeOptions class to manage options specific to ChromeDriver.



Similar to ChromeOptions, Firefox also provides FirefoxOptions class where you can set all the options specific to the execution of the session.

Remaining browsers also provides this feature.

Using Browser Options class we can set options like make the browser headless, load extensions, set binaries and set proxy so on.

Methods present in the Browser Options class

All the methods present in the ChromeOptions/ FirefoxOptions class are non-static methods, below are the methods present in the ChromeOptions/ FirefoxOptions.
1. setBinary
2. setAcceptInsecureCerts
3. setProxy
4. addExtensions
5. setHeadless
6. addArguments
7. setUnhandledPromptBehaviour
8. addEncodedExtensions
9. getPlatform
10. setExperimentalOption
11. getCapability
12. setPageLoadStrategy
13. asMap
14. merge

setBinary in ChromeOptions/FirefoxOptions

We cannot expect systems to have the browser exe file in right path, sometimes due to permission issues; People will install the browsers in different path.

In these case the Selenium searches for the browser exe in default path as the browser is not present in the default path, selenium throws an exception.

This happens not only for chrome and firefox but also happens to other browsers.

To overcome this selenium provides a method called setBinary in ChromeOptions/FirefoxOptions class, using this method we can set the browser exe path.

package pack2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeOptionsEx {
            public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
                        ChromeOptions options = new ChromeOptions();
                        // set the binary path
options.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application \\chrome.exe");
                        //open the browser
                        WebDriver driver = new ChromeDriver(options);
                        driver.get("https://google.com");
            }
}
setBinary method is overloaded method, in above example we have passed path as String but we can also send file as parameter.

Below example shows how to set the exe binary path using FirefoxOptions.
package pack2;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class FirefoxOptionsEx {
            public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
                        FirefoxOptions options = new FirefoxOptions();
options.setBinary(new FirefoxBinary(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe")));
                        //open the browser
                        WebDriver driver = new FirefoxDriver(options);
                        driver.get("https://bing.com");
            }
}

This method is used whenever tester want to perform the version testing in selenium, In version testing we may need to have more than one version of the a browser. In such cases we will install the additional browser in custom path.

No comments:

Post a Comment