OnErrorã使ã£ã¦æ¤ç¥
<script type="text/javascript"> window.javaScriptErrors = []; window.onerror = function(errorMessage) { window.javaScriptErrors[window.javaScriptErrors.length] = errorMessage; } </script>ãã®ãããªå 容ãç¨æå®é以ä¸ã®HTMLã®ç¨ã«ãªã。
index_error.html
<html> <head> <title>Seleniumã使ã£ã¦JavaScriptã®ã¨ã©ã¼ãæ¤ç¥ããã</title> <script type="text/javascript">window.javaScriptErrors = []; window.onerror = function(errorMessage) { window.javaScriptErrors[window.javaScriptErrors.length] = errorMessage; } </script> <script type="text/javascript"> alert(a); </script> </head> <body> ããã¨ã¨ã©ã¼ãåºã. </body> </html>
ã§ã¯Javaå´ã®å®è£ ã§ã。 é¢åãªã®ã§Mavenã§è¡ãã¾ã.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.blogspot.tech.joke</groupId> <artifactId>selenium</artifactId> <version>0.0.1-SNAPSHOT</version> <name>selenium</name> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.38.0</version> <systemPath></systemPath> </dependency> </dependencies> </project>
ãã¡ãã®ããã¸ã§ã¯ããä½æã以ä¸ã®Javaãä½æ.
Main.java
package com.blogspot.tech.joke.selenium; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Main { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://localhost/index_error.html"); JavascriptExecutor executor = (JavascriptExecutor) driver; System.out.println(executor.executeScript("return window.javaScriptErrors")); driver.quit(); } }ãã¡ãã®Javaãå®è¡ããã¨ã¨ã©ã¼ãåºåããã¾ã。
JavaScriptã®ã¨ã©ã¼ãã¯ãã¼ãã³ã°ãç£è¦ãªã©ãä½ãããã§ãã
ãã£ã¨æ´»ç¨ã§ããããªã®ã§ããããæ¤è¨¼ä¸