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

com.xceptance.xlt.nocoding.util.context.LightWeightContext 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.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.LightWeightPageImpl;
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 LightWeight mode of the execution. Therefore, it extends
 * Context<LightWeightPage>
 *
 * @author ckeiner
 */
public class LightWeightContext extends Context
{
    /**
     * The SgmlPage
     */
    protected SgmlPage sgmlPage;

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

    /**
     * Creates a new {@link LightWeightContext}, 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 LightWeightContext(final XltProperties xltProperties, final DataStorage dataStorage)
    {
        super(xltProperties, dataStorage);
    }

    /**
     * Creates a new {@link LightWeightContext} out of the old {@link LightWeightContext}
     *
     * @param context
     *            The Context to copy the values from
     * @see Context#Context(Context)
     */
    public LightWeightContext(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} and sets {@link #sgmlPage} to
     * null. 
* If {@link WebRequest#isXHR()} is false, it loads the {@link LightWeightPage} and sets the * {@link WebResponse}.
* If WebRequest.isXHR() is true, it only loads the WebResponse. * * @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); // If the webRequest is not a Xhr if (!webRequest.isXHR()) { // Load and set page setPage(getWebClient().getLightWeightPage(webRequest)); // Set webResponse setWebResponse(getPage().getWebResponse()); } // If the webRequest is a Xhr else { // Load and set the WebResponse 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 { final String name = getWebClient().getTimerName(); // If getPage is not null, append getPage if (getPage() != null) { ((SessionImpl) Session.getCurrent()).getRequestHistory().add(getPage()); } // If getPage is null, which happens for all requests, that only have XHR or no requests as predecessor else { // Check if the webRequest was xhr if (getWebResponse().getWebRequest().isXHR()) { // Append an empty page to the result browser ((SessionImpl) Session.getCurrent()).getRequestHistory().add(name); } // If it wasn't an XHR, build a LightWeightPage from the WebResponse else { ((SessionImpl) Session.getCurrent()).getRequestHistory() .add(new LightWeightPageImpl(getWebResponse(), name, getWebClient())); } } } /** * Creates a new {@link LightWeightContext} out of the current one. * * @return A new LightWeightContext via {@link #LightWeightContext(Context)} */ @Override public Context buildNewContext() { return new LightWeightContext(this); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy