com.arcbees.test.ElementLocator.java Source code

Java tutorial

Introduction

Here is the source code for com.arcbees.test.ElementLocator.java

Source

/*
 * Copyright 2013 ArcBees Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.arcbees.test;

import java.lang.reflect.Field;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.pagefactory.Annotations;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.arcbees.test.annotation.FindByDebugId;
import com.arcbees.test.annotation.LongImplicitWait;

public class ElementLocator implements org.openqa.selenium.support.pagefactory.ElementLocator {
    private final WebDriver webDriver;

    private boolean shouldCache;
    private By by;
    private long timeout;
    private WebElement cachedElement;
    private List<WebElement> cachedElementList;

    public ElementLocator(WebDriver webDriver, Field field) {
        this.webDriver = webDriver;

        processAnnotations(field);
    }

    @Override
    public WebElement findElement() {
        if (cachedElement != null && shouldCache) {
            return cachedElement;
        }

        WebElement element = doFindElement();
        if (shouldCache) {
            cachedElement = element;
        }

        return element;
    }

    @Override
    public List<WebElement> findElements() {
        if (cachedElementList != null && shouldCache) {
            return cachedElementList;
        }

        List<WebElement> elements = doFindElements();
        if (shouldCache) {
            cachedElementList = elements;
        }

        return elements;
    }

    private WebElement doFindElement() {
        if (timeout > 0) {
            return new WebDriverWait(webDriver, timeout).until(ExpectedConditions.presenceOfElementLocated(by));
        } else {
            return webDriver.findElement(by);
        }
    }

    private List<WebElement> doFindElements() {
        if (timeout > 0) {
            return new WebDriverWait(webDriver, timeout)
                    .until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
        } else {
            return webDriver.findElements(by);
        }
    }

    private void processAnnotations(Field field) {
        Annotations annotations = new Annotations(field);
        processFindBy(field, annotations);
        processWaitTimeout(field);
        shouldCache = annotations.isLookupCached();
    }

    private void processWaitTimeout(Field field) {
        LongImplicitWait longImplicitWait = field.getAnnotation(LongImplicitWait.class);
        if (longImplicitWait != null) {
            timeout = longImplicitWait.value();
        }
    }

    private void processFindBy(Field field, Annotations annotations) {
        FindByDebugId findByDebugId = field.getAnnotation(FindByDebugId.class);
        if (findByDebugId != null) {
            String id = findByDebugId.value();
            by = new ByDebugId(id);
        } else {
            by = annotations.buildBy();
        }
    }
}