org.xframium.page.keyWord.step.spi.KWSGetCookie.java Source code

Java tutorial

Introduction

Here is the source code for org.xframium.page.keyWord.step.spi.KWSGetCookie.java

Source

/*******************************************************************************
 * xFramium
 *
 * Copyright 2016 by Moreland Labs, Ltd. (http://www.morelandlabs.com)
 *
 * Some open source application is free software: you can redistribute 
 * it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation, either 
 * version 3 of the License, or (at your option) any later version.
 *  
 * Some open source application 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 General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with xFramium.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
 *******************************************************************************/
package org.xframium.page.keyWord.step.spi;

import java.util.Map;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.xframium.page.Page;
import org.xframium.page.data.PageData;
import org.xframium.page.keyWord.step.AbstractKeyWordStep;
import org.xframium.spi.driver.NativeDriverProvider;

// TODO: Auto-generated Javadoc
/**
 * The Class KWSGetCookie.
 */
public class KWSGetCookie extends AbstractKeyWordStep {

    /* (non-Javadoc)
     * @see com.perfectoMobile.page.keyWord.step.AbstractKeyWordStep#_executeStep(com.perfectoMobile.page.Page, org.openqa.selenium.WebDriver, java.util.Map, java.util.Map)
     */
    @Override
    public boolean _executeStep(Page pageObject, WebDriver webDriver, Map<String, Object> contextMap,
            Map<String, PageData> dataMap, Map<String, Page> pageMap) {
        if (pageObject == null) {
            throw new IllegalStateException("Page Object was not defined");
        }

        Object name = null;

        if (getParameterList().size() == 1) {
            name = getParameterValue(getParameterList().get(0), contextMap, dataMap);
            if (!(name instanceof String))
                throw new IllegalStateException("Cookie name must be of type String");
        } else {
            throw new IllegalStateException("Get cookie requires one string properties (name)");
        }
        Cookie cookie = null;
        if (webDriver instanceof RemoteWebDriver)
            cookie = ((RemoteWebDriver) webDriver).manage().getCookieNamed((String) name);
        else if (webDriver instanceof NativeDriverProvider
                && ((NativeDriverProvider) webDriver).getNativeDriver() instanceof RemoteWebDriver) {
            cookie = ((RemoteWebDriver) ((NativeDriverProvider) webDriver).getNativeDriver()).manage()
                    .getCookieNamed((String) name);
        }

        Object result = ((cookie != null) ? cookie.getValue() : null);

        if (log.isDebugEnabled())
            log.debug("Cookie [" + result + "]");

        if ((result instanceof String) && (!validateData(result + ""))) {
            throw new IllegalStateException("Get cookie Expected a format of [" + getValidationType() + "("
                    + getValidation() + ") for [" + result + "]");
        }

        if (getContext() != null) {
            if (log.isDebugEnabled())
                log.debug("Setting Context Data to [" + result + "] for [" + getContext() + "]");
            contextMap.put(getContext(), result);
        }

        return true;
    }

    /* (non-Javadoc)
     * @see com.perfectoMobile.page.keyWord.step.AbstractKeyWordStep#isRecordable()
     */
    public boolean isRecordable() {
        return false;
    }

}