Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Surefire Test Results to WebDriver Screenshots within Jenkins

The Context I have implemented a Test Suite using Java, JUnit and Selenuim 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 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 screen shot filename is a UUID which is recorded in the test log. This all works very well overall.

The Question The screen shots 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 screen shot. e.g. by clicking the UUID in the log.

like image 878
Martin Spamer Avatar asked Oct 03 '22 10:10

Martin Spamer


2 Answers

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.

like image 179
Daniel Hinton Avatar answered Oct 13 '22 11:10

Daniel Hinton


I think you need to extend Junit reports. This is already answered here. On a side note, UI tests are acceptance tests and a BDD framework like cucumber is a perfect fit for them. Take a look at Cucumber-JVM, it is tightly integrated with Junit and has excellent support for embedding screenshots to the report etc. It also gives nice visibility of your tests to business owners with jenkins plugins like this.

like image 34
nilesh Avatar answered Oct 13 '22 10:10

nilesh