How can i specify chrome profile using Selenide only - java

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

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 to set Chrome Options when using WebDriverManager?

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)

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 disable JavaScript in browser using Selenium (Java)?

In my feature automation, I need to disable JavaScript in browser and run the flow. How to disable JavaScript?
Tried DesiredCapabilities for firefox and Chrome.
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false)
And
DesiredCapabilities dc = new DesiredCapabilities();
dc.setJavascriptEnabled(false);
For firefox, tried
1) Setting up profile for firefox
2) Adding add-on - noScript.xpi
3) profile.setPreference("javascript.enabled", false);
4) Through UI, tried changing the flag - "javascript.enabled" in "about:config" to false. Here, opened firefox and gave "about:config" getting a warning - "This might void your warranty!". There is a button - "I'll be careful, I promise!" with id - warningButton. This button should be clicked to proceed further. To click this button, used driver.findElement(By.id("warningButton")).click(); but it not work.
All the above options are not working. Any advice will be helpful.
I don't know Java, but maybe a solution for Python 3 will help you.
in Python, you can use Options() instead of FirefoxProfile() to deactivate JavaScript:
from selenium.webdriver.firefox.options import Options
options = Options()
options.preferences.update({"javascript.enabled": False})
driver = webdriver.Firefox(options=options)
driver.get('about:config')
Maybe Java this:
FirefoxOptions options = new FirefoxOptions();
options.preferences.update({"javascript.enabled": False});
WebDriver driver = new FirefoxDriver(options);
driver.get('about:config')
You change the preference value using profile with lots of options:
DesiredCapabilities capabilities = new DesiredCapabilities();
// setCapability(SUPPORTS_JAVASCRIPT, javascriptEnabled);
capabilities.setJavascriptEnabled(false);
FirefoxBinary binary = new FirefoxBinary( new File( binaryPath ) );
FirefoxProfile profile = new FirefoxProfile();
//profile.setPreference("preferenceName", "Value");
profile.setPreference("javascript.enabled", false);
RemoteWebDriver driver = new FirefoxDriver(binary, profile, capabilities);
To view the preferences, you can visit the URL about:config
#See
Chrome driver to disable JavaScript issue
chromium-command-line-switches
Truse me this was random trial but works perfectly for me
from selenium import webdriver
options= webdriver.ChromeOptions()
chrome_prefs = {}
options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"javascript": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"javascript": 2}
driver = webdriver.Chrome("your chromedriver path here",options=options)
driver.get('https://google.com/search?q=welcome to python world')
Example image here:-https://i.stack.imgur.com/DdKZQ.png
As per Selenium 3.6 Java Client Release, the easiest way to disable Javascript in the browser would be to set the setJavascriptEnabled argument through an instance of DesiredCapabilities to False and merge it through an instance of FirefoxOptions as follows:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Q46883024_setJavascriptEnabled
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setJavascriptEnabled(false);
FirefoxOptions op = new FirefoxOptions();
op.merge(dc);
WebDriver driver = new FirefoxDriver(op);
driver.get("https://google.com");
driver.quit();
}
}
While execution, the browser you are using may override the setJavascriptEnabled settings.
this works:
FirefoxOptions options = new FirefoxOptions();
options.addPreference("javascript.enabled", false);
This is how you can do it for Chrome in Java.
// import org.openqa.selenium.chrome.ChromeOptions;
ChromeOptions options = new ChromeOptions();
options.addArguments("user-agent=\"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_setting_values.javascript", 2);
options.setExperimentalOption("prefs", chromePrefs);
new ChromeDriver(options);
And it worked for me with ChromeDriver 2.41.578706. As a bonus I am also setting Googlebot as user-agent.
In case you need to do something with DesiredCapabilities you can also convert the options above to capabilities:
// import static org.openqa.selenium.chrome.ChromeOptions.CAPABILITY;
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CAPABILITY, options);
new ChromeDriver(capabilities);

Categories

Resources