Passing options to chrome driver selenium - java

I am trying to disable the output to the console for chrome. If I pass the --start-maximized option it works fine. I may have the wrong command?
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--silent"));
chrome = new ChromeDriver(_chromeservice,capabilities);
I also tried
ChromeOptions options = new ChromeOptions();
options.addArguments("silent");
chrome = new ChromeDriver(options);
Output
Started ChromeDriver port=26703 version=23.0.1240.0
log=/Brett/workspace/TestNG/chromedriver.log
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending
sendsBlockquote

Hinted by this Chromedriver ticket (about the silent option), I looked in the source of ChromeDriverService.java, and found a reference to "webdriver.chrome.logfile".
After adding -Dwebdriver.chrome.logfile="/dev/null" to my java command, the logs became readable again: The usless ChromeDriver logs were gone, while theSystem.out.println calls and exceptions are still shown in the console.
I start java with the following parameters (Linux / Mac):
DIR=path/to/dir/containing/selenium/and/stuff
cd "$DIR" && java -cp "$DIR\
:$DIR/output\
:$DIR/bin/selenium-server-standalone-2.33.0.jar" \
-Dwebdriver.chrome.driver="$DIR/bin/chromedriver" \
-Dwebdriver.chrome.args="--disable-logging" \
-Dwebdriver.chrome.logfile="/dev/null" \
AllTests
If you're on Windows:
set DIR=path\to\dir\containing\selenium\and\stuff
cd "%DIR%" && java -cp "%DIR%;%DIR%\output;%DIR%\bin\selenium-server-standalone-2.33.0.jar" ^
-Dwebdriver.chrome.driver="%DIR%\bin\chromedriver.exe" ^
-Dwebdriver.chrome.args="--disable-logging" ^
-Dwebdriver.chrome.logfile=NUL ^
AllTests
Explanation for the composition of my classpath (-cp): My tests are located in a directory at "$DIR/output". The Selenium jar file is placed in "$DIR/bin/selenium-server-standalone-2.33.0.jar". "AllTests" is the name of my class containing public static void main(String[] args) - this launches my tests.
The other parameters are self-explanatory, adjust it to your needs. For convenience (used in a shell/batch script), I've declared the common directory in a variable DIR.

When I was setting chrome up with
selenium-chrome-driver-2.48.2.jar
chromedriver 2.20
selenium-java-2.48.2.jar
none of the above answers worked for me,
Since I see some of the answers are a few years old, I will post what worked for me.
ChromeOptions chromeOptions = setupChromeOptions();
System.setProperty("webdriver.chrome.logfile", "\\path\\chromedriver.log");
System.setProperty("webdriver.chrome.driver", "\\path\\chromedriver.exe");
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver(chromeOptions);

Try "--disable-logging" instead.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-logging"));
chrome = new ChromeDriver(_chromeservice,capabilities);

As of Selenium 3 at least, you can use ChromeDriverService and its inner class Builder to be able to launch the driver in silent mode.
A oneliner for that:
new ChromeDriver(new ChromeDriverService.Builder().withSilent(true).build());
The constructor is straight-forward, you create a new service builder setting silent to true (that's the critical part) and you finally build it into a ChromeDriverService which is required by ChromeDriver's constructor.

Related

Electron+Selenium+Java. Run electron app with parameters (like in windows shortcut)

I am working on automation for Electron app using Selenium + Java.
Java test class for demo electron app works good (snippet is below).
The problem is, that my real application on Windows should be launched with json configuration file. And the path to this config file set up in the SHORTCUT parameter.
Example how it works for the shortcut:
C://AppExample/Desktop/app.exe --param=config.json
Test class for demo electron app
#Test
public void test() throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setBinary("C://AppExample/Desktop/app.exe");
options.setCapability("chromeOptions", options);
options.setCapability("setBrowserName", "chrome");
driver = new ChromeDriver(options);
if (driver.findElements(By.id("button-about")).size() > 0)
driver.findElement(By.id("button-about")).click();
driver.findElement(By.id("get-started")).click();
List<WebElement> elements = driver.findElements(By.className("nav-button"));
for (WebElement element : elements) {
element.click();
}
driver.quit();
}
I tried to set custom capability but it doesn't work.
options.setCapability("--param","=config.json");
Is there a way to run electron app with parameters (like parameters in windows shortcut) in selenium?
I found the solution.
First, I need to use WinappDriver+Appium+Selenium Java stack.
The second:
Solution about parameters is here
https://github.com/microsoft/WinAppDriver/issues/1323
So the code will be like that:
dc = {
'app': App,
'platformName': 'Windows',
'platformVersion': '10',
'automationName': 'Windows',
"appArguments": "-- config=file.json",
'appWorkingDir': "path to your file.json"
}

