All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tourniquet.pageobjects.Page Maven / Gradle / Ivy

/*
 * Copyright 2015-2016 DevCon5 GmbH, [email protected]
 *
 * 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 io.tourniquet.pageobjects;


import static io.tourniquet.tx.TransactionHelper.getClassTxName;

import java.util.Optional;

import com.google.common.base.Predicate;
import io.tourniquet.tx.TransactionSupport;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Interface to declare a page of an application
 */
public interface Page extends ElementGroup {

    /**
     * Default implementation navigates to the url of the page specified by locator annotation. If the page as another
     * mechanism of navigating to it, this method must be overriden.
     */
    default void loadPage() {
        SeleniumContext.currentDriver().map(driver -> {
            Optional.ofNullable(this.getClass().getAnnotation(Locator.class))
                    .flatMap(l -> l.by().locate(l.value()))
                    .ifPresent(WebElement::click);
            new WebDriverWait(driver, 150, 50).until((Predicate) d ->
                    "complete".equals(((JavascriptExecutor) d).executeScript("return document.readyState")));
            return Void.TYPE;
        }).orElseThrow(() -> new IllegalStateException("Context not initialized"));
    }

    /**
     * Navigates to a specific page of the page object model.
     *
     * @param pageType
     *         the type of the page
     * @param 
     *         the class declaring the page
     *
     * @return an instance of the page
     */
    static  T navigateTo(Class pageType) {

        final T page = PageLoader.loadPage(pageType);
        final Optional tx = Optional.ofNullable(TransactionSupport.class.isAssignableFrom(pageType)
                                                               ? (TransactionSupport) page
                                                               : null);
        tx.ifPresent(ts -> getClassTxName(pageType).ifPresent(ts::txBegin));
        try {
            page.loadPage();
        } finally {
            tx.ifPresent(ts -> getClassTxName(pageType).ifPresent(ts::txEnd));
        }

        page.locateElements();
        return page;

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy