简体   繁体   中英

Selenium WebDriver + Java - How to configure proxy settings for Firefox?

I'm a rookie test developer using selenium 2.45 and I'm trying to configure my FirefoxDriver to use my company's proxy settings. I am failing to do so :)

I am following the instruction from here to create a profile on-the-fly:

Using a Proxy for FF

My code looks like this:

public static WebDriver driver;

String usedProxy = "http://myproxy:8080";

    org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy(usedProxy).setFtpProxy(usedProxy).setSslProxy(usedProxy);
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, proxy);

    driver = new FirefoxDriver(cap);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://TestWebsite.com");

I am not receiving any kind of errors but the connection is not working for this browser. When checking Options>Advanced>Network>Connection Settings from Firefox menu, proxy is set to manual but text inputs only contain "http://"

PS: I have a feeling this is relevant but am not sure: TestWebsite.com will load via https only (it's a shopping cart)

If your goal is to test your functionality on different IP addresses, you can use the Tor Browser.

public IWebDriver Driver { get; set; }
public Process TorProcess { get; set; }
public WebDriverWait Wait { get; set; }

[TestInitialize]
public void SetupTest()
{
    String torBinaryPath = @"C:\Users\aangelov\Desktop\Tor Browser\Browser\firefox.exe";
    this.TorProcess = new Process();
    this.TorProcess.StartInfo.FileName = torBinaryPath;
    this.TorProcess.StartInfo.Arguments = "-n";
    this.TorProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
    this.TorProcess.Start();

    FirefoxProfile profile = new FirefoxProfile();
    profile.SetPreference("network.proxy.type", 1);
    profile.SetPreference("network.proxy.socks", "127.0.0.1");
    profile.SetPreference("network.proxy.socks_port", 9150);
    this.Driver = new FirefoxDriver(profile);
    this.Wait = new WebDriverWait(this.Driver, TimeSpan.FromSeconds(60));
}

[TestCleanup]
public void TeardownTest()
{
    this.Driver.Quit();
    this.TorProcess.Kill();
}

Here is the code to refresh the Tor identity.

public void RefreshTorIdentity()
{
    Socket server = null;
    try
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9151);
        server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        server.Connect(ip);
        server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"johnsmith\"" + Environment.NewLine));
        byte[] data = new byte[1024];
        int receivedDataLength = server.Receive(data);
        string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
        server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM" + Environment.NewLine));
        data = new byte[1024];
        receivedDataLength = server.Receive(data);
        stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
        if (!stringData.Contains("250"))
        {
            Console.WriteLine("Unable to signal new user to server.");
            server.Shutdown(SocketShutdown.Both);
            server.Close();
        }
    }
    finally
    {
        server.Close();
    }
}

You can find more detailed information here: http://automatetheplanet.com/using-selenium-webdriver-tor-c-code/

The code examples are in C# but the code should be identical in Java.

Try firefox profile as well. Note this is C# code and converting to Java should be fairly simply

string usedProxy = "http://myproxy:8080";

Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy = usedProxy;
proxy.FtpProxy = usedProxy;
proxy.SslProxy = usedProxy;

FirefoxProfile profile = new FirefoxProfile();
profile.SetProxyPreferences(proxy);

This code works for me:

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", <proxy_url>);
profile.setPreference("network.proxy.http_port", 8080);
profile.setPreference("network.proxy.ssl", <proxy_url>);
profile.setPreference("network.proxy.ssl_port", 8080);

options.setProfile(profile);
options.setAcceptInsecureCerts(true)
options.setCapability("disable-restore-session-state", true);
options.setCapability("marionette", true);

WebDriver driver = new FirefoxDriver(options);

I followed answers from this link: Webdriver and proxy server for firefox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM