Java 如何将WebElement存储在json或属性中,以便轻松更改它
我在使用POM的测试框架中使用了这种类型的代码 我想将此Xpath存储在属性文件中,并只为其分配密钥 例如:Java 如何将WebElement存储在json或属性中,以便轻松更改它,java,selenium,automated-tests,Java,Selenium,Automated Tests,我在使用POM的测试框架中使用了这种类型的代码 我想将此Xpath存储在属性文件中,并只为其分配密钥 例如: 有没有办法存储WebElement或创建WebElement存储库?@FindBy接受final字符串 @FindBy(xpath=config.getsubmit()) WebElement submit; 您不能将值传递给运行时确定的注释,只允许像final字段或类型这样的常量。有关详细信息,请参阅。这是因为注释是在编译时计算的。它们的值可能保留在类文件中 尽管你可以: 但是,这需
有没有办法存储WebElement或创建WebElement存储库?
@FindBy
接受final
字符串
@FindBy(xpath=config.getsubmit())
WebElement submit;
您不能将值传递给运行时确定的注释,只允许像final字段或类型这样的常量。有关详细信息,请参阅。这是因为注释是在编译时计算的。它们的值可能保留在类文件中 尽管你可以:
但是,这需要对WebDriver实例进行引用。使用PageObjects和PageFactory的普通风格无法做到这一点 您需要进行一些定制,然后才能完成此操作 要做到这一点,您基本上需要以下几点
的定制实现,这样您就可以使用提供的org.openqa.selenium.support.pagefactory.ElementLocator
实例对WebDriver
和findElement
进行调用。默认实现只是从注释中查询findElements
的定制实现,以便它可以生成定制的org.openqa.selenium.support.pagefactory.ElementLocatorFactory
实例,而不是默认实例ElementLocator
- 自定义注释解析器,您可以通过扩展
org.openqa.selenium.support.pagefactory.AbstractAnnotations来解析添加到
对象的自定义注释WebElement
- 最后但并非最不重要的一点是,一个自定义注释捕获一些元数据,这些元数据可用于从给定web元素的JSON文件中读取定位器
- 定义自定义注释,使您能够从文件中唯一地标识定位器
- 定义一个自定义注释解析器,该解析器将读取自定义注释的属性并通过对象构造一个
- 定义自定义
,以便它使用自定义注释解析器查找元素ElementLocator
- 定义自定义
,使其生成自定义ElementLocator工厂
ElementLocator的实例
WebElement element = driver.findElement(By.xpath(config.getsubmit()));
自定义注释如下所示
WebElement element = driver.findElement(By.xpath(config.getsubmit()));
import java.lang.annotation.ElementType;
导入java.lang.annotation.Retention;
导入java.lang.annotation.RetentionPolicy;
导入java.lang.annotation.Target;
@保留(RetentionPolicy.RUNTIME)
@目标(ElementType.FIELD)
public@interface-SearchWith{
字符串inPage()默认为“”;
字符串定位器文件()默认为“”;
字符串名()默认为“”;
}
自定义ElementLocator
实现
import org.openqa.selenium.By;
导入org.openqa.selenium.SearchContext;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.support.pagefactory.AbstractAnnotations;
导入org.openqa.selenium.support.pagefactory.ElementLocator;
导入java.util.List;
公共类FileBasedElementLocator实现ElementLocator{
私有最终搜索上下文搜索上下文;
私有最终布尔shouldCache;
私人终审法院;
私有WebElement缓存删除;
私有列表cachedElementList;
公共FileBasedElementLocator(SearchContext SearchContext,AbstractAnnotations){
this.searchContext=searchContext;
this.shouldCache=annotations.isLookupCached();
this.by=annotations.buildBy();
}
@凌驾
公共WebElement findElement(){
if(cachedElement!=null&&shouldCache){
返回缓存删除;
}
WebElement=searchContext.findElement(by);
if(shouldCache){
cachedElement=元素;
}
返回元素;
}
@凌驾
公共列表findElements(){
if(cachedElementList!=null&&shouldCache){
返回cachedElementList;
}
List elements=searchContext.findElements(按);
if(shouldCache){
cachedElementList=元素;
}
返回元素;
}
}
自定义元素定位器工厂如下所示
import org.openqa.selenium.SearchContext;
导入org.openqa.selenium.support.pagefactory.ElementLocator;
导入org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
导入java.lang.reflect.Field;
公共类FileBasedElementLocatorFactory实现ElementLocatorFactory{
私有最终搜索上下文搜索上下文;
公共FileBasedElementLocatorFactory(SearchContext SearchContext){
this.searchContext=searchContext;
}
@凌驾
公共元素定位器createLocator(字段){
返回新的FileBasedElementLocator(searchContext,新的CustomAnnotations(字段));
}
}
AbstractAnnotations的自定义实现
import com.google.common.base.premissions;
导入com.google.gson.JsonArray;
导入com.google.gson.JsonElement;
导入com.google.gson.JsonObject;
导入com.google.gson.JsonParser;
导入org.openqa.selenium.By;
导入org.openqa.selenium.support.CacheLookup;
导入org.openqa.selenium.support.pagefactory.AbstractAnnotations;
导入organized.chaos.annotations.SearchWith;
导入java.io.File;
导入java.io.FileNotFoundEx
WebElement element = driver.findElement(By.xpath(config.getsubmit()));
[
{
"pageName": "HomePage",
"name": "abTesting",
"locateUsing": "xpath",
"locator": "//a[contains(@href,'abtest')]"
},
{
"pageName": "HomePage",
"name": "checkBox",
"locateUsing": "xpath",
"locator": "//a[contains(@href,'checkboxes')]"
},
{
"pageName": "CheckboxPage",
"name": "checkBox1",
"locateUsing": "xpath",
"locator": "//input[@type='checkbox'][1]"
},
{
"pageName": "CheckboxPage",
"name": "checkBox2",
"locateUsing": "xpath",
"locator": "//input[@type='checkbox'][2]"
}
]
Public class WebElementRepo
{
static WebElement01 = null;
// then create getter setter for all these webelements
public static void setWebElement01(WebElement WE)
{
WebElement01 = WE;
}
public static WebElement getWebElement01(WebElement WE)
{
return WebElement01;
}
}