selenium load chrome profile java

I tried loading the chrome profile with selenium. However, whenever I upload the profile, I get an error:
invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use - user-data-dir.
String chromeProfile = "C:\\Users\\ad\\AppData\\Local\\Google\\Chrome\\User Data";
ChromeDriverService chSvc = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("C:\\Driver\\chromedriver.exe")).usingAnyFreePort().build();
ChromeOptions chOption = new ChromeOptions();
chOption.addArguments("--user-data-dir=" + chromeProfile);
chOption.addArguments("--profile-directory=Profile 33");
chOption.addArguments("--start-maximized");
ChromeDriver driver = new ChromeDriver(chSvc, chOption);
driver.get("https://google.com");
You cannot run multiple instance of ChromeDriver with the same user-data-dir. What you can do is every time you create a ChromeDriver instance, create a temp directory then set it in ChromeOptions chOption.addArguments("--user-data-dir=" + tempDir);

Set firefox and chrome driver for selenium in java?

I am facing a problem with selenium web driver in java, it says that "The path to the driver executable must be set by the webdriver.gecko.driver system property" you can see it below, but I have set everything as usual I do.
You just need to change the execution flow.
You see, you get the exception because you create FirefoxDriver first and THEN you set the property. It should be in reverse order.
First, set property and THEN initialize WebDriver:
public class EntryPoint {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "path/to/executable.exe");
WebDriver driver = new FirefoxDriver();
}
}
Assuming you are on Windows:
1. Try replacing "/" with "\" on your path.
2. If the previous step did not work, try running Intellij as an admin. The program might not have the right permissions to execute anything on that folder.

How to run ghostdriver with Selenium using java

I want to use phantomJS for some web testing, and I've come across GhostDriver (https://github.com/detro/ghostdriver). I've built it using the instructions in the readme and I can run it on a specified port, but I am not sure how to access the web driver from my java code. To clarify, I've seen this example in ruby:
caps = {
:browserName => "phantomjs",
:platform => "LINUX"
}
urlhub = "http://key:secret#hub.testingbot.com:4444/wd/hub"
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120
#webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
#webdriver.navigate.to "http://www.google.com/"
puts #webdriver.title
#webdriver.save_screenshot("./screenshot.png")
#webdriver.quit
I'm just not sure how to do the same from java.
Just to clarify for others who might see this, to run it from java:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/Path/to/bin/phantomjs");
driver = new PhantomJSDriver(caps);
Then it can be used like a usual WebDriver.
I believe this link will answer your questions. You will need Selenium 2.28.0, and PhantomJS 1.8. I have tested this, and it works as advertised, although my tests were precursory. Note that you need to download the Selenium zip file to get the jar which contains the bindings. The Maven repo does not yet include it.
http://ivandemarino.me/2012/12/04/Finally-GhostDriver-1-0-0/
First download the exe file of the PhantomJSDriver. Don't need to install, only download this file from http://phantomjs.org/download.html and simply give the path of the exe file in the given code.
public class Browserlaunch {
public static void main(String[] args) {
DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/Drivers/phantomjs/bin/phantomjs.exe");
WebDriver driver=new PhantomJSDriver(DesireCaps);
driver.get("http://google.com");
}
}
Only set system property:
System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

