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

com.xceptance.xlt.nocoding.util.context.RequestContext Maven / Gradle / Ivy

Go to download

A library based on XLT to run Web test cases that are written in either YAML or CSV format.

The newest version!
/*
 * Copyright (c) 2013-2023 Xceptance Software Technologies GmbH
 *
 * 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.xceptance.xlt.nocoding.util.context;

import java.io.IOException;

import org.apache.commons.lang3.NotImplementedException;
import org.htmlunit.FailingHttpStatusCodeException;
import org.htmlunit.Page;
import org.htmlunit.SgmlPage;
import org.htmlunit.WebRequest;
import org.htmlunit.WebResponse;
import org.htmlunit.xml.XmlPage;

import com.xceptance.xlt.api.engine.Session;
import com.xceptance.xlt.api.htmlunit.LightWeightPage;
import com.xceptance.xlt.api.util.XltLogger;
import com.xceptance.xlt.api.util.XltProperties;
import com.xceptance.xlt.engine.SessionImpl;
import com.xceptance.xlt.engine.XltWebClient;
import com.xceptance.xlt.nocoding.util.resolver.VariableResolver;
import com.xceptance.xlt.nocoding.util.storage.DataStorage;

/**
 * The {@link Context} used in the Request mode of the execution. Since it doesn't do anything special, it's generic
 * type is {@link Void}.
 *
 * @author ckeiner
 */
public class RequestContext extends Context
{

    /**
     * The SgmlPage
     */
    protected SgmlPage sgmlPage;

    /**
     * Calls {@link Context#Context(XltProperties, DataStorage)}, with a new {@link DataStorage}.
     *
     * @param xltProperties
     *            The properties to use - normally {@link XltProperties#getInstance()}
     * @see Context#Context(XltProperties)
     */
    public RequestContext(final XltProperties xltProperties)
    {
        super(xltProperties);
    }

    /**
     * Creates a new {@link RequestContext}, with the provided {@link DataStorage}, creates a new {@link XltWebClient}
     * and {@link VariableResolver} and finally calls {@link Context#initialize()}.
     *
     * @param xltProperties
     *            The properties to use - normally {@link XltProperties#getInstance()}
     * @param dataStorage
     *            The {@link DataStorage} you want to use
     * @see Context#Context(XltProperties, DataStorage)
     */
    public RequestContext(final XltProperties xltProperties, final DataStorage dataStorage)
    {
        super(xltProperties, dataStorage);
    }

    /**
     * Creates a new {@link RequestContext} out of the old {@link RequestContext}
     *
     * @param context
     *            The Context to copy the values from
     * @see Context#Context(Context)
     */
    public RequestContext(final Context context)
    {
        super(context);
    }

    /**
     * Gets the {@link SgmlPage} if it isn't null. Else, it builds the SgmlPage from the
     * {@link WebResponse}.
     *
     * @return
     */
    public SgmlPage getSgmlPage()
    {
        // If the sgmlPage is null and therefore wasn't loaded already
        if (sgmlPage == null)
        {
            // Generate a new sgmlPage
            XltLogger.runTimeLogger.debug("Generating new SgmlPage...");
            Page page;
            try
            {
                // Load the WebResponse into the window of the web client
                page = getWebClient().loadWebResponseInto(getWebResponse(), getWebClient().getCurrentWindow());
                // If the built page is an instance of SgmlPage
                if (page instanceof SgmlPage)
                {
                    if (page instanceof XmlPage && ((XmlPage) page).getXmlDocument() == null)
                    {
                        throw new IllegalStateException("Faulty WebResponse, the page doesn't have child nodes.");
                    }
                    // Set the sgmlPage
                    setSgmlPage((SgmlPage) page);
                    XltLogger.runTimeLogger.debug("SgmlPage built");
                }
            }
            catch (FailingHttpStatusCodeException | IOException e)
            {
                throw new IllegalStateException("Cannot convert WebResponse to SgmlPage.");
            }
        }
        // Return the sgmlPage
        return sgmlPage;
    }

    public void setSgmlPage(final SgmlPage sgmlPage)
    {
        this.sgmlPage = sgmlPage;
    }

    /**
     * Loads the {@link WebResponse} corresponding to the {@link WebRequest}.
* * @param webRequest * The webRequest that should be send * @throws IOException * @throws FailingHttpStatusCodeException */ @Override public void loadWebResponse(final WebRequest webRequest) throws FailingHttpStatusCodeException, IOException { // Reset the sgmlPage setSgmlPage(null); setWebResponse(getWebClient().loadWebResponse(webRequest)); } /** * Appends the {@link #getPage()} to the result browser, if it is not null.
* Otherwise, it creates a new {@link LightWeightPage}. * * @throws IOException * @throws FailingHttpStatusCodeException */ @Override public void appendToResultBrowser() throws FailingHttpStatusCodeException, IOException { ((SessionImpl) Session.getCurrent()).getRequestHistory().add(getWebClient().getTimerName()); } /** * Creates a new {@link RequestContext} out of the current one. * * @return A new RequestContext via {@link #RequestContext(Context)} */ @Override public Context buildNewContext() { return new RequestContext(this); } /** * Gets the page. * * @return The page corresponding to the {@link #webResponse} */ @Override public Void getPage() { return null; }; /** * Sets the page. * * @param page * The page corresponding to the {@link #webResponse} */ @Override public void setPage(final Void page) { throw new NotImplementedException("This method cannot be called."); }; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy