View Javadoc

1   package com.atlassian.webdriver.debug;
2   
3   import com.atlassian.webdriver.utils.WebDriverUtil;
4   
5   import com.google.common.base.Function;
6   import com.google.common.base.Supplier;
7   import com.google.common.collect.ImmutableList;
8   
9   import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
10  
11  import org.openqa.selenium.WebDriver;
12  import org.openqa.selenium.firefox.FirefoxDriver;
13  
14  import static com.google.common.collect.Iterables.transform;
15  
16  /**
17   * Default implementation of {@link JavaScriptErrorRetriever}, using the JSErrorCollector
18   * library.
19   * @since 2.3
20   */
21  public class DefaultJavaScriptErrorRetriever implements JavaScriptErrorRetriever
22  {
23      private final Supplier<? extends WebDriver> webDriver;
24  
25      public DefaultJavaScriptErrorRetriever(Supplier<? extends WebDriver> webDriver)
26      {
27          this.webDriver = webDriver;
28      }
29  
30      @Override
31      public boolean isErrorRetrievalSupported()
32      {
33          return WebDriverUtil.isInstance(webDriver.get(), FirefoxDriver.class);
34      }
35      
36      @Override
37      public Iterable<JavaScriptErrorInfo> getErrors()
38      {
39          if (isErrorRetrievalSupported())
40          {
41              return transform(JavaScriptError.readErrors(webDriver.get()), new Function<JavaScriptError, JavaScriptErrorInfo>()
42              {
43                  public JavaScriptErrorInfo apply(final JavaScriptError e)
44                  {
45                      return new JavaScriptErrorInfo()
46                      {
47                          public String getDescription()
48                          {
49                              return e.toString();
50                          }
51      
52                          public String getMessage()
53                          {
54                              return e.getErrorMessage();
55                          }
56                      };
57                  }
58              });
59          }
60          else
61          {
62              return ImmutableList.of();
63          }
64      }
65  }