dharrya / ChromeJSErrorCollector

JSErrorCollector for Chrome
GNU General Public License v2.0
14 stars 8 forks source link

Merge with JSErrorCollector? #2

Open mguillem opened 11 years ago

mguillem commented 11 years ago

Not an issue, just an idea.

I'm the author of JSErrorCollector. Would you be interested to merge ChromeJSErrorCollector into JSErrorCollector? We could then give a unified API to WebDriver users to track JS errors in FF and Chrome (and who knows, later in other browsers?). Would this make sense to you?

dharrya commented 11 years ago

Yes, i think it will be great! Honest, I specifically did receive errors from the WebDriver compatible with yours;-) You think the using of browser extensions for track JS errors still actual? I mean, that JsonWiredProtocol have native support for obtaining the log: https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/log For example, Python code (since 2.34):

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://stuff-dharrya.rhcloud.com/get_external_js_error')
driver.get('http://stuff-dharrya.rhcloud.com/get_js_error')
print(driver.get_log('browser'))
driver.quit()

Will output:

$./sample.py 
[{u'timestamp': 1377549098775, u'message': u'http://pocs-dharrya.rhcloud.com/external.js 1 Uncaught ReferenceError: foo is not defined', u'level': u'SEVERE'}, {u'timestamp': 1377549099244, u'message': u'http://stuff-dharrya.rhcloud.com/get_js_error 9 Uncaught ReferenceError: someVariable is not defined', u'level': u'SEVERE'}]

And Java:

package org.selenium.chrome_debugger;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.Logs;

import java.util.List;

public class SeleniumTest {

    public static void main(String[] args) {
        final WebDriver driver = new ChromeDriver();
        driver.get("http://stuff-dharrya.rhcloud.com/get_external_js_error");
        driver.get("http://stuff-dharrya.rhcloud.com/get_js_error");
        Logs log = driver.manage().logs();
        List<LogEntry> logsEntries = log.get("browser").getAll();
        for (LogEntry entry: logsEntries) {
            System.out.println(entry.getMessage());
        }
        driver.quit();
    }
}

Output:

Starting ChromeDriver (v2.2) on port 26315
http://pocs-dharrya.rhcloud.com/external.js 1 Uncaught ReferenceError: foo is not defined
http://stuff-dharrya.rhcloud.com/get_js_error 9 Uncaught ReferenceError: someVariable is not defined

And phpunit-selenium (i'm waiting for merge with my pull request Added command session::log() and session::logTypes()):

<?php
class WebTest
    extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function setUp()
    {
        $this->setBrowser('chrome');
        $this->setBrowserUrl('http://stuff-dharrya.rhcloud.com');
    }

    public function testTitle()
    {
        $this->url('/get_external_js_error');
        $this->url('/get_js_error');
        print_r($this->log('browser'));
    }
}

Output:

$ phpunit test.php
PHPUnit 3.8-g5514f28 by Sebastian Bergmann.

Configuration read from /home/dharrya/work/bitrix/php-omut/phpunit.xml

.Array
(
    [0] => Array
        (
            [message] => http://pocs-dharrya.rhcloud.com/external.js 1 Uncaught ReferenceError: foo is not defined
            [timestamp] => 1377549974941
            [level] => SEVERE
        )

    [1] => Array
        (
            [message] => http://stuff-dharrya.rhcloud.com/get_js_error 9 Uncaught ReferenceError: someVariable is not defined
            [timestamp] => 1377549975282
            [level] => SEVERE
        )

)

Time: 2.81 seconds, Memory: 3.75Mb

OK (1 test, 0 assertions)

I'm not sure about .NET bindings, but I think it's a matter of time.

mguillem commented 11 years ago

I didn't know that logging was that far now. Indeed it seems that rather than a browser specific extension, parsing the log (what would be probably browser specific) to extract the JS errors would be enough.

dharrya commented 11 years ago

You are right, the logs is specific to each browser, but the differences are minor. For example:

package org.selenium.chrome_debugger;

import com.opera.core.systems.OperaDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.Logs;

import java.util.List;

public class SeleniumTest {

    public static void main(String[] args) {
        testLogs(new FirefoxDriver(), "browser", "SEVERE");
        testLogs(new ChromeDriver(), "browser", "SEVERE");
        testLogs(new OperaDriver(), "ecmascript", "WARNING");
    }

    private static void testLogs(final WebDriver driver, final String logType, final String level) {
        System.out.println(driver.getClass().toString() + ":");
        driver.get("http://stuff-dharrya.rhcloud.com/get_external_js_error");
        driver.get("http://stuff-dharrya.rhcloud.com/get_js_error");
        Logs log = driver.manage().logs();
        List<LogEntry> logsEntries = log.get(logType).getAll();
        for (LogEntry entry: logsEntries) {
            if (entry.getLevel().toString().equals(level))
                System.out.println(entry.getMessage());
        }
        System.out.println();
        driver.quit();
    }
}

Will output:

class org.openqa.selenium.firefox.FirefoxDriver:
ReferenceError: foo is not defined
ReferenceError: someVariable is not defined

Starting ChromeDriver (v2.2) on port 30238
class org.openqa.selenium.chrome.ChromeDriver:
http://pocs-dharrya.rhcloud.com/external.js 1 Uncaught ReferenceError: foo is not defined
http://stuff-dharrya.rhcloud.com/get_js_error 9 Uncaught ReferenceError: someVariable is not defined

class com.opera.core.systems.OperaDriver:
Uncaught exception: ReferenceError: Undefined variable: foo

Error thrown at line 1, column 0 in http://pocs-dharrya.rhcloud.com/external.js:
    window.notExisting = foo();

Uncaught exception: ReferenceError: Undefined variable: someVariable

Error thrown at line 9, column 1 in http://stuff-dharrya.rhcloud.com/get_js_error:
    someVariable

Maybe better way - write cross-browser helpers for work with this logs?

mguillem commented 11 years ago

Probably. But they need to be cross languages too: I'm interested in Java and you seem to be interested by different languages.

Paul-Oginni commented 9 years ago

@dharrya,

Thank you so much dharrya. Your PHP code really helped out!

And thanks @mguillem for developing this useful extension.

PurpleCatfish commented 9 years ago

Hello All,

I have used your extension to extend https://github.com/grinslife/JSErrorCollector.NET I have got your source code and put into that project. (without extension)

Thanks to @mguillem ; @dharrya and @protectedtrust In case you decided to merge Firefox and Chrome JS code, it would be grait.

P.S. Plan to add Chrome support to @mguillem source code.

PurpleCatfish commented 9 years ago

@dharrya In the C# fork there is no such ability to get chrome get log