简体   繁体   中英

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. It's a selenium testing project and in previous versions I had hundreds of individual pages that extended DetailPage and had the following lines:

@FindBy(id="someID")
private WebElement someIDBox;

to search a page for a given id and assign that element to a variable. I now have a similar website that is nonW3C so instead of finding by id I now need to find by name ie:

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

I DO NOT want a function that just replaces the text id with name in the individual pages as I need to preserve those.

This does not meet the criteria that I asked but thanks to @guy I was introduced to ByIdOrName which helps with my problem. 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. You can use this to override the 'FindBy' and make your on implementation.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM