How to set Chrome Options when using WebDriverManager? - java

I'm using Web driver manager to setup chrome driver. When setting up the driver I want to add some chrome options? How can I do it when using web driver manager?
I checked the WebDriverManager API but couldn't find any clue..

As of WebDriverManager 5.x you can instantiate the webDriver directly via the WebDriverManager builder with additional capabilities this way (in java) :
WebDriver driver;
//...
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//...
//chromeOptions.addArguments(<another-option>);
//...
driver = WebDriverManager.chromedriver().capabilities(chromeOptions).create();
The capabilities method take a Capabilities as param.
Lucky we are, ChromeOptions implements the Capabilities interface.

public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create Chrome Options
ChromeOptions option = new ChromeOptions();
option.addArguments("--test-type");
option.addArguments("--disable-popup-bloacking");
DesiredCapabilities chrome = DesiredCapabilities.chrome();
chrome.setJavascriptEnabled(true);
option.setCapability(ChromeOptions.CAPABILITY, option);
//Create driver object for Chrome
WebDriver driver = new ChromeDriver(option);
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
Found the answer.. Check above!

This is the example code:
public class Test1{
#Test
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
}

from https://pypi.org/project/webdriver-manager/, pass it in after .install()
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager
options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.binary_location = "C:\\Users\\USERNAME\\FOLDERLOCATION\\Opera\\VERSION\\opera.exe"
driver = webdriver.Opera(executable_path=OperaDriverManager().install(), options=options)

Related

Switch Driver from chrome to electron with java and Selenium

I am currently testing my application using chrome driver, but for one test I need to select a link to then open an electron app, after this i am unable to interact after the op up window has appeared. Is it possible to switch over to the electron app after ive launch the chromedriver?
My driver class looks like this:
public static WebDriver startDriver() {
String projectLocation = System.getProperty("user.dir");
// add in elements for logging into the mobile application also - Android and
// iOS.
if (OSValidator.isMac()) {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver_mac");
} else if (OSValidator.isWindows()) {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");
} else {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver_linux");
}
if (System.getProperty("app.env") != null) { // If coming from Jenkins/Maven goal..
// This is for logging results. Added when investigating crashes on chrome driver. Can be disabled when not needed. 26/03/2020
System.setProperty("webdriver.chrome.verboseLogging", "true");
}
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
options.addArguments("--window-size=1920x1080");
options.addArguments("--disable-cache");
//options.addArguments("--headless");
options.addArguments("--disable-application-cache");
options.addArguments("--disk-cache-size=0");
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-notifications");
options.addArguments("disable-infobars");
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
//options.addArguments("--no-sandbox"); // Bypass OS security model
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
driver = new ChromeDriver(options);
//--------------------
driver.manage().window().maximize();
return driver;
}
You need to instantiate WebDriver with the path to Electron as the binary location.
https://sites.google.com/a/chromium.org/chromedriver/capabilities#TOC-Using-a-Chrome-executable-in-a-non-standard-location

How can i specify chrome profile using Selenide only

I want to use Custom driver provider with my configuration.
But in this case selenium instead selenide is used.
Going such way i need to specify path to chromedriver.exe, but Selenide does not require to download it.
How can i use custom driver provider without setting
System.setProperty("webdriver.chrome.driver", "path/chromedriver.exe");
Call webdriver:
switch (conf_browser) {
case "chrome":
Configuration.browser = DriverProviderChrome.class.getName();
break;
WebdriverProvider:
public class DriverProviderChrome implements WebDriverProvider {
#Override
public ChromeDriver createDriver(DesiredCapabilities capabilities) {
File adf = new File("drivers");
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver/win32/75.0.3770.90/chromedriver.exe");
capabilities = DesiredCapabilities.chrome();
LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.DRIVER, Level.ALL);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--incognito");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("--user-data-dir=C:\\Users\\AntonK\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1");
chromeOptions.setCapability(CapabilityType.LOGGING_PREFS, logs);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
TestBase_working.log(capabilities.getVersion());
chromeOptions.merge(capabilities);
return new ChromeDriver(chromeOptions);
}
}
Selenide calls WebDriverFactory to download chromedriver to the local maven repo and init webdriver.chrome.driver System property.
You can set driver path automatically this way:
WebDriverManager.chromedriver().setup();
Chromedriver path is stored here, but mind that path is not static, it changes when driver gets updated automatically:
WebDriverManager.chromedriver().binaryPath

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

IE goes to http://localhost:36559/ with message "This is the initial start page for the WebDriver server"

I am trying to launch IE but nothing happens with Selenium Java code below:
protected WebDriver ieBrowserSetUp() throws Exception {
// Read the properties file
Properties prop = getProperties();
System.setProperty("webdriver.ie.driver", prop.getProperty("IEDriverPath"));
driver = new InternetExplorerDriver();
driver.navigate().to("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");
WebDriver driver= new InternetExplorerDriver(caps);
System.out.println("Internet Explorer Browser Setup Done Successfully");
return driver;
}
It is working fine with the Firefox and Chrome Webdriver. I saw in one of the posts the Protected Mode Settings needs to be changed. Is there a workaround as the settings is disabled at work considering I am not a Windows admin. The path is pointed to IEDriverServer.exe I downloaded instead of the "Windows-installed-and-defaulted" IE application.
I solved it with this:
System.setProperty("webdriver.ie.driver", prop.getProperty("IEDriverPath"));
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); //disable protected mode settings
caps.setCapability("initialBrowserUrl", url);
driver = new InternetExplorerDriver(caps);

How to run chrome extension with Selenium

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

Categories

Resources