Selenium - using WebDriver interface as type - java

Less of a concrete question but still valid I think.
Selenium tutorial presents an example of obtaining a driver:
WebDriver driver = new FirefoxDriver();
However, this prevents us from using browser-specific drivers' methods. In my case it was setProxy(). Hence, I used:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setProxy("someproxy", 6666);
The question is: why would I limit myself to using methods only from the WebDriver interface, when I know which driver I want to use?

So:
Using the WebDriver interface as a type is better because it provides flexibility: different types of WebDrivers (such as FirefoxDriver or ChromeDriver etc.) could be required at different times.
Setting a proxy could be done with Proxy and DesiredCapabilities. To cite the Selenium Tutorial:
String PROXY = "localhost:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

Related

What is the difference between ChromeDriver driver = new ChromeDriver(); and WebDriver driver = new ChromeDriver();

What is the difference between:
ChromeDriver driver = new ChromeDriver ();
and
WebDriver driver = new ChromeDriver ();
Will I get same output if I use any of these codes in Selenium Java?
I didn't any difference in two codes so will my output also same if I use of these two?
ChromeDriver driver = new ChromeDriver();
When using:
ChromeDriver driver = new ChromeDriver();
The ChromeDriver instance will be only able to invoke and act on the methods implemented by ChromeDriver and supported by google-chrome only. To act with other browsers we have to specifically create individual objects as below :
FirefoxDriver driver = new FirefoxDriver();
InternetExplorerDriver driver = new InternetExplorerDriver();
WebDriver Interface
From Selenium perspective, the WebDriver Interface is similar like a agreement which the 3rd party Browser Vendors like firefox, google-chrome, internet-explorer, safari, etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available Browsers without any change.
WebDriver driver = new ChromeDriver();
Using WebDriver driver = new ChromeDriver(); you are creating an instance of the WebDriver interface and casting it to ChromeDriver Class. All the browser drivers like FirefoxDriver, ChromeDriver, InternetExplorerDriver, PhantomJSDriver, SafariDriver etc implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the Browser Drivers extends RemoteWebDriver). So if we use WebDriver driver, then we can use the already initialized driver (as common object variable) for all browsers we want to automate e.g. Mozilla, Chrome, InternetExplorer, PhantomJS, Safari.
WebDriver driver = new FirefoxDriver();
driver = new ChromeDriver();
driver = new FirefoxDriver();
driver = new SafariDriver();
The correct driver initialisation is the second one. Use this:
WebDriver driver = new ChromeDriver ();

Use of Proxy in Selenium

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

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

How to turn off the proxy settings on chrome browser through selenium webdriver?

When I navigate to my url on chrome, I get The system cannot find the file specified." . I thought it might be due to automatic proxy settings on chrome.
I want to explicitly turn off the proxy setting before starting chrome browser in selenium. I tried below, it isn't working. Can anyone help me
ChromeOptions options = new ChromeOptions();
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability("chrome.setProxyByServer", false);
System.setProperty("webdriver.chrome.driver",sChromeDriverPath);
WebDriver driver = new ChromeDriver();
No errors are thrown at any point of time but URL doesn't open up
Tia
Anjana
You need to pass the options object to the chrome driver when you initialize it. If you use a specific capability then pass it to the chromeDriver(), so that chrome knows what to start with. Also there is no JSON object as setProxyByServer in chrome, instead use noProxy JSON object. Check this out. Here's how -
Proxy proxy=startProxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setNoProxy("");
ChromeOptions options = new ChromeOptions();
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(CapabilityType.PROXY, proxy);
System.setProperty("webdriver.chrome.driver",sChromeDriverPath);
dc.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(dc);
More info about chrome capabilities. Hope it helps.

RemoteWebdriver started with PhantomJS does not open https url

I am using selenium with PhantomJs to scrape the URL. I initialized the driver as below
final DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"PhantomJsPath");
caps.setCapability("page.settings.loadImages", false);
caps.setCapability("trustAllSSLCertificates", true);
RemoteWebDriver driver = new PhantomJSDriver(caps);
driver.setLogLevel(Level.OFF);
driver.get("https://.......")
The pagesource obtained from the driver is empty
Am I missing anything?
Recently the POODLE vulnerability forced websites to remove SSLv3 support. Since PhantomJS < v1.9.8 uses SSLv3 by default, the page cannot be loaded. To fix this, you would need to run PhantomJS with --ssl-protocol=tlsv1 or --ssl-protocol=any. See this answer for plain PhantomJS.
caps = DesiredCapabilities.phantomjs(); // or new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
new String[] {"--ssl-protocol=tlsv1"});
// other capabilities
driver = new PhantomJSDriver(caps);
If this doesn't solve the issue, you can also add
"--web-security=false", "--ignore-ssl-errors=true"
to the String array of cli args as seen in SiKing's answer here.

Categories

Resources