1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui

File TestDebugger.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
23
7
1
148
68
10
0.43
3.29
7
1.43

Classes

Class Line # Actions
TestDebugger 53 23 0% 10 6
0.823529482.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.ui;
21   
22    import java.io.File;
23   
24    import org.apache.commons.io.FileUtils;
25    import org.junit.rules.TestWatcher;
26    import org.junit.runner.Description;
27    import org.openqa.selenium.OutputType;
28    import org.openqa.selenium.TakesScreenshot;
29    import org.openqa.selenium.WebDriver;
30    import org.slf4j.Logger;
31    import org.slf4j.LoggerFactory;
32   
33    /**
34    * Generates debugging information on test failure:
35    * <ul>
36    * <li>captures a screenshot of the browser window</li>
37    * <li>logs the URL of the current page</li>
38    * <li>logs the source of the current page</li>
39    * </ul>
40    * NOTE: The reason we also log when a test starts and passes is simply to overcome a deficiency in error reporting in
41    * Jenkins. The reason is that Jenkins bases its test reporting on the Maven Surefire plugin reporting which itself is
42    * using a file to report test status. Since ui-tests are using a test suite, {@link PageObjectSuite}, there's only a
43    * single file generated and it's only generated when all tests have finished executing. Thus if a test hangs there
44    * won't be any file generated and looking at the Jenkins UI it won't be possible to see which tests have executed.
45    * <p>
46    * Normally each JUnit Test Runner knows what test is executing and when it's finished and thus can report them in its
47    * own console (as this is the case for IDEs for example). Again the issue here is that Jenkins doesn't have any JUnit
48    * Test Runner but instead is calling JUnit by delegation to the Maven Surefire plugin.
49    *
50    * @version $Id: 113fd89702dfca6ab833b73dc87675edd31de43f $
51    * @since 4.3
52    */
 
53    public class TestDebugger extends TestWatcher
54    {
55    /**
56    * Logging helper object.
57    */
58    private static final Logger LOGGER = LoggerFactory.getLogger(TestDebugger.class);
59   
60    /**
61    * The folder where to save the screenshot.
62    */
63    private static final String SCREENSHOT_DIR = System.getProperty("screenshotDirectory");
64   
65    /**
66    * The object used to get the debugging information on test failure.
67    */
68    private final WebDriver driver;
69   
70    /**
71    * Creates a new test rule that generates debugging information on test failure.
72    *
73    * @param driver the object used to get the debugging information on test failure
74    */
 
75  577 toggle public TestDebugger(WebDriver driver)
76    {
77  577 this.driver = driver;
78    }
79   
 
80  577 toggle @Override
81    protected void starting(Description description)
82    {
83  577 LOGGER.info("{} started", getTestName(description));
84    }
85   
 
86  567 toggle @Override
87    protected void succeeded(Description description)
88    {
89  567 LOGGER.info("{} passed", getTestName(description));
90    }
91   
 
92  10 toggle @Override
93    protected void failed(Throwable e, Description description)
94    {
95  10 LOGGER.info("{} failed", getTestName(description));
96  10 takeScreenshot(description);
97  10 LOGGER.info("Current page URL is [{}]", driver.getCurrentUrl());
98  10 LOGGER.info("Current page source is [{}]", driver.getPageSource());
99    }
100   
101    /**
102    * @param description the test description
103    * @return the test name (using the format TestSimpleClassName-TestMethodName)
104    */
 
105  1164 toggle private String getTestName(Description description)
106    {
107  1164 return description.getTestClass().getSimpleName() + "-" + description.getMethodName();
108    }
109   
110    /**
111    * Captures a screenshot of the browser window.
112    *
113    * @param description the description of the failing test
114    */
 
115  10 toggle private void takeScreenshot(Description description)
116    {
117  10 takeScreenshot(getTestName(description));
118    }
119   
120    /**
121    * Captures a screenshot of the browser window.
122    *
123    * @param testName the name of the file in which the screenshot will be taken. A ".png" suffix will be appended
124    */
 
125  10 toggle public void takeScreenshot(String testName)
126    {
127  10 if (!(driver instanceof TakesScreenshot)) {
128  0 LOGGER.warn("The WebDriver that is currently used doesn't support taking screenshots.");
129  0 return;
130    }
131   
132  10 try {
133  10 File sourceFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
134  10 File screenshotFile;
135  10 if (SCREENSHOT_DIR != null) {
136  10 File screenshotDir = new File(SCREENSHOT_DIR);
137  10 screenshotDir.mkdirs();
138  10 screenshotFile = new File(screenshotDir, testName + ".png");
139    } else {
140  0 screenshotFile = new File(new File(System.getProperty("java.io.tmpdir")), testName + ".png");
141    }
142  10 FileUtils.copyFile(sourceFile, screenshotFile);
143  10 LOGGER.info("Screenshot for failing test [{}] saved at [{}].", testName, screenshotFile.getAbsolutePath());
144    } catch (Exception e) {
145  0 LOGGER.error("Failed to take screenshot for failing test [{}].", testName, e);
146    }
147    }
148    }