I need to setup proxy in my QAF test method. Below code is working fine without QAF. How to setup the same with QAF implementation?
// #Test
public void sampleTest() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
#SuppressWarnings("deprecation")
ChromeDriver driver = new ChromeDriver(capabilities); // Here opening new window and able
to hit my localhost:8080
//i need to use following QAF methods which also needs to trigger my localhost
get("http://demo.rapidtestpro.com/login.php");
sendKeys("1234567", "//*[#id='accno']");
sendKeys("password123", "//*[#id=\"pass\"]");
System.out.println("Exit in sample test");
}
Also I tried to setup proxy in application.properties file like below
system.http.proxyHost=localhost
system.http.proxyPort=8080
Also tried in Testngconfig.xml file like below
<test name="java Test" enabled="true">
<parameter name="driver.name" value="chromeDriver"/>
<parameter name="system.http.proxyHost" value="localhost"/>
<parameter name="system.http.proxyPort" value="8080"/>
Nothing is triggered in localhost:8080.
As per your working code, you want to set proxy for driver using capabilities. When you are using qaf you can provide driver capabilities in different ways. Simplest way is by setting appropriate property with json value of desired capability. In your case it will look like as below:
driver.name=chromeDriver
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]},"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
If you want to set proxy capability for all browsers :
driver.name=<driver name>
#additional capabilities for any driver
driver.additional.capabilities={"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
#additional capabilities only for chrome
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]}}
If you don't know what will be the json representation for capability you can print in console and take a reference. For example blow code with print json value of capabilities that you refereed in question:
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.out.println(JSONUtil.toString(capabilities.toJson()));
}
Other way is using driver listener, where you can append capabilities in void beforeInitialize(Capabilities capabilities) For example:
void beforeInitialize(Capabilities capabilities){
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = (DesiredCapabilities)capabilities;
capabilities.setCapability("proxy", proxy);
}
Related
Trying to initialize a Chrome Remote web driver using WebDriverManager, while passing ChromeOptions and a Selenium Grid standalone server URL using Java.
From online examples;
Passing Chrome options would look like this:
WebDriverManager.chromedriver().setup();
RemoteWebDriver remoteWebDriver = new ChromeDriver(options);
threadLocalDriver.set(remoteWebDriver);
Passing the hub URL for the selenium grid standalone server would look like this:
WebDriverManager.chromedriver().setup();
RemoteWebDriver remoteWebDriver = ((RemoteWebDriver) WebDriverManager
.chromedriver()
.remoteAddress(hubURL)
.create());
threadLocalDriver.set(remoteWebDriver);
How can I pass both to a RemoteWebDriver object?
Thanks
EDIT:
Here is my code. I am getting an error from create() method
[main] ERROR io.github.bonigarcia.wdm.WebDriverManager - There was an error creating WebDriver object for Chrome
io.github.bonigarcia.wdm.config.WebDriverManagerException: Timeout of 30 seconds creating WebDriver object
public void createDriver() throws IOException {
ChromeOptions options = getPlatformSpecificOptions();
logger.info("Driver options: " + options.toString());
String hubURL = "http://127.0.0.1:4444/wd/hub";
WebDriver driver = WebDriverManager.chromedriver()
.capabilities(options)
.remoteAddress(hubURL)
.create();
threadLocalDriver.set(((RemoteWebDriver) driver));
}
TestHelper.setPlatform(PLATFORM);
}
You need to use the method capabilities() for that:
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = WebDriverManager.chromedriver()
.capabilities(options)
.remoteAddress(hubURL)
.create();
Also calling setup() is not needed as it is called by create() method.
I've been trying to add arguments to my Chrome Options to use a proxy and to ignore certain URL's.
I've followed the documentation and am trying to run this very simple test:
#Test
public void myTest(){
ChromeOptions options = new ChromeOptions();
options.addArguments("--proxy-server=http://XXX.XX.XX.XX:8080");
options.addArguments("--proxy-bypass-list=http://www.google.com");
System.setProperty("webdriver.chrome.driver", "C:/drivers/chromeDriver/win/chromedriver.exe");
ChromeDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
}
}
I've also tried with the variation:
options.addArguments("--proxy-bypass-list=*");
But it won't open the URL, is there something I'm doing wrong?
I guess you should use chromedriver.exe instead of eclipse.exe while setting property and make sure you have compatible chromedriver as per current version available in your system.
Here we go :
ChromeOptions options = new ChromeOptions();
options.addArguments("--proxy-server=http://XXX.XX.XX.XX:8080");
options.addArguments("--proxy-bypass-list=https://www.google.com");
System.setProperty("webdriver.chrome.driver", "driver_location\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
I'm using Selenium Standalone Server 3.141.59 https://www.seleniumhq.org/download
In my code, when a WebDriver is created the Selenium server debugs something like: Starting ChromeDriver on port 28208
Is it possible to configure a range of ports (e.g., 28000-28100) that are allowed to be used by the Selenium server?
Use below code to configure chrome to run on other then default port.
int desiredPortNo = 22300;
ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File("chrome_driver_path")).usingPort(desiredPortNo).build();
WebDriver driver = new ChromeDriver(service);
Update
To use with RemoteWebDriver :
int desiredPortNo = 22300;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("...", true);
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("driver_path")).usingPort(desiredPortNo)
.build();
service.start();
WebDriver driver = new RemoteWebDriver(service.getUrl(),capabilities);
driver.get("site_url");
I just wanted to know is it possible to set homepage of Chrome using capabilities and Chrome options in Selenium.
Yes it is possible to instantiate ChromeDriver with using both DesiredCapabilities and ChromeOption to set your desired Homepage as below :-
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
Map<String, Object> preferences = new HashMap<String, Object>();
preferences.put( "browser.startup.homepage", "http://my.home.page" );
preferences.put( "browser.startup.page", START_WITH_HOME_PAGE );
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
capabilities.setCapability( ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
Just to be clear, the capabilities you are trying to set here are options that you use to customize and configure a ChromeDriver session.
Below are the keys which you can use to set for that session:
"browser.startup.homepage", "startup.homepage_welcome_url", "startup.homepage_welcome_url.additional" etc.
You can pass URL for these or if you don't want you can also set something like : "about:blank" as value
I was able to use a proxy successfully, however the proxy is only applied to http, and not https. I am using the code below
Proxy proxy = new Proxy();
proxy.setHttpProxy("myproxy:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
How can I apply the proxy for both http and https?
I found the answer. It's done by the code below
proxy.setSslProxy();
try to use:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("ignore-certificate-errors");
chromeOptions.AddArgument("--ignore-ssl-errors");
switch (ProxyType) // your string variable
{
case "HTTP":
chromeOptions.AddArgument("--proxy-server=http://" + "192.1.1.1:2338"); //example
break;
case "SOCKS":
chromeOptions.AddArgument("--proxy-server=socks5://" + "192.1.1.1:2338");
break;
}