title: Protractor afterEach extract browser.manage().logs() to a helper function
body: <p>My overall goal is to extract the browser.manage().logs() to a helper function.  I'm stepping towards this goal by including the <code>browser.manage().logs()</code> functionality inside the <code>afterEach</code> call inside the test.  </p>
  
  <p>I know that my test is throwing a console error.  </p>
  
  <p>The inline version of <code>browser.manage().logs()</code> will print the error.  Neither the <code>afterEach</code> nor the <code>checkConsole</code> will print the logs.  </p>
  
  <p>What do I need to do to fix this?<br>
  Is the inline <code>browser.manage().logs()</code> retrieving the error and wiping the log?  I've tried every combination of having them all commented out and the only one that ever works is the inline version.</p>
  
  <p>I also have <code>protractor-screenshoter-plugin</code> configured and the report it produces shows the console error under the <code>Show browsers logs</code> button.  Is this somehow grabbing the errors and preventing <code>browser.manage().logs()</code> from getting it?</p>
  
  <p>conf.ts</p>
  
  <pre><code>import { browser, Config } from 'protractor';
  
  export let config: Config = {
      plugins: [{
          package: 'protractor-screenshoter-plugin',
          screenshotPath: './protractor/screenshots',
          screenshotOnExpect: 'failure+success',
          screenshotOnSpec: 'none',
          withLogs: true,
          writeReportFreq: 'asap',
          imageToAscii: 'none',
          clearFoldersBeforeTest: true
      }],
  
      capabilities: {
          'browserName': 'chrome',
          chromeOptions: {
              args: [ '--disable-gpu', '--window-size=1440x900', '--headless'],
          },
          'moz:firefoxOptions': {
              args: [ '-headless' ],
          },
          'loggingPrefs' : {"driver": "ALL", "server": "ALL", "browser": "ALL"}
      },
  
      directConnect: true,
      framework: 'jasmine2',
  
      baseUrl: 'https://localhost/',
      rootElement: 'app',
  
      getPageTimeout: 10000,
      allScriptsTimeout: 11000,
      jasmineNodeOpts: {
          defaultTimeoutInterval: 52000,
          onComplete: null,
          showColors: true,
          includeStackTrace: true,
      },
  
      onPrepare: function() {
          // returning the promise makes protractor wait for the reporter config before executing tests
          return browser.getProcessedConfig().then(function(pconfig) {
              // console.log("pconfig", pconfig);
          });
      }
  };
  </code></pre>
  
  <p>console_helper.ts</p>
  
  <pre><code>import { browser } from 'protractor';
  
  export let checkConsole = () =&gt; {
      browser.manage().logs().get('browser').then((browserLog: any) =&gt; {
          // this also fails to print the error message
          console.log("browserLog", browserLog);
     });
  }
  </code></pre>
  
  <p>project.ts</p>
  
  <pre><code>import { browser, element, by, ExpectedConditions } from 'protractor';
  const EC = ExpectedConditions;
  
  import { checkConsole } from "./helpers/console_helper";
  
  
  afterEach(function() {
      browser.manage().logs().get('browser').then(function(browserLogs) {
          // this fails to print the error message
          console.log("browserLogs", browserLogs); 
      });
  });
  
  it("should click on the button and redirect", () =&gt; {
      browser.get('/url-it-needs');
      browser.wait(EC.urlContains('/url-it-needs'), 5000);
      let button = element(by.cssContainingText('button', 'BUTTON TEXT'));
      browser.wait(EC.visibilityOf(button), 5000);
      browser.wait(EC.elementToBeClickable(button), 5000);
      button.click();
      browser.sleep(500);  // wait for the error to be returned from the backend
      /* 
      // this will print the proper error message
     browser.manage().logs().get('browser').then(function(browserLogs) {
            console.log("browserlogs Inline", browserLogs);
       });  */
  
     // this times out
     browser.wait(browser.getCurrentUrl().then((url: string) =&gt; {
         console.log("URL after click", url);
         expect(url).toMatch("/properUrl");
     }, 5000);
    });
  </code></pre>