001/* 002 * (C) Copyright 2015-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.functionaltests; 020 021import static org.junit.Assert.fail; 022 023import java.util.List; 024 025import org.openqa.selenium.WebDriver; 026 027import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError; 028 029/** 030 * Helper class to collect JavaScript errors on a page. 031 * 032 * @since 8.1 033 */ 034public class JavaScriptErrorCollector { 035 036 protected final WebDriver driver; 037 038 public JavaScriptErrorCollector(WebDriver driver) { 039 super(); 040 this.driver = driver; 041 } 042 043 /** 044 * Throws an {@link AssertionError} when JavaScript errors are detected on current page. 045 */ 046 public void checkForErrors() { 047 if (driver != null) { 048 List<JavaScriptError> jsErrors = JavaScriptError.readErrors(driver); 049 if (jsErrors != null && !jsErrors.isEmpty()) { 050 StringBuilder msg = new StringBuilder(); 051 int i = 0; 052 for (JavaScriptError jsError : jsErrors) { 053 String error = jsError.getErrorMessage(); 054 // skip error which is actually a warning for FF 42 055 if (error != null && error.startsWith("mutating the [[Prototype]] of an object")) { 056 continue; 057 } 058 if (i != 0) { 059 msg.append(", "); 060 } 061 i++; 062 msg.append("\"").append(error).append("\""); 063 msg.append(" at ").append(jsError.getSourceName()); 064 msg.append(" line ").append(jsError.getLineNumber()); 065 } 066 if (i > 0) { 067 msg.append("]"); 068 msg.insert(0, jsErrors.size() + " Javascript error(s) detected: " + "["); 069 fail(msg.toString()); 070 } 071 } 072 } 073 074 } 075 076}