Switch Driver from chrome to electron with java and Selenium - java

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

Related

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)

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

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

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