2015-05-18 116 views
1

我是一名使用硒2.45的新手測試開發人員,我試圖配置我的FirefoxDriver以使用我公司的代理設置。我不這樣做:)Selenium WebDriver + Java - 如何配置Firefox的代理設置?

我下面從這裏的指令來創建一個配置文件上即時:

Using a Proxy for FF

我的代碼如下所示:

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"); 

我沒有收到任何類型的錯誤,但該連接不適用於此瀏覽器。當從Firefox菜單檢查選項>高級>網絡>連接設置,代理設置爲手動,但文本輸入只包含「http://」

PS:我有一種感覺這是相關的,但我不知道:TestWebsite。 com只會通過https加載(這是一個購物車)

回答

3

如果您的目標是在不同的IP地址上測試您的功能,您可以使用Tor瀏覽器。

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(); 
} 

這裏是刷新Tor身份的代碼。

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(); 
    } 
} 

你可以在這裏找到更詳細的信息:http://automatetheplanet.com/using-selenium-webdriver-tor-c-code/

的代碼示例在C#中,但代碼應該在Java中是相同的。

1

嘗試Firefox的配置文件以及。請注意,這是C#代碼,並轉換爲Java應該是相當簡單的

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); 
+0

您好,感謝您的答覆,但FirefoxProfile.SetProxyPreferences()被棄用v2.40.0的:http://grepcode.com/file_/repo1 .maven.org/maven2/org.seleniumhq.selenium/selenium-firefox-driver/2.45.0/org/openqa/selenium/firefox/FirefoxProfile.java /?v = diff&id2 = 2.40.0 – AndreiB

+0

然後應該有類似的實現。那是什麼? – Saifur

相關問題