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

org.jboss.arquillian.graphene.DefaultGrapheneRuntime Maven / Gradle / Ivy

The newest version!
/**
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.arquillian.graphene;

import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.core.spi.ServiceLoader;
import org.jboss.arquillian.drone.api.annotation.Default;
import org.jboss.arquillian.graphene.context.GrapheneContext;
import org.jboss.arquillian.graphene.enricher.PageFragmentEnricher;
import org.jboss.arquillian.graphene.guard.RequestGuardFactory;
import org.jboss.arquillian.graphene.javascript.JSInterfaceFactory;
import org.jboss.arquillian.graphene.location.LocationEnricher;
import org.jboss.arquillian.graphene.page.document.Document;
import org.jboss.arquillian.graphene.proxy.GrapheneProxy;
import org.jboss.arquillian.graphene.proxy.GrapheneProxyInstance;
import org.jboss.arquillian.graphene.request.RequestGuard;
import org.jboss.arquillian.graphene.request.RequestType;
import org.jboss.arquillian.graphene.wait.WebDriverWait;
import org.jboss.arquillian.graphene.wait.WebDriverWaitImpl;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class DefaultGrapheneRuntime extends GrapheneRuntime {

    @Inject
    private Instance serviceLoader;

    /**
     * Returns the guarded object checking whether the HTTP request is done during
     * each method invocation. If the request is not found,
     * the {@link org.jboss.arquillian.graphene.request.RequestGuardException} is thrown.
     *
     * @param  type of the given target
     * @param target object to be guarded
     * @return the guarded object
     */
    public  T guardHttp(T target) {
        return getRequestGuardFactoryFor(target).guard(target, RequestType.HTTP, true);
    }

    /**
     * Returns the guarded object checking that no request is done during
     * each method invocation. If any request is found,
     * the {@link org.jboss.arquillian.graphene.request.RequestGuardException} is thrown.
     *
     * @param  type of the given target
     * @param target object to be guarded
     * @return the guarded object
     */
    public  T guardNoRequest(T target) {
        return getRequestGuardFactoryFor(target).guard(target, RequestType.NONE, true);
    }

    /**
     * Returns the guarded object checking whether the Ajax (XHR) request is done during
     * each method invocation. If the request is not found,
     * the {@link org.jboss.arquillian.graphene.request.RequestGuardException} is thrown.
     *
     * @param  type of the given target
     * @param target object to be guarded
     * @return the guarded object
     */
    public  T guardAjax(T target) {
        return getRequestGuardFactoryFor(target).guard(target, RequestType.XHR, true);
    }

    public  T waitForHttp(T target) {
        return getRequestGuardFactoryFor(target).guard(target, RequestType.HTTP, false);
    }

    public WebDriverWait waitAjax() {
        return waitAjax(context().getWebDriver());
    }

    public WebDriverWait waitAjax(WebDriver driver) {
        return new WebDriverWaitImpl(null, driver, ((GrapheneProxyInstance) driver).getGrapheneContext().getConfiguration().getWaitAjaxInterval());
    }

    public WebDriverWait waitGui() {
        return waitGui(context().getWebDriver());
    }

    public WebDriverWait waitGui(WebDriver driver) {
        return new WebDriverWaitImpl(null, driver, ((GrapheneProxyInstance) driver).getGrapheneContext().getConfiguration().getWaitGuiInterval());
    }

    public WebDriverWait waitModel() {
        return waitModel(context().getWebDriver());
    }

    public WebDriverWait waitModel(WebDriver driver) {
        return new WebDriverWaitImpl(null, driver, ((GrapheneProxyInstance) driver).getGrapheneContext().getConfiguration().getWaitModelInterval());
    }

    public  T createPageFragment(Class clazz, WebElement root) {
        return PageFragmentEnricher.createPageFragment(clazz, root);
    }

    public  T goTo(Class clazz) {
        return goTo(clazz, Default.class);
    }

    public  T goTo(Class pageObject, Class browserQualifier) {
        LocationEnricher locationEnricher = serviceLoader.get().onlyOne(LocationEnricher.class);
        return locationEnricher.goTo(pageObject, browserQualifier);
    }

    @Override
    public void doubleClick(WebElement element) {
        new Actions(context().getWebDriver()).doubleClick(element).perform();
    }

    @Override
    public void click(WebElement element) {
        new Actions(context().getWebDriver()).click(element).perform();
    }

    @Override
    protected void writeIntoElement(WebElement element, String text) {
        new Actions(context().getWebDriver()).moveToElement(element).click().sendKeys(text).perform();
    }

    private RequestGuardFactory getRequestGuardFactoryFor(Object target) {
        GrapheneContext context;
        if (GrapheneProxy.isProxyInstance(target)) {
            context = ((GrapheneProxyInstance) target).getGrapheneContext();
        } else {
            context = context();
        }
        return new RequestGuardFactory(
                JSInterfaceFactory.create(context, RequestGuard.class),
                JSInterfaceFactory.create(context, Document.class),
                context);
    }

    private GrapheneContext context() {
        GrapheneContext context = GrapheneContext.lastContext();
        if (context == null) {
            throw new IllegalStateException("The last used Graphene context is not available.");
        }
        return context;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy