mtmx
mtmx

Reputation: 947

Selenium: changing proxy in Firefox

i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?

I have code below:

public class SodirRejestracja {
String baseUrl = "http://google.pl"; 
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;


@BeforeTest
public void beforeTest() {
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 2);
    System.setProperty("webdriver.gecko.driver", driverPath);
    driver=new FirefoxDriver(profile);
}
@Test
public void test(){
    driver.get("http://google.com");
}
}

Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?

but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"

Upvotes: 0

Views: 2136

Answers (2)

user11634629
user11634629

Reputation: 1

This code works

Proxy proxy = new Proxy();
    proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
    FirefoxOptions options = new FirefoxOptions();
    options.setProxy(proxy);
    driver = new FirefoxDriver(options);

Upvotes: 0

Ji aSH
Ji aSH

Reputation: 3457

A sample (not tested but that compiles) that should do it

String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
     .setFtpProxy(proxyName)
     .setSslProxy(proxyName);

DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);

Upvotes: 0

Related Questions