Java AspectJ—捕获用@FindBy注释的所有WebElement的切入点
我的测试框架使用selenium的PageFactory和Lambok。我想编写一个方面来捕获测试流在运行时遇到的所有web元素 典型的页面如下所示:Java AspectJ—捕获用@FindBy注释的所有WebElement的切入点,java,selenium,aop,aspectj,page-factory,Java,Selenium,Aop,Aspectj,Page Factory,我的测试框架使用selenium的PageFactory和Lambok。我想编写一个方面来捕获测试流在运行时遇到的所有web元素 典型的页面如下所示: @Slf4j public class MyCustomPage { @Inject private IWebDriverSet driverSet; @Getter @FindBy(id = PAGE_ROOT) private WebElement root; @FindAll({
@Slf4j
public class MyCustomPage {
@Inject
private IWebDriverSet driverSet;
@Getter
@FindBy(id = PAGE_ROOT)
private WebElement root;
@FindAll({
@FindBy(css = FOOT_BAR),
@FindBy(css = FOOT_BAR_B)
})
private WebElement navBar;
}
@FindBy确定测试所处理的webelement。这样的页面有50页
当使用PageFactory实例化页面时,将实例化webElement字段(使用与@FindBy中的值对应的webElement实例进行分配)
我希望在实例化这些webElements后立即捕获它们,并用@FindBy/@FindAll进行注释。
我不想为每个页面类编写单独的切入点。
如何做到这一点?因为通过反射指定的WebElement的值不能用set()切入点指示器截取。但是您可以跟踪对
java.lang.reflect.Field.set的所有调用
@After("call(* java.lang.reflect.Field.set(..)) && args(obj, value) && target(target)")
public void webelementInit(JoinPoint jp, Object obj, Object value, Field target) {
//obj - instance of a class (page object) that declares current field
//value - new field value (instantiated WebElement)
//field - current field
//you can filter calls to the fields you need by matching target.getDeclaringClass().getCanonicalName() with page object's package
//for example:
//if(target.getDeclaringClass().getCanonicalName().contains("com.example.pageobjects")) {
//do stuff
//}
}
在这种情况下,您需要在pom.xml的dependencies部分中定义rt.jar
<dependencies>
<dependency>
<groupId>java</groupId>
<artifactId>jre-runtime</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
...
</dependencies>
JAVA
jre运行时
1.8
系统
${java.home}/lib/rt.jar
...
在aspectj maven插件的weaveDependencies部分
<weaveDependencies>
<weaveDependency>
<groupId>java</groupId>
<artifactId>jre-runtime</artifactId>
</weaveDependency>
...
</weaveDependencies>
JAVA
jre运行时
...
由于通过反射指定的WebElement的值,所以不能使用set()切入点指示器截取该值。但是您可以跟踪对java.lang.reflect.Field.set的所有调用
@After("call(* java.lang.reflect.Field.set(..)) && args(obj, value) && target(target)")
public void webelementInit(JoinPoint jp, Object obj, Object value, Field target) {
//obj - instance of a class (page object) that declares current field
//value - new field value (instantiated WebElement)
//field - current field
//you can filter calls to the fields you need by matching target.getDeclaringClass().getCanonicalName() with page object's package
//for example:
//if(target.getDeclaringClass().getCanonicalName().contains("com.example.pageobjects")) {
//do stuff
//}
}
在这种情况下,您需要在pom.xml的dependencies部分中定义rt.jar
<dependencies>
<dependency>
<groupId>java</groupId>
<artifactId>jre-runtime</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
...
</dependencies>
JAVA
jre运行时
1.8
系统
${java.home}/lib/rt.jar
...
在aspectj maven插件的weaveDependencies部分
<weaveDependencies>
<weaveDependency>
<groupId>java</groupId>
<artifactId>jre-runtime</artifactId>
</weaveDependency>
...
</weaveDependencies>
JAVA
jre运行时
...
IMO没有selenium方法,只有好的旧java反射。即使这样,您也必须传递要分析的类列表。或者重写FindBy,让它有一些日志记录。你说的“捕获”是什么意思?只是记录?您可以扩展DefaultFieldDecorator并向decoration()方法添加代码。然后将新的decorator类传递给PageFactory静态initElements(FieldDecorator decorator,java.lang.Object page)方法以初始化PageObject。这段代码将针对每个需要代理的带有findby注释的webelement运行。IMO没有硒的方法,只有好的旧java反射。即使这样,您也必须传递要分析的类列表。或者重写FindBy,让它有一些日志记录。你说的“捕获”是什么意思?只是记录?您可以扩展DefaultFieldDecorator并向decoration()方法添加代码。然后将新的decorator类传递给PageFactory静态initElements(FieldDecorator decorator,java.lang.Object page)方法以初始化PageObject。此代码将针对每个需要代理的带有findby注释的webelement运行。