簡體   English   中英

在非 spring maven 項目上未調用方面

[英]Aspect not getting called on non-spring maven project

我正在使用 Java 8、testng、maven 和 selenium 進行 UI 自動化框架工作,並嘗試在調用 object 中的任何方法時使用 aspectj 在切換 iframe(進入和退出)之前和之后執行方面。 當我運行任何 induvidual 測試 class 時,Aspect 永遠不會被調用。這是我的 package 結構。 我嘗試同時使用編譯時和加載時編織但沒有任何幫助。

項目A是父模塊項目B在項目A中

項目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>

項目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)

src/main/java 中的接口

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

src/main/java NavigationBar.java 中的 PageObject Class

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
   }
}

src/main/java 中的自定義注解 class

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

src/main/java 中的方面 Class

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
        }
    }

src/test/java/META-INF 中的 apo.xml 文件

<?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>

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

我正在使用 Run Configuration/Run as testng 測試在 Eclipse 上運行 NavigationTests class。我還嘗試以提供 vm 參數 -Dtest=NavigationTests 的方式運行測試

測試運行良好,但從未調用過這些方面。 不確定我在這里犯了什么錯誤,或者當我們運行單個測試 class 時方面沒有運行。非常感謝任何幫助。

您的問題似乎更多是關於 Maven 基礎知識,而不是 AspectJ。

將 AspectJ Maven 插件配置放入您的pluginManagement/plugins部分而不實際激活plugins ,對您沒有幫助。 所以請確保實際使用配置的插件。

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM