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

com.applitools.eyes.selenium.frames.FrameChain Maven / Gradle / Ivy

There is a newer version: 3.36
Show newest version
/*
 * Applitools software.
 */
package com.applitools.eyes.selenium.frames;

import com.applitools.eyes.EyesException;
import com.applitools.eyes.Location;
import com.applitools.eyes.Logger;
import com.applitools.eyes.RectangleSize;
import com.applitools.eyes.selenium.exceptions.NoFramesException;
import com.applitools.utils.ArgumentGuard;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Represents a path to a frame, including their location and scroll.
 */
public class FrameChain implements Iterable{
    private final Logger logger;
    private List frames;

    /**
     * Compares two frame chains.
     * @param c1 Frame chain to be compared against c2.
     * @param c2 Frame chain to be compared against c1.
     * @return True if both frame chains represent the same frame, false otherwise.
     */
    public static boolean isSameFrameChain(FrameChain c1, FrameChain c2) {
        int lc1 = c1.frames.size();
        int lc2 = c2.frames.size();

        // different chains size means different frames
        if (lc1 != lc2) {
            return false;
        }

        Iterator c1Iterator = c1.iterator();
        Iterator c2Iterator = c2.iterator();

        //noinspection ForLoopReplaceableByForEach
        for(int i = 0; i();
    }

    /**
     * Creates a frame chain which is a copy of the current frame.
     * @param logger A Logger instance.
     * @param other A frame chain from which the current frame chain will be created.
     */
    public FrameChain(Logger logger, FrameChain other) {
        ArgumentGuard.notNull(logger, "logger");
        ArgumentGuard.notNull(other, "other");
        this.logger = logger;
        logger.verbose(String.format("Frame chain copy constructor (size %d)", other.size()));
        frames = new LinkedList<>();
        for (Frame otherFrame: other.frames) {
            frames.add(new Frame(logger, otherFrame.getReference(),
                    otherFrame.getLocation(),
                    otherFrame.getSize(), otherFrame.getInnerSize(),
                    otherFrame.getOriginalLocation()));
        }
        logger.verbose("Done!");
    }

    /**
     *
     * @return The number of frames in the chain.
     */
    public int size() {
        return frames.size();
    }

    /**
     * Removes all current frames in the frame chain.
     */
    public void clear() {
        frames.clear();
    }

    /**
     * Removes the last inserted frame element. Practically means we switched
     * back to the parent of the current frame
     */
    public Frame pop() {
        return frames.remove(frames.size() - 1);
    }

    /**
     * @return Returns the top frame in the chain.
     */
    public Frame peek() {
        return frames.get(frames.size() - 1);
    }

    /**
     * Appends a frame to the frame chain.
     * @param frame The frame to be added.
     */
    public void push(Frame frame) {
        frames.add(frame);
    }

    /**
     *
     * @return The location of the current frame in the page.
     */
    public Location getCurrentFrameOffset() {
        Location result = new Location(0 ,0);

        for (Frame frame: frames) {
            result = result.offset(frame.getLocation());
        }

        return result;
    }

    /**
     *
     * @return The outermost frame's location, or NoFramesException.
     */
    public Location getDefaultContentScrollPosition() {
        if (frames.size() == 0) {
            throw new NoFramesException("No frames in frame chain");
        }
        return new Location(frames.get(0).getOriginalLocation());
    }

    /**
     *
     * @return The size of the current frame.
     */
    public RectangleSize getCurrentFrameSize() {
        logger.verbose("getCurrentFrameSize()");
        RectangleSize result = frames.get(frames.size()-1).getSize();
        logger.verbose("Done!");
        return result;
    }

    /**
     *
     * @return The inner size of the current frame.
     */
    public RectangleSize getCurrentFrameInnerSize() {
        logger.verbose("GetCurrentFrameInnerSize()");
        RectangleSize result = frames.get(frames.size() - 1).getInnerSize();
        logger.verbose("Done!");
        return result;
    }

    /**
     *
     * @return An iterator to go over the frames in the chain.
     */
    @Override
    public Iterator iterator() {
        return new Iterator() {
            Iterator framesIterator = frames.iterator();
            public boolean hasNext() {
                return framesIterator.hasNext();
            }

            public Frame next() {
                return framesIterator.next();
            }

            public void remove() {
                throw new EyesException("Remove is forbidden using the iterator!");
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy