i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?
I have code below:
public class SodirRejestracja {
String baseUrl = "http://google.pl";
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;
#BeforeTest
public void beforeTest() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
System.setProperty("webdriver.gecko.driver", driverPath);
driver=new FirefoxDriver(profile);
}
#Test
public void test(){
driver.get("http://google.com");
}
}
Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?
but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"
A sample (not tested but that compiles) that should do it
String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
.setFtpProxy(proxyName)
.setSslProxy(proxyName);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);
This code works
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxy);
driver = new FirefoxDriver(options);
Related
Since I implemented the Headless configuration on my Test I received this issue: java.lang.NullPointerException
I tried to to switch to other types of implemantation for the Gecko Headless, but none of them worked
#BeforeTest
public static void OpenBrowser () {
System.setProperty("webdriver.gecko.driver","binaries/geckodriver");
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("-headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
driver.get(...
}
Once the test is executed I receive th3 following error: java.lang.NullPointerException
The commands to get it running in headless mode are as follows:
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
WebDriver driver = new FirefoxDriver(options);
You'll probably want to have the WebDriver driver part defined outside the test so that you can perform the following in your #BeforeTest like so:
WebDriver driver;
#BeforeTest
public static void OpenBrowser() {
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
driver = new FirefoxDriver(options);
}
I've faced with a problem that I'm not able to open Chrome with any extension. I've already added an extension but don't know how to run it properly with Selenide framework. Could you please help me
#BeforeClass
public static void setUp() {
Configuration.browser = "chrome";
System.setProperty("selenide.browser", "chrome");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
}
Selenide : http://selenide.org/2018/01/12/selenide-4.10/
You can set custom capabilities in Configuration, and Selenide will use them when opening a browser:
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserCapabilities.setCapability(SOME_CAP, "SOME_VALUE_FROM_CONFIGURATION");
Also you can set custom webdriver like in #dangi13 answer:
WebDriverRunner.setWebDriver(myDriverWithExtension);
I do not know how to do it in selenide but you can add extension in selenium like this :
public static WebDriver getChromeDriverWithAdblockCrx() {
System.setProperty("webdriver.chrome.driver", "src//main//resources//chromedriver.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src//main//resources//uBlock Origin.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(capabilities);
}
Hope that helps you:).
#sers, #dangi13 Thanks a lot!
But capabilities were not added from Configuration.browserCapabilities. I wrote the following code:
#BeforeClass
public static void setUp() {
Configuration.browser = "chrome";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
}
It is known issue that is mentioned on github: https://github.com/codeborne/selenide/issues/676
As workarond I'm using the following option:
#BeforeClass
public static void setUp() {
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
Configuration.browser = "chrome";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
WebDriver webDriver = new ChromeDriver(options);
setWebDriver(webDriver);
}
I want to merge ChromeDriverService with chromeOptions or with DesiredCapabilities for running browser in xvfb.
Below is part of code ChromeDriverService I previously used without selenium grid.
String NodeChromeIncognito = "http://localhost:5558/wd/hub"
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("driver_linux/chromedriver"))
.usingAnyFreePort()
.withEnvironment(ImmutableMap.of("DISPLAY", ":1")).build();
chromeDriverService.start();
// commented because using RemoteWebDriver
// driver = new ChromeDriver(chromeDriverService);
and below is part code of RemoteWebDriver which I will merge with ChromeDriverService.
DesiredCapabilities cap = null;
String NodeChromeIncognito = "http://localhost:5558/wd/hub";
String NodeChrome = "http://localhost:5557/wd/hub";
String NodeFirefox = "http://localhost:5556/wd/hub";
if (browserName.equals("chrome")) {
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
driver = new RemoteWebDriver(new URL(NodeChrome), cap);
} else if (browserName.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", "driver_linux/geckodriver");
cap = DesiredCapabilities.firefox();
cap.setCapability("marionette", true);
driver = new RemoteWebDriver(new URL(NodeFirefox), cap);
}else if (browserName.equals("chromeIncognito")) {
ChromeOptions option = new ChromeOptions();
option.addArguments("--incognito");
cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, option);
cap.setPlatform(Platform.WIN10);
cap.setBrowserName("chrome incognito");
driver = new RemoteWebDriver(new URL(NodeChromeIncognito), cap);
}
I know I could use addArguments("--headless") for chrome, but it does not work well with the my webApp. And also I used DesiredCapabilities.merge and error.
How to merge code/configuration ChromeDriverService with ChromeOptions or DesiredCapabilites ?
As you mentioned you want to merge ChromeDriverService with ChromeOptions or with DesiredCapabilities both can be achieved. But as of current Selenium Java Client releases the following Constructors are Deprecated :
ChromeDriver(Capabilities capabilities)
//and
ChromeDriver(ChromeDriverService service, Capabilities capabilities)
Hence we have to use either of the following options :
Option A : Use only ChromeOptions :
private static ChromeDriverService service;
private WebDriver driver;
//code block
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
.usingAnyFreePort()
.build();
chromeDriverService.start();
ChromeOptions option = new ChromeOptions();
option.addArguments("--incognito");
driver = new RemoteWebDriver(service.getUrl(), options);
Option B : Use DesiredCapabilities and then use merge() from MutableCapabilities to merge within ChromeOptions :
private static ChromeDriverService service;
private WebDriver driver;
//code block
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("path/to/my/chromedriver.exe"))
.usingAnyFreePort()
.build();
chromeDriverService.start();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("...", true);
ChromeOptions option = new ChromeOptions();
option.addArguments("--incognito");
option.merge(capabilities);
driver = new RemoteWebDriver(service.getUrl(), options);
I am trying to run my selenium java code to test a webpage. But webpage is not loading because of network restrictions. When I set the proxy manually and hit the url in browser it works fine. Now I need to pass those proxy setting while running the selenium code. Please help me on this.
I tried below code, but still it shows the same error:
Proxy p=new Proxy();
// Set HTTP Port to 7777
p.setHttpProxy("www.abc.com:8080");
// Create desired Capability object
DesiredCapabilities cap=new DesiredCapabilities();
// Pass proxy object p
cap.setCapability(CapabilityType.PROXY, p);
// Open firefox browser
WebDriver driver=new ChromeDriver(cap);
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. One way to use a proxy is this:
String proxy = "127.0.0.1:5000";
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
WebDriver webDriver = new ChromeDriver(options);
Issue is resolved with below code -
Proxy proxy = new Proxy();
proxy.setHttpProxy("yoururl:portno");
proxy.setSslProxy("yoururl:portno");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Passing a Capabilities object to the ChromeDriver() constructor is deprecated. You can find new official doc here.
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setAutodetect(false);
proxy.setHttpProxy("http_proxy-url:port");
proxy.setSslProxy("https_proxy-url:port");
proxy.setNoProxy("no_proxy-var");
chromeOptions.setCapability("proxy", proxy);
driver = new ChromeDriver(chromeOptions);
DesiredCapabilities dc;
dc = DesiredCapabilities.chrome();
System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "9090");
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "9090");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--disable-extensions");
dc.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(dc);
Another way of doing it:
boolean useProxy = true;
ChromeOptions options = new ChromeOptions().addArguments(
'--headless',
'--no-sandbox',
'--disable-extensions',
'--proxy-bypass-list=localhost');
if (useProxy) {
options.addArguments("--proxy-server=http://ProxyHost:8080");
}
WebDriver driver = new ChromeDriver(options);
See https://peter.sh/experiments/chromium-command-line-switches/ for more Chrome switches
Another way to set a proxy:
Proxy proxy = new Proxy();
proxy.setHttpProxy("<HOST:PORT>");
proxy.setSslProxy("<HOST:PORT>");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability("proxy", proxy);
WebDriver webDriver = new ChromeDriver(chromeOptions);
How to run chrome extension after that?
I already added it but how to run?
public class asset_testing {
#Test
public void user(){
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=C:\\GUI\\distr\\chrome");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", "C:/my-app/drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver(capabilities);