Sometime we have to set some property of browser when initial driver instance. There is many properties like set download folder, PDF preview, 'SaveToDisk' option when download anything etc.
So, using DesiredCapabilities we can easily set properties of browser as per our requirement.
If you are suing proxy for Internet then you have to set proxy setting also with capabilities like auto detect setting, proxy type, HTTP proxy etc.
First, you have to add below packages into your code.
import net.lightbody.bmp.proxy.ProxyServer;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
We have to use DesiredCapabilities class object for set all required properties of browser.
So, using DesiredCapabilities we can easily set properties of browser as per our requirement.
If you are suing proxy for Internet then you have to set proxy setting also with capabilities like auto detect setting, proxy type, HTTP proxy etc.
First, you have to add below packages into your code.
import net.lightbody.bmp.proxy.ProxyServer;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
We have to use DesiredCapabilities class object for set all required properties of browser.
DesiredCapabilities capability = DesiredCapabilities.firefox(); capability.setBrowserName("firefox"); // Start proxy setting Proxy proxy = new Proxy(); proxy .setAutodetect(false); proxy .setProxyType(ProxyType.MANUAL); proxy .setHttpProxy("localhost:8080"); proxy .setSslProxy("localhost:8080"); capability.setCapability(CapabilityType.PROXY, proxy); // Proxy setting end // start browser property setting FirefoxProfile Profile = new FirefoxProfile(); Profile.setPreference("browser.download.manager.showWhenStarting", false); Profile.setPreference("browser.download.dir", "c:\\mydownloads"); Profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/ms-excel"); Profile.setPreference("browser.helperApps.alwaysAsk.force", false); // Disables PDF preview Profile.setPreference("pdfjs.disabled", true); // End browser property setting capability.setCapability(FirefoxDriver.PROFILE, Profile); // Add all properties to DesiredCapabilities object WebDriver driver = new FirefoxDriver(capability); // Initiate webdriver instance with DesiredCapabilities object
Code Explanation :
Proxy proxy = new Proxy(); - Proxy class is used for setup proxy setting.
proxy .setAutodetect(false); - It is used for Disabled auto detect setting.
proxy .setProxyType(ProxyType.MANUAL); - It is used for set proxy type.
proxy .setHttpProxy("localhost:8080");
proxy .setSslProxy("localhost:8080");
Both commands are used for set HTTP and SSL proxy.
capability.setCapability(CapabilityType.PROXY, proxy);
At last once all proxy setting set then we have to add that proxy setting into capabilities.
FirefoxProfile Profile = new FirefoxProfile(); - For setting Firefox driver profile.
Profile.setPreference("browser.download.manager.showWhenStarting", false);
It is for stop see download manager when download start
Profile.setPreference("browser.download.dir", "c:\\mydownloads");
It is for set download directory
Profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
application/ms-excel");
It is for set 'SaveToDisk' option off when download start
Profile.setPreference("browser.helperApps.alwaysAsk.force", false);
It is for avoid popup for downloading file
Profile.setPreference("pdfjs.disabled", true);
It is for set PDF preview off
capability.setCapability(FirefoxDriver.PROFILE, Profile);
It is for add all properties in to capability.
WebDriver driver = new FirefoxDriver(capability);
It is for initiate web driver instance with capability.
Please add comment if you have any question.
No comments:
Post a Comment