Expand my Community achievements bar.

SOLVED

AEM Page adapt to experience fragment in test

Avatar

Level 1

Hi, This problem, I've been bugging me around a bit. Whenever I am adapt to ExperienceFragmentVariation, it returns me a NULL. Anyone have some insight on this?

Screen Shot 2020-11-06 at 5.52.11 PM.png

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

 

Is your page resource is Experience Fragment Page?

can you try like get Experience Fragment resource page and then get the variations list of that page resource and then adapto

 

ResourceResolver resourceResolver;
resourceResolver = resourceResolverFactory.getServiceResourceResolver(<<<inputparam>>>);
Resource res=resourceResolver.getResource(<<<<<<Page Path / Resource Path>>>>);
Page page=res.adaptTo(Page.class);
ExperienceFragmentsService expService=experienceFragmentsServiceFactory.getExperienceFragmentsService(<<<resourceResolver>>>);
            /// check if the page is Experience Fragment type or content Fragment type
if(page.getContentResource().isResourceType("cq/experience-fragments/components/experiencefragment")){ 
Iterator<Page> fragmentVariation=page.listChildren(new PageFilter(),false);
while(fragmentVariation.hasNext()){
Page fragmentPage=fragmentVariation.next();
if(null!=fragmentPage){
 ExperienceFragmentVariation fragmentVariation = fragmentPage.adaptTo(ExperienceFragmentVariation.class);
      if (null!=fragmentVariation)  
              <<< LOGIC to implement>>>
      else 
           logger.warn("The page at path cannot be adapted to an experience fragment variation");
}
 

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

 

Is your page resource is Experience Fragment Page?

can you try like get Experience Fragment resource page and then get the variations list of that page resource and then adapto

 

ResourceResolver resourceResolver;
resourceResolver = resourceResolverFactory.getServiceResourceResolver(<<<inputparam>>>);
Resource res=resourceResolver.getResource(<<<<<<Page Path / Resource Path>>>>);
Page page=res.adaptTo(Page.class);
ExperienceFragmentsService expService=experienceFragmentsServiceFactory.getExperienceFragmentsService(<<<resourceResolver>>>);
            /// check if the page is Experience Fragment type or content Fragment type
if(page.getContentResource().isResourceType("cq/experience-fragments/components/experiencefragment")){ 
Iterator<Page> fragmentVariation=page.listChildren(new PageFilter(),false);
while(fragmentVariation.hasNext()){
Page fragmentPage=fragmentVariation.next();
if(null!=fragmentPage){
 ExperienceFragmentVariation fragmentVariation = fragmentPage.adaptTo(ExperienceFragmentVariation.class);
      if (null!=fragmentVariation)  
              <<< LOGIC to implement>>>
      else 
           logger.warn("The page at path cannot be adapted to an experience fragment variation");
}
 

Avatar

Level 1
In Aem testing, the the resourceResolverFactory, should I do it like

Avatar

Level 1

Hi, I am writing a Unit test, unfortunately, I don't have any logger for this, but my problem, it's that whenever I try adapting the resource Page to ExperienceFragmentVariation => null.