2

Hi I am writing a Selenium WebDriver Java code/script.

public static WebDriver dr =null;
public static EventFiringWebDriver driver=null;

dr = new FirefoxDriver();

driver = new EventFiringWebDriver(dr);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

So Firefox browser is opening but proxy setting are stopping.

If it is manual I went to Tools->Options-Settings-> There I have given Auto-detect proxy settings for this network
It is working.

But whenever I open by script I think new profile is opening. That's why I have set Auto-detect proxy settings for this network true by using script.

So can you please assist me how to do that?

Thanks Raju

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Raju
  • 137
  • 2
  • 4
  • 15
  • have you tried creating new firefox profile and use for selenium tests. follow this how to [click this](http://girliemangalo.wordpress.com/2009/02/05/creating-firefox-profile-for-your-selenium-rc-tests/) once created use about:config and set value for network.proxy.type as 2 – StaleElementException Jan 09 '13 at 13:26

4 Answers4

4

This is the good solution:

import org.openqa.selenium.Proxy.ProxyType;` 

public static WebDriver dr = null;
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); 
proxy.setSslProxy("proxyurl"+":"+8080); 
proxy.setFtpProxy("proxy url"+":"+8080); 
proxy.setSocksUsername("SSSLL277"); 
proxy.setSocksPassword("password"); 

DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.PROXY, proxy); 
dr = new FirefoxDriver(dc);
kamituel
  • 34,606
  • 6
  • 81
  • 98
Raju
  • 137
  • 2
  • 4
  • 15
  • 1
    and for http proxy : proxy.setHttpProxy("http://${login}:${password}@${proxy}:${port}");` – revo Nov 07 '13 at 16:30
3

You can set the preferences of the profile at runtime atleast with firefox driver. Give the following a try :

FirefoxProfile ff = new FirefoxProfile();
ff.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
FirefoxDriver ffD = new FirefoxDriver(ff);
niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Do you get some error? What version of webdriver are you on? I have checked the code and when i put a breakpoint, I see the setting done in the browser launched by driver. – niharika_neo Jan 09 '13 at 14:59
  • In code no error... After opening a firfox Server not found Firefox can't find the server at www.quikr.com. Check the address for typing errors such as ww.example.com instead of www.example.com If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web. – Raju Jan 09 '13 at 15:26
  • I have modified code like this public static WebDriver dr =null; FirefoxProfile ff = new FirefoxProfile(); ff.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal()); FirefoxDriver ffD = new FirefoxDriver(ff); dr = new FirefoxDriver(ffD); – Raju Jan 09 '13 at 15:27
  • I am using latest Webdriver 2.28.0 – Raju Jan 09 '13 at 15:37
  • 1. Why do u need to have 2 drivers? 2. I tried the above code with the last statement as ffD.get("https://www.google.com"). Works for me..Have you tried with some other url? – niharika_neo Jan 09 '13 at 16:30
  • WebDriver dr = new FirefoxDriver(ffD); – Raju Jan 09 '13 at 16:49
  • public static WebDriver dr = null; – Raju Mar 21 '13 at 14:33
  • To use this solution, you need a proxy server setuped to provide an autosetup resource. – Edgard Leal Aug 17 '16 at 14:31
  • This is not runtime, you set config before start driver – Wonka Jan 22 '19 at 09:21
1

This is the solution that worked for me, a bit of a combo of the first two and simple enough. Did not need to do individual user authentication.

import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.firefox.FirefoxProfile;

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "proxy.something.com");
profile.setPreference("network.proxy.http_port", 8080);
profile.setPreference("network.proxy.ssl", "proxy.something.com");
profile.setPreference("network.proxy.ssl_port", 8080);


WebDriver driver = new FirefoxDriver(profile); 
adwilson07
  • 21
  • 2
  • This worked for me too (`network.proxy.type` is crucial), but if the application runs on localhost (my case), one may want to add: `profile.setPreference("network.proxy.no_proxies_on", "");` Default value is `localhost, 127.0.0.1` – virgo47 Apr 12 '15 at 19:20
  • For localhost you may need another profile setting, that does not relate to proxy - but it kept replacing localhost/something for www.localhost.com/something. Just add: `profile.setPreference("browser.fixup.alternate.enabled", false);` – virgo47 Apr 12 '15 at 20:08
0

This is what I do to set auto detection:

FirefoxProfile profile = new FirefoxProfile();
Proxy proxy = new Proxy();
proxy.IsAutoDetect=true;
profile.SetProxyPreferences(proxy);
driver = new FirefoxDriver(profile);