在Selenium WebDriver中捕获JavaScript错误——浏览器独立。

时间:2022-10-28 15:01:18

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.

我想知道是否有人可以对这些尝试进行扩展,或者有其他的方法来捕捉JS错误,这些方法可以在Firefox、Chrome、Internet Explorer和Safari中使用。

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

这里是迄今为止尝试过的尝试:尝试-问题:

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

JSErrorCollector。jar -运行良好,但是是Firefox唯一的解决方案。

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.

将JS注入页面源- I注入窗口。使用WebDriver将onerror代码插入到页面的源代码中,但是由于注入太晚,所以会漏掉任何初始错误。

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)

BrowserMob—我可以拦截HTTP响应并计划注入窗口。onerror代码转换为响应体,但是作者还没有实现getBody()方法,所以我知道只有报头可以修改。所有响应的主体都是空的。(我在一个网页上,作者谈到了实现getBody(),但是还没有实现,我再也找不到)

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

Fiddler - JS将正确注入,但是Fiddler是Windows,所以Safari不能工作。

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.

父/子窗口——我使用javascript打开并存储对测试页面窗口的引用。窗外。onerror代码包含在父窗口中,因此不会错过子窗口中的启动错误。除了Firefox和Chome,我什么都做不到。我在这里已经问过一个问题了。

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.

Selenium RC——我还没有尝试它,因为我所有的测试都使用WebDriver,但是我知道它有一些类似captureNetworkTraffic()的方法,但是我认为它不能用于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.

IE错误弹出-我将使用Firefox/Chrome的父/子解决方案,然后查找IE错误弹出。当选中设置时,这个弹出窗口将显示它。弹出窗口是一个本机窗口(我认为),所以我不能使用selenium访问它。

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.

阅读浏览器控制台——我无法在所有浏览器中找到这样做的方法。在Chrome中,我找到了一种方法,将控制台日志保存到文件中,然后读取文件。这就是我所得到的。

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

我想要一个类似于BrowserMob的解决方案,因为它看起来像是一个跨浏览器的解决方案。是否有其他代理可以放入测试并拦截响应?如果实现了getBody()方法,那将会非常棒。我也喜欢父/子解决方案,因为它看起来也是一个简单的跨浏览器解决方案,但是它对IE不起作用(父/子问题)。

Thanks for any help.

感谢任何帮助。

1 个解决方案

#1


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?

我不知道有什么方法可以通过测试框架直接捕获Javascript代码错误。如果我猜的话,我会用鬼机器人。或者,像MITM代理这样的东西会有用吗?

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.

作为旁注,如果您使用一个单独的节点运行Selenium2网格集线器,您可以将一个Java选项传递给节点的JVM,就像这样,这将允许通过Fiddler代理工作。Fiddler在端口8888上监听(默认情况下)。使用此方法,您可以监视包。

:: 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.

我创建了可以在这里启动网格和节点的脚本。在我看来,您可以使用这个方法来与8080端口上的BrowserMob代理对话?我还没试过。

#1


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?

我不知道有什么方法可以通过测试框架直接捕获Javascript代码错误。如果我猜的话,我会用鬼机器人。或者,像MITM代理这样的东西会有用吗?

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.

作为旁注,如果您使用一个单独的节点运行Selenium2网格集线器,您可以将一个Java选项传递给节点的JVM,就像这样,这将允许通过Fiddler代理工作。Fiddler在端口8888上监听(默认情况下)。使用此方法,您可以监视包。

:: 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.

我创建了可以在这里启动网格和节点的脚本。在我看来,您可以使用这个方法来与8080端口上的BrowserMob代理对话?我还没试过。