12

I am wondering if anyone could expand on any of these attempts or has any other ideas for catching JS errors using WebDriver that will work in Firefox, Chrome, Internet Explorer, and Safari.

Here is what's been tried so far:
Attempt – Problem:

JSErrorCollector.jar - Works fine, but is a Firefox only solution.

Inject JS into page source – I injected window.onerror code into the page’s source code using WebDriver, but any initial errors are missed because the injection is too late.

BrowserMob – I can intercept the HTTP response and planned to inject the window.onerror code into response body, but the author has not implemented the getBody() method yet, so only headers can be modified, that I am aware of. The body is always null for all responses. (I was on a webpage where the author talked about implementing getBody() but it hasn’t happened yet and I cannot find it again)

Fiddler – JS will inject correctly, but Fiddler is Windows only so Safari won’t work.

Parent/Child windows – I use javascript to open and store a reference to the test page’s window. The window.onerror code is contained in the parent window so it will not miss startup errors in the child window. I cannot get this to work in anything but Firefox and Chome somewhat. I already asked a question about it here.

Selenium RC – I haven’t tried it because all my tests use WebDriver, but I know it has some kind of method like captureNetworkTraffic(), but I don’t think it can be used in WebDriver.

IE error popup – I was going to use the parent/child solution for Firefox/Chrome and then look for the IE error popup. This popup displays when the setting is checked to display it. The popup is a native Window window (I think) so I cannot use selenium to access it.

Read browser console – I could not find a way to do this in all browsers. In Chrome I found a way to save the console log to a file and then read the file. That is as far as I got.

I would like a solution similar to BrowserMob since it seems like it would be a cross browser solution. Are there any other proxies that can be put in the test and intercept the response? It would have been excellent if the getBody() method was implemented. I also like the parent/child solution because it also seems like a simple, cross browser solution, but it is not working for IE (parent/child question again).

Thanks for any help.

Community
  • 1
  • 1
Ryan
  • 1,135
  • 1
  • 13
  • 28
  • 1
    As far as I know, you listed all the possible solutions currently available (July 2013, Selenium 2.33.0). [See the official Selenium bug for more discussion.](http://code.google.com/p/selenium/issues/detail?id=148) Until there's any official API, you'll have to use a mix of those solutions, or probably write your own browser addon for every browser you need to support. – Petr Janeček Jul 09 '13 at 19:00
  • 2
    You realize the BrowserMob proxy is [open-source](https://github.com/lightbody/browsermob-proxy), right? What's stopping you from implementing the missing functionality and submitting a pull request? – JimEvans Jul 10 '13 at 08:05
  • Nothing, well, maybe my knowledge :p I have a time constraint, but if it is my only option then I can surely try. Thanks for the motivation. – Ryan Jul 10 '13 at 13:39
  • Browsermob proxy has not overriden the getBody method of BrowserMobHttpResponse. It returns null. Hence cannot add our own javascript. – Khushboo Jun 11 '15 at 11:16

1 Answers1

1

I don't know of any way to directly catch Javascript code errors by a test framework. If I were to guess, I would use PhantomJS. Or, maybe something like MITM Proxy would work?

As a sidenote, if you run Selenium2 Grid Hub with a separate Node, you can pass a Java option to the JVM of the node like this that will allow a proxy through Fiddler to work. Fiddler listens (by default) on port 8888. With this method you can watch packets.

:: batch script: Set JAVA_OPTS java options to JVM
SET "JAVA_OPTS=-Dwebdriver.chrome.^
 driver=%CHROMEDRIVER%"
IF "%PROXY_TO_FIDDLER%"=="true" SET "JAVA_OPTS=%JAVA_OPTS% -DproxySet=true^
 -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888"

I created scripts you can use to start your grid and node here. It seems to me that you could use this method to also talk to BrowserMob proxy on port 8080? I have not tried that.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • I am using Fiddler right now to watch packets, but I do not have a grid setup. I will look into your code. Thanks for the answer/code. – Ryan Jul 26 '13 at 20:33
  • I would guess that page source does not pass between the grid hub and node though. Each web browser session on the node instance that encounters a Javascript error is in its own sandbox. Your idea of using JQuery injection seems to me the only way to do it but i know you said that it does not catch errors early enough. I am theorizing, but perhaps you could load your page in a iFrame wrapper test page frame that pre-loads jquery and controls/watches your page loading within its iframe? In that case your test driver would have to do a switchTo to get into the frame. – djangofan Jul 26 '13 at 21:49