View Javadoc

1   package com.atlassian.webdriver.it.tests;
2   
3   import com.atlassian.pageobjects.Page;
4   import com.atlassian.pageobjects.browser.Browser;
5   import com.atlassian.pageobjects.browser.RequireBrowser;
6   import com.atlassian.webdriver.it.AbstractSimpleServerTest;
7   import com.atlassian.webdriver.it.pageobjects.page.jsconsolelogging.IncludedScriptErrorPage;
8   import com.atlassian.webdriver.it.pageobjects.page.jsconsolelogging.NoErrorsPage;
9   import com.atlassian.webdriver.it.pageobjects.page.jsconsolelogging.UntypedErrorPage;
10  import com.atlassian.webdriver.it.pageobjects.page.jsconsolelogging.WindowErrorPage;
11  import com.atlassian.webdriver.testing.rule.JavaScriptErrorsRule;
12  import com.google.common.collect.ImmutableSet;
13  
14  import org.junit.Before;
15  import org.junit.Ignore;
16  import org.junit.Test;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.containsString;
20  import static org.hamcrest.Matchers.equalTo;
21  import static org.hamcrest.Matchers.not;
22  
23  /**
24   * Test the rule for logging client-side console output.
25   *
26   * At the time of writing, the logging only captures exceptions.
27   * The tests only work in Firefox, since the rule uses a Firefox extension to get the output.
28   */
29  @RequireBrowser(Browser.FIREFOX)
30  public class TestCapturingJavaScriptConsoleOutput extends AbstractSimpleServerTest
31  {
32      private static final String UNCAUGHT_EXCEPTION_THROW_STRING = "uncaught exception: throw string";
33  
34      private JavaScriptErrorsRule rule;
35  
36      @Before
37      public void setUp()
38      {
39          rule = new JavaScriptErrorsRule();
40      }
41  
42      @Test
43      public void testPageWithNoErrors()
44      {
45          product.visit(NoErrorsPage.class);
46          final String consoleOutput = rule.getConsoleOutput();
47          assertThat(consoleOutput, equalTo(""));
48      }
49  
50      @Test
51      public void testSingleErrorInWindowScope()
52      {
53          final Page page = product.visit(WindowErrorPage.class);
54          final String consoleOutput = rule.getConsoleOutput();
55          assertThat(consoleOutput, containsString("ReferenceError: foo is not defined"));
56          assertThat(consoleOutput, containsString(page.getUrl()));
57      }
58  
59      @Test
60      public void testMultipleErrorsInIncludedScripts()
61      {
62          final IncludedScriptErrorPage page = product.visit(IncludedScriptErrorPage.class);
63          final String consoleOutput = rule.getConsoleOutput();
64          assertThat(consoleOutput, containsString("TypeError: $ is not a function"));
65          assertThat(consoleOutput, containsString(page.objectIsNotFunctionScriptUrl()));
66  
67          assertThat(consoleOutput, containsString("Error: throw Error('bail')"));
68          assertThat(consoleOutput, containsString(page.throwErrorObjectScriptUrl()));
69      }
70  
71      @Test
72      public void testEachJSErrorIsOnANewLine()
73      {
74          final IncludedScriptErrorPage page = product.visit(IncludedScriptErrorPage.class);
75          final String consoleOutput = rule.getConsoleOutput();
76          final String[] errors = consoleOutput.split("\n");
77  
78          assertThat(errors.length, equalTo(3));
79          // We get “unreachable code after return statement” warnings from Firefox 40 onwards;
80          // see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return$revision/945829#Automatic_Semicolon_Insertion>.
81          assertThat(errors[0], containsString("jquery-1.4.2.js"));
82          assertThat(errors[1], containsString(page.objectIsNotFunctionScriptUrl()));
83          assertThat(errors[2], containsString(page.throwErrorObjectScriptUrl()));
84      }
85  
86      @Test
87      public void testCanCaptureUntypedErrors()
88      {
89          product.visit(UntypedErrorPage.class);
90          final String consoleOutput = rule.getConsoleOutput();
91          assertThat(consoleOutput, containsString(UNCAUGHT_EXCEPTION_THROW_STRING));
92      }
93  
94      @Test
95      public void testCanBeOverriddenToIgnoreSpecificErrors()
96      {
97          rule = rule.errorsToIgnore(ImmutableSet.of(UNCAUGHT_EXCEPTION_THROW_STRING));
98          product.visit(UntypedErrorPage.class);
99          final String consoleOutput = rule.getConsoleOutput();
100         assertThat(consoleOutput, not(containsString(UNCAUGHT_EXCEPTION_THROW_STRING)));
101     }
102 
103     @Ignore("The JSErrorCollector plugin currently cannot capture console errors")
104     @Test
105     public void testCanCaptureConsoleErrors()
106     {
107         final UntypedErrorPage page = product.visit(UntypedErrorPage.class);
108         final String consoleOutput = rule.getConsoleOutput();
109         assertThat(consoleOutput, containsString("console.error"));
110         assertThat(consoleOutput, containsString(page.consoleErrorScriptUrl()));
111     }
112 
113     @Test
114     public void testCurrentlyCannotCaptureConsoleErrors()
115     {
116         final UntypedErrorPage page = product.visit(UntypedErrorPage.class);
117         final String consoleOutput = rule.getConsoleOutput();
118         assertThat(consoleOutput, not(containsString("console.error")));
119         assertThat(consoleOutput, not(containsString(page.consoleErrorScriptUrl())));
120     }
121 }