|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Fiddler; |
| 9 | +using OpenQA.Selenium; |
| 10 | + |
| 11 | +namespace JavaScriptErrorsExample |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A class of extension methods for a WebDriver instance. |
| 15 | + /// </summary> |
| 16 | + static class ExtensionMethods |
| 17 | + { |
| 18 | + private static TimeSpan DefaultTimeout = TimeSpan.FromSeconds(10); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Navigates to a specified URL, injecting code to capture JavaScript errors on the page. |
| 22 | + /// </summary> |
| 23 | + /// <param name="driver">The driver used to navigate to the URL.</param> |
| 24 | + /// <param name="targetUrl">The URL to navigate to.</param> |
| 25 | + public static void NavigateTo(this IWebDriver driver, string targetUrl) |
| 26 | + { |
| 27 | + string errorScript = "window.__webdriver_javascript_errors = []; window.onerror = function(errorMsg, url, line) { window.__webdriver_javascript_errors.push(errorMsg + ' (found at ' + url + ', line ' + line + ')'); };"; |
| 28 | + SessionStateHandler beforeRequestHandler = delegate(Session targetSession) |
| 29 | + { |
| 30 | + // Tell Fiddler to buffer the response so that we can modify |
| 31 | + // it before it gets back to the browser. |
| 32 | + targetSession.bBufferResponse = true; |
| 33 | + }; |
| 34 | + |
| 35 | + SessionStateHandler beforeResponseHandler = delegate(Session targetSession) |
| 36 | + { |
| 37 | + if (targetSession.fullUrl == targetUrl && |
| 38 | + targetSession.oResponse.headers.ExistsAndContains("Content-Type", "html")) |
| 39 | + { |
| 40 | + targetSession.utilDecodeResponse(); |
| 41 | + string responseBody = targetSession.GetResponseBodyAsString(); |
| 42 | + string headTag = Regex.Match(responseBody, |
| 43 | + "<head.*>", |
| 44 | + RegexOptions.IgnoreCase).ToString(); |
| 45 | + string addition = headTag + "<script>" + errorScript + "</script>"; |
| 46 | + targetSession.utilReplaceOnceInResponse(headTag, addition, false); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + FiddlerApplication.BeforeRequest += beforeRequestHandler; |
| 51 | + FiddlerApplication.BeforeResponse += beforeResponseHandler; |
| 52 | + driver.Url = targetUrl; |
| 53 | + FiddlerApplication.BeforeResponse -= beforeResponseHandler; |
| 54 | + FiddlerApplication.BeforeRequest -= beforeRequestHandler; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets the JavaScript errors on the current page. |
| 59 | + /// </summary> |
| 60 | + /// <param name="driver">The driver used to retrieve the errors.</param> |
| 61 | + /// <returns>A list of all JavaScript errors captured on the page.</returns> |
| 62 | + public static IList<string> GetJavaScriptErrors(this IWebDriver driver) |
| 63 | + { |
| 64 | + return GetJavaScriptErrors(driver, TimeSpan.FromSeconds(5)); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets the JavaScript errors on the current page. |
| 69 | + /// </summary> |
| 70 | + /// <param name="driver">The driver used to retrieve the errors.</param> |
| 71 | + /// <param name="timeout">A <see cref="TimeSpan"/> structure for the time out of the retrieval.</param> |
| 72 | + /// <returns>A list of all JavaScript errors captured on the page.</returns> |
| 73 | + public static IList<string> GetJavaScriptErrors(this IWebDriver driver, TimeSpan timeout) |
| 74 | + { |
| 75 | + string errorRetrievalScript = "var errorList = window.__webdriver_javascript_errors; window.__webdriver_javascript_errors = []; return errorList;"; |
| 76 | + DateTime endTime = DateTime.Now.Add(timeout); |
| 77 | + List<string> errorList = new List<string>(); |
| 78 | + IJavaScriptExecutor executor = driver as IJavaScriptExecutor; |
| 79 | + ReadOnlyCollection<object> returnedList = executor.ExecuteScript(errorRetrievalScript) as ReadOnlyCollection<object>; |
| 80 | + while (returnedList == null && DateTime.Now < endTime) |
| 81 | + { |
| 82 | + System.Threading.Thread.Sleep(250); |
| 83 | + returnedList = executor.ExecuteScript(errorRetrievalScript) as ReadOnlyCollection<object>; |
| 84 | + } |
| 85 | + |
| 86 | + if (returnedList == null) |
| 87 | + { |
| 88 | + return null; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + foreach (object returnedError in returnedList) |
| 93 | + { |
| 94 | + errorList.Add(returnedError.ToString()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return errorList; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments