AEM Page adapt to experience fragment in test | Community
Skip to main content
Nov 6, 2020 10:53 PM UTC
Solved

AEM Page adapt to experience fragment in test

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SureshDhulipudi
SureshDhulipudiAccepted solution
New Participant
Nov 7, 2020 12:19 PM UTC

 

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");
}
 

4 replies

BrianKasingli
New Participant
Nov 8, 2020 8:34 PM UTC

@17388356,

Can you please share the error logs?

Translate
Nov 9, 2020 4:24 PM UTC

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.

Translate
SureshDhulipudi
SureshDhulipudiAccepted solution
New Participant
Nov 7, 2020 12:19 PM UTC

 

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");
}
 
Translate
Nov 9, 2020 3:51 PM UTC
In Aem testing, the the resourceResolverFactory, should I do it like
Translate