How to run chrome extension with Selenium - java

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

Related

Selenide: How to open Chrome browser with extensions

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

Selenium: changing proxy in Firefox

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

How to Merge Chrome driver service with desired capabilities for headless using xvfb?

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

selenium headless chrome java ignore ssl errors

Selenium headless chrome testing with java in unix returns empty page source as
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
which was due to accessing the secure http (https) website.
Is there a way to ignore the ssl certificate issue? Please let me know how to ignore it.
Selenium Version 3.7.1..java version 1.8.0.144 chrome driver version 2.33
Chrome Version 62+
I gave a try with options below..but it doesn't seem to work.
1. ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setHeadless(true);DesiredCapabilities capabilities =
DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--
ignore-certificate-errors,--web-security=false,--ssl-
protocol=any,--ignore-ssl-errors=true"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
2. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability (CapabilityType.ACCEPT_INSECURE_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Am i doing this in a right way? Let me know the trick to make it work
Thanks in advance
Complete Code:
WebDriver driver = null;
try {
String filePath = "Path to driver";
System.setProperty("webdriver.chrome.driver", filePath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.setAcceptInsecureCerts(true);
options.addArguments("test-type");
String[] switches = {"--ignore-certificate-errors"};
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList(switches));
capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://meta.stackexchange.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("PAGE SOURCE : \n" + driver.getPageSource());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
driver.close();
driver.quit();
}
Now you can add capabilities in option. please try following:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
options.addArguments("--headless", "--window-size=1920,1200", "--ignore-certificate-errors");
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
driver = new ChromeDriver(options);

Selenium ChromeDriver not going to url using default user data

It opens in a new window with the default start menu and shows a notification that Chrome is being controlled by automated test software, but it does not go to the url.
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
What I also tried:
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com");
The exception it gives
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 61.65 seconds
I am using the lastest ChromeDriver 2.30 and Selenium 3.4.0 versions
Found the answer to my own question. It worked when I copied the Default folder from the profile path and moved it somewhere else.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/"+System.getProperty("user.name")+"/Desktop/");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
Try this:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "CHROME_DRIVER_PATH");
capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));
HashMap<String, String> chromePreferences = new HashMap<String, String>();
chromePreferences.put("profile.password_manager_enabled", "false");
capabilities.setCapability("chrome.prefs", chromePreferences);
ChromeOptions options = new ChromeOptions();
options.setBinary("CHROME_BINARY_PATH");
options.addArguments("--test-type");
options.addArguments("--allow-running-insecure-content");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = ChromeDriver(capabilities);
If you don't want to move the profile, you can use the below (change %Profile% to the profile you want to use):
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=%Profile%")
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");

Categories

Resources