Search code examples
jmeterconfluenceconfluence-rest-api

How to update Atlassian Confluence Wiki using JMeter and the REST API


I wanted a way to update a wiki status page and upload a file after a JMeter test was done running. This is something that you could conditionally kick off depending on the results of your Jenkins job.


Solution

  • I did this with these steps:

    1. in a setup thread group, added a BeanShell Sampler to locate the most recent report file in my results folder.

      import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.apache.commons.io.comparator.LastModifiedFileComparator;

      log.info("GET MOST RECENT RESULTS REPORT FOR THE APP TESTED");
      String dir_path = props.get("test_results_path");
      File theNewestFile = null;
      
       try {
         File dir = new File(dir_path);
         FileFilter fileFilter = new WildcardFileFilter("Results_${testApp}*.*");
         File[] files = dir.listFiles(fileFilter);
       if (files.length > 0) {
              /** The newest file comes first **/
              Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
              theNewestFile = files[0];
              String fileName = files[0].getName().toString();
              log.info("fileName:  "+fileName);
              print("fileName:  "+fileName);
              props.put("varResultsReportFile",fileName);
          }
      
          return theNewestFile;
      }
      catch (Throwable ex) {
         log.error("Failed in Beanshell", ex);
         throw ex;
      }
      
    2. login with a wiki/confluence system account

    3. GET rest/api/content?title=${testApp}&spaceKey=${testSpaceKey}&expand=version,history
    4. Use a JSON Extractors to extract page Version number(results..version.number) and page id(results..id)
    5. Use a BeanShell PostProcessor to add 1 to the page version number and store that value in a variable. You will need this when you PUT your update into the wiki
    6. GET rest/api/content?title=${testApp}&spaceKey=${testSpaceKey}&expand=body.storage
    7. Use JSON Extractor to extact page body value(results..body.storage.value)
    8. Using a CSS/JQuery Extractor on the JMeter Variable you created in step 7, Extract all the table values. For example,CSS/JQuery Expression=td and Match No= 1 to extract first column value.
    9. PUT rest/api/content/${varPageId} and in the JSON body, update the single table value that you need to update and restore the values you extracted that you dont need updated.
    10. POST rest/api/content/${varResultsPageId}/child/attachment For the Files upload tab, File Path=${__P(test_results_path)}${__P(varResultsReportFile)}, Parameter Name=file, MIME Type=text/csv
    11. logout