Search code examples
aem

Adobe AEM (Adobe Experience Manager) HTL2HTML for components


Adobe AEM (Adobe Experience Manager) HTL2HTML compile and test (AEM 6.1 soon to be moving to 6.3)

I would like to automatically generate HTML from HTL Components as part of the build process (using default / and custom values ) so I can provide additional automated testing and quality assurance. i.e. HTML validation and Accessibility QA.

Is there a java call, or some other command line tool that could generate html snippets or pages for each of the components. Which could be incorporated into the build process when making a call to mvn clean install -PautoInstallPackage

I have considered generating pages using selenium but I suspect that approach is slow and error prone.

Thanks for your help

Mike


Solution

  • You can get the rendered HTML on the server side using SlingRequestProcessor.

    From @nateyolles blog post:

    Getting the HTML markup for a resource in Apache Sling.

    @SlingServlet(paths={"/bin/foo"})
    public class SlingResourceResolutionServlet extends SlingSafeMethodsServlet {
    
        /** Service to process requests through Sling */
        @Reference
        private SlingRequestProcessor requestProcessor;
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    
            /* The resource path to resolve. Use any selectors or extension. */
            String requestPath = "/content/myapp/us/en/index/jcr:content/myparsys/mycomponent_f93d.html";
    
            /* 
             * Create a fake request and fake response. The response adds a method to make the HTML accessible.
             * You need the following three files from Apache Sling:
             *
             * https://github.com/apache/sling/blob/trunk/testing/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java
             * https://github.com/apache/sling/blob/trunk/testing/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java
             * https://github.com/apache/sling/blob/trunk/testing/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java
             */
            final HttpRequest req = new HttpRequest(requestPath);
            final HttpResponse resp = new HttpResponse();
    
            /* Process request through Sling */
            requestProcessor.processRequest(req, resp, request.getResourceResolver());
            String html = resp.getContent();
        }
    

    Getting the HTML markup for a resource in AEM.

    @SlingServlet(paths={"/bin/foo"})
    public class AemResourceResolutionServlet extends SlingSafeMethodsServlet {
    
        /** Service to create HTTP Servlet requests and responses */
        @Reference
        private RequestResponseFactory requestResponseFactory;
    
        /** Service to process requests through Sling */
        @Reference
        private SlingRequestProcessor requestProcessor;
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    
            /* The resource path to resolve. Use any selectors or extension. */
            String requestPath = "/content/myapp/us/en/index/jcr:content/myparsys/mycomponent_f93d.html";
    
            /* Setup request */
            HttpServletRequest req = requestResponseFactory.createRequest("GET", requestPath);
            WCMMode.DISABLED.toRequest(req);
    
            /* Setup response */
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            HttpServletResponse resp = requestResponseFactory.createResponse(out);
    
            /* Process request through Sling */
            requestProcessor.processRequest(req, resp, request.getResourceResolver());
            String html = out.toString();
        }
    }