Created
July 13, 2011 14:35
-
-
Save krmahadevan/1080409 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.Map; | |
import org.openqa.selenium.Capabilities; | |
import org.openqa.selenium.Proxy; | |
import org.openqa.selenium.WebDriverBackedSelenium; | |
import org.openqa.selenium.remote.CapabilityType; | |
import org.openqa.selenium.remote.Command; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.openqa.selenium.remote.Response; | |
import org.openqa.selenium.remote.SessionId; | |
import com.thoughtworks.selenium.Selenium; | |
public class ProxyServerSettingsWithRemoteWebDriver { | |
public static void main(String[] args) { | |
String proxyPacURL = "http://localhost/proxy.pac"; | |
String hubURL = "http://localhost:4444/wd/hub"; | |
String urlToLaunch = "http://www.google.com"; | |
try { | |
configureRemoteProxySettings(proxyPacURL, hubURL, urlToLaunch); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void configureRemoteProxySettings(String proxyPacURL, String hubURL, String urlToLaunch) | |
throws MalformedURLException, InterruptedException { | |
Proxy proxy = new Proxy(); | |
proxy.setProxyAutoconfigUrl(proxyPacURL); | |
DesiredCapabilities capability = DesiredCapabilities.internetExplorer(); | |
capability.setPlatform(null); | |
capability.setCapability(CapabilityType.PROXY, proxy); | |
RemoteWebDriver d = new MyDriver(new URL(hubURL), capability); | |
Selenium s = new WebDriverBackedSelenium(d, "http://www.google.com"); | |
s.open(urlToLaunch); | |
} | |
} | |
class MyDriver extends RemoteWebDriver { | |
public MyDriver(URL url, Capabilities capability) { | |
super(url, capability); | |
} | |
@Override | |
protected void log(SessionId sessionId, String commandName, Object toLog) { | |
if (sessionId != null) { | |
System.out.println("Session ID information"); | |
System.out.println(sessionId.toString()); | |
} | |
System.out.println("Command = " + commandName); | |
if (toLog != null) { | |
if (toLog instanceof Response) { | |
Response response = (Response) toLog; | |
System.out.println("Printing the response obtained"); | |
System.out.println("Session ID from response = " + response.getSessionId()); | |
System.out.println("Status from response = " + response.getStatus()); | |
System.out.println("Response value as below : [" + response.getValue() + "]"); | |
} | |
if (toLog instanceof Command) { | |
Command command = (Command) toLog; | |
Map<String, ?> params = command.getParameters(); | |
for (String key : params.keySet()) { | |
Object individualValue = params.get(key); | |
if (individualValue instanceof Capabilities) { | |
Capabilities c = (Capabilities) individualValue; | |
Proxy p = (Proxy) c.getCapability(CapabilityType.PROXY); | |
System.out.println("Printing proxy settings"); | |
System.out.println("Ftp proxy " + p.getFtpProxy()); | |
System.out.println("Http Proxy " + p.getHttpProxy()); | |
System.out.println("No Proxy " + p.getNoProxy()); | |
System.out.println("SSL Proxy " + p.getSslProxy()); | |
System.out.println("Proxy auto config url : " + p.getProxyAutoconfigUrl()); | |
System.out.println("Auto detection status " + p.isAutodetect()); | |
System.out.println("Proxy setting that is done : " + p.getProxyType().toString()); | |
}else{ | |
System.out.println("Individual key value " + individualValue); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following is the complete Stack Trace
Exception in thread "main" org.openqa.selenium.WebDriverException: Proxy autodetect is incompatible with manual settings
Build info: version: '2.0.0', revision: '12817', time: '2011-07-07 19:14:12'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_20'
Driver info: driver.version: ProxyServerSettingsWithRemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:402)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:101)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:84)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:92)
at com.paypal.MyDriver.(ProxyServerSettingsWithRemoteWebDriver.java:51)
at com.paypal.ProxyServerSettingsWithRemoteWebDriver.configureRemoteProxySettings(ProxyServerSettingsWithRemoteWebDriver.java:42)
at com.paypal.ProxyServerSettingsWithRemoteWebDriver.main(ProxyServerSettingsWithRemoteWebDriver.java:26)
Caused by: java.lang.IllegalStateException: Proxy autodetect is incompatible with manual settings
at org.openqa.selenium.Proxy.verifyProxyTypeCompatilibily(Proxy.java:153)
at org.openqa.selenium.Proxy.setFtpProxy(Proxy.java:101)
at org.openqa.selenium.Proxy.(Proxy.java:48)
at org.openqa.selenium.browserlaunchers.Proxies.extractProxy(Proxies.java:89)
at org.openqa.selenium.browserlaunchers.Proxies.newProxyPac(Proxies.java:102)
at org.openqa.selenium.browserlaunchers.Proxies.makeProxyPAC(Proxies.java:63)
at org.openqa.selenium.browserlaunchers.Proxies.makeProxyPAC(Proxies.java:55)
at org.openqa.selenium.browserlaunchers.WindowsProxyManager.changeRegistrySettings(WindowsProxyManager.java:163)
at org.openqa.selenium.ie.InternetExplorerDriver.prepareProxy(InternetExplorerDriver.java:125)
at org.openqa.selenium.ie.InternetExplorerDriver.(InternetExplorerDriver.java:68)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(null)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(null)
at java.lang.reflect.Constructor.newInstance(null)
at org.openqa.selenium.remote.server.DefaultDriverFactory.callConstructor(DefaultDriverFactory.java:87)
at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance(DefaultDriverFactory.java:81)
at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:154)
at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:1)
at java.util.concurrent.FutureTask$Sync.innerRun(null)
at java.util.concurrent.FutureTask.run(null)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(null)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(null)
at java.lang.Thread.run(null)