How to access chromedriver logs for Protractor test
Asked Answered
I

8

8

I have seen that chromedriver can output a logfile (https://sites.google.com/a/chromium.org/chromedriver/logging)

This page shows how to set this up when executing the exe directly:

chromedriver.exe --verbose --log-path=chromedriver.log

I cannot figure out how to set this up in Protractor however

My current protractor.conf.js

require('babel/register');

exports.config = {
    framework: 'jasmine2',
    seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar'
};

From @alecxe's answer below and protractor's browser setup docs I tried adding the following (with and without --s) but with no apparent effect:

    capabilities: {
        browserName: "chrome",
        chromeOptions: {
            args: [
                "--verbose",
                "--log-path=chromedriver.log"
            ]
        }
    }

I also tried specifying an absolute path (log-path=/chromedriver.log) which also didn't work.

Isomerous answered 27/7, 2015 at 20:37 Comment(1)
(relevant GitHub issue: github.com/angular/protractor/issues/…)Isomerous
D
9

You can always start up your own instance of chromedriver in a separate process and tell Protractor to connect to that. For example, if you start chromedriver with:

chromedriver --port=9515 --verbose --log-path=chromedriver.log

Then you could use a configuration file for Protractor like so:

   exports.config = {
     seleniumAddress: 'http://localhost:9515',
     capabilities: {
       'browserName': 'chrome'
     },
     specs: ['example_spec.js'],
   };
Deify answered 14/1, 2016 at 1:33 Comment(0)
U
8

We use a shell script to add chromedriver logging, among other checks. You can then point protractor at the shell script:

protractor config:

// When running chromedriver, use this script:
chromeDriver: path.resolve(topdir, 'bin/protractor-chromedriver.sh'),

bin/protractor-chromedriver.sh

TMPDIR="/tmp"
NODE_MODULES="$(dirname $0)/../node_modules"
CHROMEDRIVER="${NODE_MODULES}/protractor/selenium/chromedriver"
LOG="${TMPDIR}/chromedriver.$$.log"

fatal() {
    # Dump to stderr because that seems reasonable
    echo >&2 "$0: ERROR: $*"
    # Dump to a logfile because webdriver redirects stderr to /dev/null (?!)
    echo >"${LOG}" "$0: ERROR: $*"
    exit 11
}


[ ! -x "$CHROMEDRIVER" ] && fatal "Cannot find chromedriver: $CHROMEDRIVER"

exec "${CHROMEDRIVER}" --verbose --log-path="${LOG}" "$@"
Unsavory answered 5/8, 2015 at 19:16 Comment(4)
Where does the protractor-chromedriver.sh file go?University
Answering my own query in the comment: I placed the protractor-chromedriver.sh in the same directory as protractor.conf.jsUniversity
The problem is that the original question is based on a Windows system, which won't allow this solution with the bash shell code to run. Do you have a solution that uses a BAT file?Kulda
Thanks for the great work @Unsavory ... I expanded on your solution and made it work for Windows 7 (somewhere appearing down below)...Kulda
N
5

According to the protractor's source code, chromedriver service is started without any arguments and there is no direct way to configure the arguments. Even though the chromedriver's Service Builder that protractor uses actually has an ability to specify the verbosity and the log path:

var service = new chrome.ServiceBuilder()
    .loggingTo('/my/log/file.txt')
    .enableVerboseLogging()
    .build();

Old (incorrect) answer:

You need to set the chrome arguments:

capabilities: {
    browserName: "chrome",
    chromeOptions: {
        args: [
            "verbose", 
            "log-path=chromedriver.log"
        ]
    }
},

See also:

Nelson answered 27/7, 2015 at 20:43 Comment(7)
in your referenced answer, browserName is outside of the capabilities object. Neither way seems to work for me. The linked answers approach results in Target browser must be a string, but is <object>; did you forget to call forBrowser()Isomerous
This page seems to agree with your approach (minus the --s in the args array), but still has no apparent effect for me :(Isomerous
@ZachL okay, can you post your current config? Also, have you tried to specify the absolute path to the log file?.. Thanks!Nelson
@ZachL can you also check whether the complete absolute path works for you, e.g.: /Users/user/directory/chromedriver.log? Thanks.Nelson
I exactly that, (with and without --s).Isomerous
@ZachL yeah, I am afraid args is for setting the chrome command-line switches peter.sh/experiments/chromium-command-line-switches and not the chromedriver command line arguments. I'll research it more. Thanks.Nelson
@ZachL I've added an update with some more information. What you should do next, I think, is to create an issue at protractor's bugtracker. Please throw the link here so I can subscribe. Thanks!Nelson
K
4

Since, the previous answer by @P.T. didn't work for me on Windows 7, I started with his suggestions and got it working on Windows. Here is a working solution for Windows 7 users.

STEP 1: Install BASH and JQ and confirm they are working on your Windows box

  1. Download bash (for Windows 10 https://itsfoss.com/install-bash-on-windows/ ; for Windows 7 download latest here: https://sourceforge.net/projects/win-bash/files/shell-complete/latest/ ; for Windows Server 2012 or any Windows OS that already has Git installed on it, you already have a bash.exe and sh.exe installed at C:\Program Files\Git\usr\bin or C:\Program Files (x86)\Git\usr\bin already )
  2. Install bash - For Windows 7/ download it and extract the zip files to a directory.
  3. Download jq (https://stedolan.github.io/jq/) and install it in the same directory location as bash
  4. Make SURE that you add your above directory (for Windows 7- where you extracted the bash zip files to; for other applicable OSes that have git, the path it is installed at) to your PATH system environment variable.
  5. Once the above is installed and added to your PATH, close ALL and reopen Webstorm and any CMD windows you wish to run your work in.
  6. Test that bash is actually installed by simply typing it on a windows command prompt C:\git\> bash . Doing so should produce a bash cmd prompt like this bash$

STEP 2: Add Custom Files for Redirecting Chromedriver to user Debug Logging

Add the following files to the top level of the project (wherever your protractor-conf.js is located). These files allow us to add custom debug switches to the chromedriver.exe execution.

Note that this is necessary because these switches are not exposed through protractor and cannot be done directly in the protractor.conf.js file via the chromeOptions/args flags as you would normally expect

chromedriver.cmd -- exact source shown below:

bash protractor-chromedriver.sh %*

protractor-chromedriver.sh -- exact source shown below:

TMPDIR="$(dirname $0)/tmp"
NODE_MODULES="$(dirname $0)/node_modules"
SELENIUM="${NODE_MODULES}/protractor/node_modules/webdriver-manager/selenium"
UPDATECONFIG="${SELENIUM}/update-config.json"
EXEFILENAME="$(cat ${UPDATECONFIG} | jq .chrome.last | tr -d '""')"
CHROMEDRIVER="${SELENIUM}/${EXEFILENAME##*'\\'}"
LOG="${TMPDIR}/chromedriver.$$.log"

fatal() {
    # Dump to stderr because that seems reasonable
    echo >&2 "$0: ERROR: $*"
    # Dump to a logfile because webdriver redirects stderr to /dev/null (?!)
    echo >"${LOG}" "$0: ERROR: $*"
    exit 11
}


[ ! -x "$CHROMEDRIVER" ] && fatal "Cannot find chromedriver: $CHROMEDRIVER"

exec "${CHROMEDRIVER}" --verbose --log-path="${LOG}" "$@"

/tmp -- create this directory at the top level of your project (same as the location of the protractor.conf.js file.

STEP 3: Update protractor.conf.js file.

In the protractor.conf.js file, add the following line as a property in the exports.config object. As in:

exports.config = {
.. ..
        chromeDriver: 'chromedriver.cmd',
.. ..

STEP 4: Launch your tests

your test should now run and if the chrome driver outputs any log information it will appear in a file called chromedriver.???.log in the tmp directory under your project.

Important caveats

This script set up assumes you install and run protractor (and the chrome driver under it) within the local node_modules directory inside your project. That is how I run my code, because I want it complete self-contained and re-generated in the build process/cycle. If you have protractor/chromedriver installed globally you should change the CHROMEDRIVER variable within the protractor-chromedriver.sh file to match your installation of protractor/chrome driver.

hope that helps.

Kulda answered 4/5, 2017 at 1:3 Comment(0)
S
2

If you're using the seleniumServerJar, in protractor.conf.js set the logfile path to wherever you want it to write the file:

seleniumArgs: [
    '-Dwebdriver.chrome.logfile=/home/myUsername/tmp/chromedriver.log',
]

If you're using webdriver-manager start to run a local selenium server, you'll need to edit the webdriver-manager file:

// insert this line
args.push('-Dwebdriver.chrome.logfile=/home/myUsername/tmp/chromedriver.log');

// this line already exists in webdriver-manager, add the push to args before this line
var seleniumProcess = spawnCommand('java', args);
Strontia answered 14/1, 2016 at 14:12 Comment(0)
R
1

In case you use webdriver-manager: webdriver manager has the chrome_logs option (you can find it in its source code (in opts.ts or opts.js in the compiled code)), so you can use it something like:

webdriver-manager start --chrome_logs /path/to/logfile.txt
Revenuer answered 22/3, 2017 at 13:48 Comment(1)
You could also find this information with webdriver-manager start help. The start help displays there is an option for --chrome_log with description "File path to chrome logs"Cellar
T
1

Old question, but I had some help from the answer. @PT and @Kim Gentes gave good pointers.

You don't need to install bash on windows. Use "start /B /W ..."

chromedriver.cmd can have something like:

start /B /W .\node_modules\protractor\node_modules\webdriver-manager\selenium\chromedriver.exe --verbose --log-path=./chromeriver.log %*
Theo answered 16/2 at 5:41 Comment(0)
N
0

I'm using this as a global afterEach hook (mocha):

afterEach(() => {
    browser.manage().logs().get('browser').then(function(browserLog) {
        if(browserLog && browserLog.length) {
            console.log('\nlog: ' + util.inspect(browserLog) + '\n');
        }
    });
});
Noranorah answered 27/5, 2016 at 17:2 Comment(1)
Where does util.inspect come from?Fortuitous

© 2022 - 2024 — McMap. All rights reserved.