我正在使用Java 8、testng、maven和selenium进行UI自动化框架工作,并尝试使用aspectj在调用对象中的任何方法时切换iframe(进入和退出)之前和之后执行方面。当我运行任何单独的测试类时,方面从未被调用。这是我的包结构。我尝试使用编译时和加载时编织,但没有任何帮助。
项目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
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中的自定义注解类
import com.stackoverflow.shop
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HandleFrame {
}
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
}
}
源代码/测试/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();
}
}
我使用RunConfiguration/Runastestng测试在Eclipse上运行NavigationTests类。
测试运行良好,但方面从未被调用。不确定我在这里犯了什么错误,或者当我们运行一个测试类时,方面没有运行。任何帮助都非常感谢。
1条答案
按热度按时间t2a7ltrp1#
您的问题似乎更多地与Maven基础知识有关,而不是与AspectJ有关。
将AspectJ Maven插件配置放入
pluginManagement/plugins
部分而不实际激活plugins
部分中的插件,对您没有帮助。因此,请确保实际使用已配置的插件。