[英]Super class that can override @FindBy
I am porting over an existing project and I have a super class called DetailPage
that is the only thing I can change/edit. 我正在移植一个现有的项目,我有一个名为DetailPage
的超类,这是我唯一可以更改/编辑的东西。 It's a selenium testing project and in previous versions I had hundreds of individual pages that extended DetailPage
and had the following lines: 这是一个硒测试项目,在之前的版本中,我有数百个单独的页面扩展了DetailPage
并有以下几行:
@FindBy(id="someID")
private WebElement someIDBox;
to search a page for a given id and assign that element to a variable. 在页面中搜索给定的id并将该元素分配给变量。 I now have a similar website that is nonW3C
so instead of finding by id
I now need to find by name
ie: 我现在有一个非nonW3C
的类似网站,所以不是通过id
找到我现在需要通过name
找到,即:
@FindBy(name="someID")
private WebElement someIDBox;
is what I now want. 是我现在想要的。
So my question is how can I (if possible) make a super class function in DetailPage that would notice I say id
and override with name
? 所以我的问题是我怎么能(如果可能的话)在DetailPage中创建一个超类函数,它会注意到我说id
并覆盖name
?
I DO NOT want a function that just replaces the text id
with name
in the individual pages as I need to preserve those. 我不想要一个只用单个页面中的name
替换文本id
的函数,因为我需要保留它们。
This does not meet the criteria that I asked but thanks to @guy I was introduced to ByIdOrName which helps with my problem. 这不符合我要求的标准但是感谢@guy我被介绍给ByIdOrName,这有助于解决我的问题。 So I made a little script that went through all my files in the workspace and replaced 所以我制作了一个小脚本,遍历工作区中的所有文件并替换
@FindBy(id="someID")
private WebElement someIDBox;
with 同
@FindBy(how = How.ID_OR_NAME, using="someID")
private WebElement someIDBox;
while this made it so I had to alter all test pages(which is what I wanted to avoid) it does not alter how other tests for other websites work which is key! 虽然这样做了所以我不得不改变所有测试页面(这是我想要避免的)它不会改变其他网站的其他测试工作的关键!
The author of this article extended the 'FindBy` annotation to support his needs. 本文作者扩展了'FindBy`注释以支持他的需求。 You can use this to override the 'FindBy' and make your on implementation. 您可以使用它来覆盖'FindBy'并实现您的实现。
Edited code sample: 编辑过的代码示例:
private static class CustomFindByAnnotations extends Annotations {
protected By buildByFromLongFindBy(FindBy findBy) {
How how = findBy.how();
String using = findBy.using();
switch (how) {
case CLASS_NAME:
return By.className(using);
case ID:
return By.id(using); // By.name(using); in your case
case ID_OR_NAME:
return new ByIdOrName(using);
case LINK_TEXT:
return By.linkText(using);
case NAME:
return By.name(using);
case PARTIAL_LINK_TEXT:
return By.partialLinkText(using);
case TAG_NAME:
return By.tagName(using);
case XPATH:
return By.xpath(using);
default:
throw new IllegalArgumentException("Cannot determine how to locate element " + field);
}
}
}
Please note I didn't try it myself. 请注意我自己没有尝试过。 Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.