1

I'm using Java 8, testng, maven and selenium for UI automation frame work and trying to use aspectj to execute aspects before and after for switching iframes(entry and exit) when any method in an object is called. Aspect never gets called when i run any induvidual test class. Here is my package structure. I tried using both compile time and load time weaving but nothing help.

Project A is the parent The module Project B is inside Project A

Project A pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.projectA</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
        <name>projectA</name>
        <modules>
        <module>projectB</module>
        </modules>
        <properties>
            <!-- Maven Plugins -->
            <maven-compiler-version>3.6.0</maven-compiler-version>
            <maven-surefire-version>2.20.1</maven-surefire-version>
            <!-- Dependencies -->
            <selenium-version>3.14.0</selenium-version>
            <testng-version>7.4.0</testng-version>
            <log4j2-version>2.17.0</log4j2-version> 
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        </properties>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>${maven-compiler-version}</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <useIncrementalCompilation>false</useIncrementalCompilation>
                        </configuration>
                    </plugin>
                    <plugin>
                         <groupId>org.codehaus.mojo</groupId>
                         <artifactId>aspectj-maven-plugin</artifactId>
                         <version>1.8</version>
                         <configuration>
                             <showWeaveInfo>true</showWeaveInfo>
                             <source>1.8</source>
                             <target>1.8</target>
                             <Xlint>ignore</Xlint>
                             <complianceLevel>1.8</complianceLevel>
                             <encoding>UTF-8</encoding>
                             <verbose>true</verbose>
                         </configuration>
                        <executions>
                            <execution>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                            </execution>
                        </executions>   
                        <dependencies>
                            <dependency>
                                <groupId>org.aspectj</groupId>
                                <artifactId>aspectjtools</artifactId>
                                <version>1.8.13</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${maven-surefire-version}</version>
                        <systemPropertyVariables>
                                <user1>${username}</user1>
                                <!-- Other system variables -->
                            </systemPropertyVariables>
                        <configuration>
                            <argLine>-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.8.13/aspectjweaver-1.8.13.jar</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                    <version>${selenium-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-firefox-driver</artifactId>
                    <version>${selenium-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-htmlunit-driver</artifactId>
                    <version>${htmlunit-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-support</artifactId>
                    <version>${selenium-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                    <version>${testng-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                    <version>${log4j2-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-core</artifactId>
                    <version>${log4j2-version}</version>
                </dependency>
                  <dependency>
                     <groupId>org.aspectj</groupId>
                     <artifactId>aspectjrt</artifactId>
                     <version>1.8.13</version>
                     <scope>runtime</scope>
                 </dependency>
                 <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.13</version>
                <scope>test</scope>
            </dependency>
            </dependencies>
    </project>

Project B pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>projectB</artifactId>
    <name>projectB</name>
    <parent>
        <groupId>com.projectA</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-htmlunit-driver</artifactId>
        </dependency>
        
        <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Packaging Structure for Project B.
all the code is in Project B
src/main/java(Holds all the page object classes, interface, aspects and custom annotation classes)
src/test/java(Holds only test classes and has META-INF folder that holds aop.xml)

Interface in src/main/java

 public interface NavBarInterface{
      public void navigateToHelp();
      public void navigateToMenu();
    }

PageObject Class in src/main/java NavigationBar.java

import com.stackoverflow.shop

Class NavigationBar{

   NavigationBar(){}
   @Override
   @HandleFrame
   public void navigateToHelp() {
    // Navigates to Help section
   }

   @Override
   @HandleFrame
   public void navigateToMenu() {
    // Navigates to Help Menu section
   }
}

Custom Annotation class in src/main/java

import com.stackoverflow.shop
        
        @Target(ElementType.METHOD)
        @Retention(RetentionPolicy.RUNTIME)
        public @interface  HandleFrame {
        }

Aspect Class in src/main/java

import com.stackoverflow.shop
    
    @Aspect
    Class FrameHandlingAspect{
    
       @Before("@annotation(com.stackoverflow.shop.NavigationBar)")
        public void enterFrame() {
            logger.info("Switched to IFrame");
            // switches to iframe
        }
    
       @After("@annotation(com.stackoverflow.shop.NavigationBar)")
        public void enterFrame() {
            logger.info("exited from IFrame");
            // exit from iframe
        }
    }

apo.xml file in src/test/java/META-INF

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
 <aspects>
  <aspect name="com.stackoverflow.shop.FrameHandlingAspect"/>
 </aspects> 
 <weaver options="-verbose -debug -showWeaveInfo">
  <include within="com.stackoverflow..*" />
 </weaver>
</aspectj>

TestClass in src/test/java

Class NavigationTests {

@BeforeMethod(alwaysRun = true)
public void refreshAndSwitchTab(){
  // performs refresh page action and opens a new tab
}

@AfterMethod(alwaysRun = true)
public void closeTab(){
  // closes secondary tab
}

@Test(enabled=true)      
public void testNavigation() {
NavBarInterface nbar = new NavigationBar();
nbar.navigateToHelp(); //Aspects are not called before and after
nbar.navigateToMenu();
}
    
}

I'm running the NavigationTests class on Eclipse using Run Configuration/Run as testng test.I also tried running the test as providing vm argument -Dtest=NavigationTests

The tests are running fine but the aspects are never called. Not sure what mistake i'm doing here or does aspect doesn't run when we run a single test class. Any help is much appreciated.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
gamelover
  • 15
  • 4
  • Working with Aspects without Spring is a little different. There are some examples [here](https://www.baeldung.com/aspectj) – timofeevle Jan 16 '23 at 23:39
  • @timeofeevle I looked into the link you provided and i think i did everything except the .aj file. I wasn't able to create a .aj file in my project. Do i still need the .aj file when i'm using annotations? – gamelover Jan 17 '23 at 00:30

1 Answers1

0

Your problem seems to be more about Maven basics than AspectJ at all.

Putting AspectJ Maven plugin configuration into your pluginManagement/plugins section without actually activating the plugin in the plugins section, will not help you. So please, make sure to actually use the configured plugin.

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.14.0</version>  <!-- No reason to use the outdated 1.8 -->
      <configuration>
        <!-- (...) -->
      </configuration>
    <plugin>
  <plugins>
<pluginManagement>

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <!-- No version/config necessary here, because already configured above) -->
  <plugin>
<plugins>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • The aspects started working when I run as a maven test. They don’t work when I run as a testing test. Are they suppose to work only for maven? – gamelover Jan 21 '23 at 16:28
  • What is a "testing test" supposed to be? Please express yourself more clearly. Do not just throw keywords at people who are trying to help you. Provide some context and explain your situation. It is really tedious to help you - not because you are a newbie, but because you are lazy to explain what you mean. This way you are not going to make lots of friends here. – kriegaex Jan 21 '23 at 17:17
  • I also do not understand why you mix compile-time weaving (AspectJ compiler used via AspectJ Maven plugin) with load-time weaving (_aop.xml_ + `-javaagent` JVM parameter). Decide what you want. Eating lots of medicine does not help more than taking the right one in the right dosage. It probably would help to publish an [MCVE](https://stackoverflow.com/help/mcve) on GitHub to make your problem reproducible. – kriegaex Jan 21 '23 at 17:24
  • Sorry for the late response and thanks for your help. The aspects started triggering when i run only as maven test . I used to run individual tests using eclipse testng and the aspects were not getting triggered so wanted know if they will work only when we run as maven test. I was new to aspectj so wasn't sure and i added both compile time and load time weaving. now i removed compile time weaving and currently using only load time weaving – gamelover Jan 22 '23 at 18:34
  • You did not mention your IDE (Eclipse, IntelliJ IDEA, Vs Code), so I can just generally give you a hint: Please verify that your test run configuration picks up the `-javaagent` JVM parameter for load-time weaving correctly. In IDEA, this works correctly for me, if Surefire is configured correctly and I use Maven auto-import, but I only use Spock and rarely JUnit for testing, never TestNG. But it should not make a big difference. – kriegaex Jan 25 '23 at 07:48