Issue installing extension with Selenium remote Firefox webdriver in Saucelabs

Issue
Trying to install a Firefox browser extension during remote execution of Selenium tests on Saucelabs. When executing the tests locally, the extension is installed and active in Firefox, but in remote execution on Saucelabs the extension does not appear in the list of installed extensions. Following the steps outlined in this Saucelabs support article.
Setup
Selenium.Support v2.48.2 or v2.49.0
Selenium.WebDriver v2.48.2 or v2.49.0
Windows 10 or 7
Firefox 43
C# test setup
private static FirefoxProfile CreateFirefoxProfile()
{
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension("Tools/modify_headers-0.7.1.1-fx.xpi");
profile.SetPreference("general.useragent.override", "UA-STRING");
profile.SetPreference("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
profile.SetPreference("modifyheaders.headers.count", 1);
profile.SetPreference("modifyheaders.headers.action0", "Add");
profile.SetPreference("modifyheaders.headers.name0", "SampleHeader");
profile.SetPreference("modifyheaders.headers.value0", "test1234");
profile.SetPreference("modifyheaders.headers.enabled0", true);
profile.SetPreference("modifyheaders.config.active", true);
profile.SetPreference("modifyheaders.config.alwaysOn", true);
profile.SetPreference("modifyheaders.config.start", true);
return profile;
}
private static IWebDriver GetRemoteDriver()
{
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile);
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
capabilities.SetCapability(CapabilityType.Version,"");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}
Firefox settings
When looking at about:support in Firefox during local execution and opening the user.js file, it includes the following extension setup which matches the web driver configuration. Inspecting user.js on the Saucelabs remote instance does not include that. Here's a paste bin of the contents of the remote user.js file.
user_pref("general.useragent.override", "UA-STRING");
user_pref("extensions.modify_headers.currentVersion", "0.7.1.1-signed");
user_pref("modifyheaders.headers.count", 1);
user_pref("modifyheaders.headers.action0", "Add");
user_pref("modifyheaders.headers.name0", "SampleHeader");
user_pref("modifyheaders.headers.value0", "test1234");
user_pref("modifyheaders.headers.enabled0", true);
user_pref("modifyheaders.config.active", true);
user_pref("modifyheaders.config.alwaysOn", true);
user_pref("modifyheaders.config.start", true);
I've also tried referencing an external version of the xpi with same result.
https://addons.mozilla.org/firefox/downloads/latest/967/addon-967-latest.xpi
Posted a bug report to SeleniumHQ and received this response, which fixed the above code.
In the RemoteWebDriver case for .NET, you need to use the
ToBase64String() method. This should resolve the issue. Note that this
is one of the reasons that other drivers have type-safe options
classes instead of passing raw capabilities. Future versions of the
.NET bindings should extend this pattern to Firefox as well, removing
this as an issue in the future.
The GetRemoteDriver method from above should be updated to this.
private static IWebDriver GetRemoteDriver()
{
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
// Note the change here, calling .ToBase64String()
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
capabilities.SetCapability("name", buildContext);
capabilities.SetCapability(CapabilityType.BrowserName,"firefox");
capabilities.SetCapability(CapabilityType.Version,"");
capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
capabilities.SetCapability("screen-resolution", "1280x1024");
capabilities.SetCapability("username", "SaucelabsUserName");
capabilities.SetCapability("accessKey", "SaucelabsAccessKey");
capabilities.SetCapability("build", "BuildNumber");
return new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com/wd/hub"), capabilities);
}
After seeing what the fix is, I was able to find additional resources that mentioned this change.
https://stackoverflow.com/a/14285902/276681
https://code.google.com/p/selenium/issues/detail?id=2696#c4

Categories