How to get Chrome browser console log [INFO] entries with Selenium - java

I am having issues getting chrome browser console log [INFO] entries with Selenium and the only Level type of entries I am getting are the errors(WARNING, SEVERE).
Is there any way to get anything different than the error entries as I need to get the [INFO] entries and assert based on their content, I have read recently that Selenium is able to return entries only for errors is that accurate?
Would really appreciate any information given,
thank you for your attention and time!

thank you for your answer but unfortunately none of them worked for me, I have managed to come up with a solution for my issue using:
ChromeOptions options = new ChromeOptions();
options.setCapability(ChromeOptions.CAPABILITY, getCap());
WebDriver driver = new ChromeDriver(options);
Alongside with the custom made method:
private static DesiredCapabilities getCap() {
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
logPrefs.enable(LogType.PROFILER, Level.INFO);
logPrefs.enable(LogType.BROWSER, Level.INFO);
logPrefs.enable(LogType.CLIENT, Level.INFO);
logPrefs.enable(LogType.DRIVER, Level.INFO);
logPrefs.enable(LogType.SERVER, Level.INFO);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
return caps;
}
And finally had to make a filter method as the Selenium one was not working properly for me and was returning all of the entries:
private static List<LogEntry> filterLog(LogEntries entries) {
List<LogEntry> logs = new ArrayList<>();
for (LogEntry entry : entries) {
if(entry.getLevel().toString().equals(INFO)) {
logs.add(entry);
}
}
return logs;
}

From the documentation (http://chromedriver.chromium.org/logging):
By default ChromeDriver logs only warnings/errors to stderr. When debugging issues, it is helpful to enable more verbose logging.
Also from documentation:
System.setProperty("webdriver.chrome.logfile", "D:\\chromedriver.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");
Hope that works.
Alternative Option
You could try the following if the above doesn't work, this time using the ChromeOptions object instead:
ChromeOptions options = new ChromeOptions();
options.setArguments("--log-level=1");
or from (https://www.chromium.org/for-testers/enable-logging)
To enable logging, launch Chrome with these command line flags:
--enable-logging --v=1
Which translates into:
options.setArguments("--enable-logging --v=1");

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

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

Passing options to chrome driver selenium

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.

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