Hi Jenkins masters,

I had a groovy postbuild script that I'm trying to migrate to pipeline 
groovy. 

It's setting the description of failed test (in order to embed the 
screenshot taken by our selenium tests in the jenkins failed test UI - For 
example: 
http://ci.xwiki.org/job/xwiki-enterprise-test-ui-7.4.x/org.xwiki.enterprise$xwiki-enterprise-test-ui/350/testReport/junit/org.xwiki.test.ui.appwithinminutes/WizardTest/testCreateApplication/
 
).

I've googled a lot but couldn't find any starting point to convert this 
script:

 /**
   * This script attaches the screenshot of a failing Selenium test to the 
failed test's description.
   * The screenshot is preserved after the workspace gets cleared by a new 
build.
   */
  def attachScreenshotToFailingTests() {
    def channel = manager.build.workspace.channel;
    def workspace = manager.build.workspace.toString();

    def testResults = manager.build.testResultAction;
    if (testResults == null) {
      // No tests were run in this build, nothing left to do.
      return;
    }
    
    // Go through each failed test in the current build.
    def failedTests = testResults.getFailedTests();
    for (def failedTest : failedTests) {
      // Compute the test's screenshot file name.
      def testClass = failedTest.getClassName();
      def testSimpleClass = failedTest.getSimpleName();
      def testExample = failedTest.getName();

      def suiteResultFile = failedTest.getSuiteResult().getFile();
      if (suiteResultFile == null) {
        // No results available. Go to the next test.
        continue;
      }
      
      // Compute the screenshot's location on the build agent.
      def targetFolderPath = new hudson.FilePath(channel, 
suiteResultFile).getParent().getParent();
      // The screenshot can have 2 possible file names and locations, we have 
to look for both.
      // Selenium 1 test screenshots.
      def imageAbsolutePath1 = new hudson.FilePath(targetFolderPath, 
"selenium-screenshots/${testClass}-${testExample}.png");
      // Selenium 2 test screenshots.
      def imageAbsolutePath2 = new hudson.FilePath(targetFolderPath, 
"screenshots/${testSimpleClass}-${testExample}.png");
      // Determine which one exists, if any.
      def imageAbsolutePath = imageAbsolutePath1.exists() ? imageAbsolutePath1 
: (imageAbsolutePath2.exists() ? imageAbsolutePath2 : null);

      // If the screenshot exists...
      if (imageAbsolutePath != null) {
        // Build a base64 string of the image's content.
        def imageDataStream = imageAbsolutePath.read();
        byte[] imageData = IOUtils.toByteArray(imageDataStream);
        def imageDataString = "data:image/png;base64," + 
DatatypeConverter.printBase64Binary(imageData);

        def testResultAction = failedTest.getParentAction();
        def testResult = testResultAction.getResult();

        // Build a description HTML to be set for the failing test that 
includes the image in Data URI format.
        def description = "<h3>Screenshot</h3>";
        description += "<a href=\"" + imageDataString + "\"><img style=\"width: 
800px\" src=\"" + imageDataString + "\" /></a>";

        // Set the description to the failing test and save it to disk.
        testResultAction.setDescription(failedTest, description);
        // Note: the owner field is marked as deprecated and it might go away. 
It should be replaced by an instance of Run in the future,
        // but FTM, I do not know how to access it. For the future, in case 
descriptions start not getting stored, this might cause it.
        testResultAction.owner.save();
      }
    }
  }


Any pointer would be much appreciated.

Thanks
-Vincent


-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/15ee3c93-8849-492a-8355-3b2e11c05ada%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to