javajenkinswebdriverjunit4maven-surefire-plugin

Linking Surefire Test Results to WebDriver Screenshots within Jenkins


The Context I have implemented a Test Suite using Java, JUnit and Selenium WebDriver to automate the testing of a Web Application. The Test Suite is deployed as a Maven Job within Jenkins which rebuilds on changes to the TestSuite or SUT. Jenkins also runs the TestSuite for a number of top-level maven jobs for test cases to continuously test the target system using different browsers, both on a schedule and on demand. The Surefire plugin is used to make the test results visible and the Emma plugin for Test Coverage.

When a JUnit @Test fails (i.e. detects a bug in the SUT, not a test suite error) the suite takes a screenshot of the Browser using the WebDriver feature which it persists into a folder under the Jenkins workspace. The screenshot filename is a UUID which is recorded in the test log. This all works very well overall.

The Question The screenshots are visible by drilling into the workspace via Jenkins UI. However, this is a clumsy mechanism. I want to find a way within the Jenkins UI to hyperlink either the Surefire results and/or the console output to the specific screenshot. e.g. by clicking the UUID in the log.


Solution

  • Excellent question. I just went through implementing something similar. While this is not exactly what you're looking for, it does make the screenshots available in the Jenkins UI (as opposed to file system access).

    The first thing you'll need is the Jenkins JUnit Attachment plugin: https://wiki.jenkins-ci.org/display/JENKINS/JUnit+Attachments+Plugin

    I have an existing question out regarding a bug where the files aren't able to be viewed: Jenkins JUnit Attachments Plugin throws 404 for attached files

    However, I managed to recompile the Maven plugin and got it working. I'd be happy to provide instructions, or to send you the compiled .hpi file (it's a one line change).

    Basically I include this code in my tests to capture a screenshot when the test fails (and save it in the correct directory (as required by the attachment plugin). It also names the screenshot after the failed test:

    @Rule
    public TestRule watcher = new TestWatcher() {
        @Override
        public void finished(Description description) {
            driver.quit();
        }
    
        @Override
        public void failed(Throwable e, Description description) {
            try {
                File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    
                String filePathRoot = "C:\\_Jenkins\\workspace\\" + jenkinsJobName + "\\target\\surefire-reports\\";
                String fullFilePath = filePathRoot + description.getClassName() + "\\" + description.getMethodName() + ".jpg";
    
                FileUtils.copyFile(screenshot, new File(fullFilePath));
            } catch(Exception ex) {
                System.out.println(ex.toString());
                System.out.println(ex.getMessage());
            }
    
            driver.quit();
        }
    };
    

    If the test happens to succeed, the "finished" method is called instead.

    The end result is a screenshot attached to a jenkins build whenever a test fails.

    n.b. All of my tests are run on a Windows 7 VM (slave), so any path you see is specifically for Windows.