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.isEmptyString;
22  import static org.hamcrest.Matchers.not;
23  
24  /**
25   * Test the rule for logging client-side console output.
26   *
27   * At the time of writing, the logging only captures exceptions.
28   * The tests only work in Firefox, since the rule uses a Firefox extension to get the output.
29   */
30  @RequireBrowser(Browser.FIREFOX)
31  public class TestCapturingJavaScriptConsoleOutput extends AbstractSimpleServerTest
32  {
33      private JavaScriptErrorsRule rule;
34  
35      @Before
36      public void setUp()
37      {
38          rule = new JavaScriptErrorsRule();
39      }
40  
41      @Test
42      public void testPageWithNoErrors()
43      {
44          product.visit(NoErrorsPage.class);
45          final String consoleOutput = rule.getConsoleOutput();
46          assertThat(consoleOutput, equalTo(""));
47      }
48  
49      @Test
50      public void testSingleErrorInWindowScope()
51      {
52          final Page page = product.visit(WindowErrorPage.class);
53          final String consoleOutput = rule.getConsoleOutput();
54          assertThat(consoleOutput, containsString("ReferenceError: foo is not defined"));
55          assertThat(consoleOutput, containsString(page.getUrl()));
56      }
57  
58      @Test
59      public void testMultipleErrorsInIncludedScripts()
60      {
61          final IncludedScriptErrorPage page = product.visit(IncludedScriptErrorPage.class);
62          final String consoleOutput = rule.getConsoleOutput();
63          assertThat(consoleOutput, containsString("TypeError: $ is not a function"));
64          assertThat(consoleOutput, containsString(page.objectIsNotFunctionScriptUrl()));
65  
66          assertThat(consoleOutput, containsString("Error: throw Error('bail')"));
67          assertThat(consoleOutput, containsString(page.throwErrorObjectScriptUrl()));
68      }
69  
70      @Test
71      public void testEachJSErrorIsOnANewLine()
72      {
73          final IncludedScriptErrorPage page = product.visit(IncludedScriptErrorPage.class);
74          final String consoleOutput = rule.getConsoleOutput();
75          final String[] errors = consoleOutput.split("\n");
76  
77          assertThat(errors.length, equalTo(2));
78          assertThat(errors[0], containsString(page.objectIsNotFunctionScriptUrl()));
79          assertThat(errors[1], containsString(page.throwErrorObjectScriptUrl()));
80      }
81  
82      @Test
83      public void testCanCaptureUntypedErrors()
84      {
85          product.visit(UntypedErrorPage.class);
86          final String consoleOutput = rule.getConsoleOutput();
87          assertThat(consoleOutput, containsString("uncaught exception: throw string"));
88      }
89  
90      @Test
91      public void testCanBeOverriddenToIgnoreSpecificErrors()
92      {
93          rule = rule.errorsToIgnore(ImmutableSet.of("uncaught exception: throw string"));
94          product.visit(UntypedErrorPage.class);
95          final String consoleOutput = rule.getConsoleOutput();
96          assertThat(consoleOutput, isEmptyString());
97      }
98  
99      @Ignore("The JSErrorCollector plugin currently cannot capture console errors")
100     @Test
101     public void testCanCaptureConsoleErrors()
102     {
103         final UntypedErrorPage page = product.visit(UntypedErrorPage.class);
104         final String consoleOutput = rule.getConsoleOutput();
105         assertThat(consoleOutput, containsString("console.error"));
106         assertThat(consoleOutput, containsString(page.consoleErrorScriptUrl()));
107     }
108 
109     @Test
110     public void testCurrentlyCannotCaptureConsoleErrors()
111     {
112         final UntypedErrorPage page = product.visit(UntypedErrorPage.class);
113         final String consoleOutput = rule.getConsoleOutput();
114         assertThat(consoleOutput, not(containsString("console.error")));
115         assertThat(consoleOutput, not(containsString(page.consoleErrorScriptUrl())));
116     }
117 }