Use of Proxy in Selenium - java

I have a website that I want to test through automation. My client wants to test the website using a proxy of another country as we can testing manual using Browserc Extension. How we can perform it in selenium using java. Below is the code I tried but how can I check this is the same country that proxy I used.
`
Proxy proxy= new Proxy();
proxy.setHttpProxy("localhost:8888");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
System.setProperty("webdriver.gecko.driver", "G:\\Selenium\\Driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://vapesuite.co.uk/#/");`

I think the issue is with your code and proxy and it is not working properly.
I have tried the free proxy and it was working fine for me, please see the attached screenshot.
Code Used:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "116.80.41.12");
profile.setPreference("network.proxy.http_port", 80);
profile.setPreference("network.proxy.ssl", "116.80.41.12");
profile.setPreference("network.proxy.ssl_port", 80);
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://vapesuite.co.uk/#/");
https://free-proxy-list.net/
https://mylocation.org/

if you are using maven this should work for you :
https://maven.apache.org/guides/mini/guide-proxies.html

Related

Selenium + Proxy = Fail

Proxy not working in Selenium
First of all i use free proxys from proxyfish (maybe thats the problem alrdy) https://hidemyna.me/en/proxy-checker/ tells me theyre working.
I found lots of code/solutions, most is outdated (capabilities=options..) or not working for me.
Tried several code/proxys chrome/ff however https://www.iplocation.net/ and https://whatismyipaddress.com showing actual IP address (IPv4 and IPv6).
What I tried..
System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
System.getProperties().put("74.208.112.***", "8080"); //1st try
Proxy proxy = new Proxy();
proxy.setHttpProxy("74.208.112.***:8080"); //2nd try
ChromeOptions options = new ChromeOptions();
options.setCapability("proxy", proxy);
options.setProxy(proxy);
options.addArguments("--proxy-server=74.208.112.***:8080"); //3rd try
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.iplocation.net/");
driver.get("https://whatismyipaddress.com/");
Solution:
proxy.setSslProxy("74.208.112.***:8080");
Is there a way to test proxy connection before using proxy?

Selenium Proxy with Google chrome portable

I want to use portable google chrome for my selenium testing. I am using DesiredCapabilities object to set proxy in browser.
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
And for using portable Google chrome i am using this code.
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Selenium\\Browsers\\GoogleChromePortable\\GoogleChromePortable.exe");
driver = new ChromeDriver(options);
Now the problem is the constructor of ChromeDriver dont have option for creating driver object using both DesiredCapabilities as well as proxy. i.e. either i can apply proxy or i can use portable chrome.
I want something like this
new ChromeDriver(capabilities ,options);
I found a way
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
ChromeOptions options = new ChromeOptions();
options.setBinary("D:\\m_ali\\GoogleChromePortable\\GoogleChromePortable.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", "D:\\m_ali\\chromeDriver\\chromedriver_2.27win32\\chromedriver.exe");
driver = new ChromeDriver(capabilities);

Getting Request And Response Using BrowserMobProxy, Selenium, Firefox, marionette/gecko

I'm trying to get response and request using BMP's RequestFilter and ResponseFilter. However, when the webpage loads, nothing gets printed in the console.
Everything else seems to work though. Maybe BMP is not watching GeckoDriver?
I'm using Firefox 50.0, BrowserMobProxy 2.1.2, Selenium 3.0.1, and GeckoDriver 0.11.1
The testing code is below. Could someone please help me?
Thank you very much!
BrowserMobProxy server = new BrowserMobProxyServer();
server.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
server.start();
int port = server.getPort();
server.addRequestFilter((request, content, info) -> {
String q = URLDecoder.decode(info.getOriginalUrl(), "UTF-8");
System.out.println("Request: "+q);
return null;
});
server.addResponseFilter((response, content, info) -> {
String type = response.headers().get("Content-Type");
System.out.println("Response: "+info.getOriginalRequest());
System.out.println(type);
});
Proxy proxy = ClientUtil.createSeleniumProxy(server);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability("marionette", true);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
FirefoxProfile fp = new FirefoxProfile();
capabilities.setCapability(FirefoxDriver.PROFILE, fp);
String gecko = "d:/Programming/java/geckodriver.exe";
System.setProperty("webdriver.gecko.driver", gecko);
driver = new FirefoxDriver(capabilities);
driver.get("https://google.com");;
In Firefox 51 and lower, there is a bug/missing feature in Selenium 3's GeckoDriver that prevents Firefox from picking up the proxy settings when setting CapabilityType.PROXY on the DesiredCapabilities object.
However, you can still set the proxy settings directly on the FirefoxProfile. There's an example of this in one of BMP's tests. Since you're already using a FirefoxProfile object, this would probably be a sensible solution for you. It would look something like this (replace localhost with a hostname/ip address as appropriate):
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("network.proxy.http", "localhost");
fp.setPreference("network.proxy.http_port", server.getPort());
fp.setPreference("network.proxy.ssl", "localhost");
fp.setPreference("network.proxy.ssl_port", server.getPort());
fp.setPreference("network.proxy.type", 1);
fp.setPreference("network.proxy.no_proxies_on", "");
This geckodriver issue also discusses a few other alternatives to using CapabilityType.PROXY on the DesiredCapabilities object.
UPDATE
According to the mozilla bug report, this issue is fixed in Firefox 52, which is scheduled to be released on March 7, 2017. In the meantime, the solution with the FirefoxProfile should work with 51 (and lower), and should also continue to work with 52+.

Set Selenium proxy programmatically

In my automation project I need to set proxy server. I tried with system variable settings and profile setting for firefox browser. But those technique does not work for me. Please any one help me in this regard.
Note: I also tried with executing shell command using java but I got stuck when password is asked.
You definitely don't need to set any System-level properties. This is one way to do it in Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1); // Manual proxy config
profile.setPreference("network.proxy.http", "proxy3.proxy.net");
profile.setPreference("network.proxy.http_port", 3128);
profile.setPreference("network.proxy.ssl", "proxy3.proxy.net");
profile.setPreference("network.proxy.ssl_port", 3128);
WebDriver driver = new FirefoxDriver(profile);
Or a more flexible, less browser-specific alternative:
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy("proxy3.proxy.net:3128");
proxy.setSslProxy("proxy3.proxy.net:3128");
DesiredCapabilities caps = DesiredCapabilities.firefox(); // or chrome() etc.
caps.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(caps);

WebDriver: set initial URL

I use WebDriver Java bindings. Creating a FirefoxDriver instance launches the Firefox browser with the 'firstrun' page: https://www.mozilla.org/en-US/firefox/42.0/firstrun/learnmore/ . It takes some time to load this page.
I want Firefox to start from "about:blank" to make the initialization faster. For Internet Explorer this can be done by:
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "about:blank");
new InternetExplorerDriver(cap);
How to do the same for Firefox and Chrome?
Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.startup.homepage", "about:blank");
profile.setPreference("startup.homepage_welcome_url", "about:blank");
profile.setPreference("startup.homepage_welcome_url.additional", "about:blank");
WebDriver driver = new FirefoxDriver(profile);

Categories

Resources