From 3ee00e07a68cd7f4584deeeb9dd24d4713d1c40a Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Fri, 20 Nov 2020 12:20:17 +0300 Subject: [PATCH] Add ability to create Selenium proxy from hostname and port --- .../com/browserup/bup/client/ClientUtil.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/browserup-proxy-core/src/main/java/com/browserup/bup/client/ClientUtil.java b/browserup-proxy-core/src/main/java/com/browserup/bup/client/ClientUtil.java index 2f1ec9b42..945993bfb 100644 --- a/browserup-proxy-core/src/main/java/com/browserup/bup/client/ClientUtil.java +++ b/browserup-proxy-core/src/main/java/com/browserup/bup/client/ClientUtil.java @@ -91,6 +91,20 @@ public static org.openqa.selenium.Proxy createSeleniumProxy(BrowserUpProxy brows return createSeleniumProxy(new InetSocketAddress(connectableAddress, browserUpProxy.getPort())); } + /** + * Creates a Selenium Proxy object from the BrowserUpProxy instance, using the specified hostnameOrAddress as the Selenium Proxy object's + * proxy address. Determines the port using {@link BrowserUpProxy#getPort()}. The BrowserUpProxy must be started. + * + * @param browserUpProxy started BrowserUpProxy instance to read the port from + * @param hostnameOrAddress the hostnameOrAddress or the String form of the address the Selenium Proxy will use to + * reach its proxy server. + * @return a Selenium Proxy instance, configured to use the BrowserUpProxy instance as its proxy server + * @throws java.lang.IllegalStateException if the proxy has not been started. + */ + public static org.openqa.selenium.Proxy createSeleniumProxy(BrowserUpProxy browserUpProxy, String hostnameOrAddress) { + return createSeleniumProxy(hostnameOrAddress, browserUpProxy.getPort()); + } + /** * Creates a Selenium Proxy object using the specified connectableAddressAndPort as the HTTP proxy server. * @@ -99,10 +113,22 @@ public static org.openqa.selenium.Proxy createSeleniumProxy(BrowserUpProxy brows * @return a Selenium Proxy instance, configured to use the specified address and port as its proxy server */ public static org.openqa.selenium.Proxy createSeleniumProxy(InetSocketAddress connectableAddressAndPort) { + return createSeleniumProxy(connectableAddressAndPort.getHostString(), connectableAddressAndPort.getPort()); + } + + /** + * Creates a Selenium Proxy object using the specified connectableAddressAndPort as the HTTP proxy server. + * + * @param hostnameOrAddress the hostnameOrAddress or the String form of the address the Selenium Proxy will use to + * reach its proxy server. + * @param port the port the Selenium Proxy will use to reach its proxy server. + * @return a Selenium Proxy instance, configured to use the specified address and port as its proxy server + */ + public static org.openqa.selenium.Proxy createSeleniumProxy(String hostnameOrAddress, int port) { Proxy proxy = new Proxy(); proxy.setProxyType(Proxy.ProxyType.MANUAL); - String proxyStr = String.format("%s:%d", connectableAddressAndPort.getHostString(), connectableAddressAndPort.getPort()); + String proxyStr = String.format("%s:%d", hostnameOrAddress, port); proxy.setHttpProxy(proxyStr); proxy.setSslProxy(proxyStr);