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

com.github.markusbernhardt.seleniumlibrary.locators.WindowManager Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha-2.0
Show newest version
package com.github.markusbernhardt.seleniumlibrary.locators;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;

import com.github.markusbernhardt.seleniumlibrary.SeleniumLibraryNonFatalException;

public class WindowManager {

	public static int WINDOW_INFO_INDEX_WINDOW_ID = 0;
	public static int WINDOW_INFO_INDEX_WINDOW_NAME = 1;
	public static int WINDOW_INFO_INDEX_DOCUMENT_TITLE = 2;
	public static int WINDOW_INFO_INDEX_DOCUMENT_URL = 3;

	protected enum WindowManagerStrategy {
		DEFAULT {

			@Override
			public void select(WebDriver webDriver, SelectCoordinates selectCoordinates) {
				if (selectCoordinates.criteria == null || selectCoordinates.criteria.toLowerCase().equals("null")) {
					webDriver.switchTo().window("");
					return;
				}

				try {
					NAME.select(webDriver, selectCoordinates);
					return;
				} catch (Throwable ignored) {
				}

				try {
					TITLE.select(webDriver, selectCoordinates);
					return;
				} catch (Throwable ignored) {
				}
				throw new SeleniumLibraryNonFatalException("Unable to locate window with name or title '"
						+ selectCoordinates.criteria + "'");
			}
		},
		TITLE {

			@Override
			public void select(WebDriver webDriver, final SelectCoordinates selectCoordinates) {
				String selectionCriteria = selectCoordinates.criteria;
				selectMatching(webDriver,
						currentWindowInfo -> currentWindowInfo.get(WINDOW_INFO_INDEX_DOCUMENT_TITLE).trim()
								.equalsIgnoreCase(selectionCriteria),
						String.format("Unable to locate window with title '%s'", selectionCriteria));
			}
		},
		NAME {

			@Override
			public void select(WebDriver webDriver, final SelectCoordinates selectCoordinates) {
				String selectionCriteria = selectCoordinates.criteria;
				selectMatching(webDriver,
						currentWindowInfo -> currentWindowInfo.get(WINDOW_INFO_INDEX_WINDOW_NAME).trim().equalsIgnoreCase(selectionCriteria),
						String.format("Unable to locate window with name '%s'", selectionCriteria));
			}
		},
		URL {

			@Override
			public void select(WebDriver webDriver, final SelectCoordinates selectCoordinates) {
				String selectionCriteria = selectCoordinates.criteria;
				selectMatching(webDriver,
						currentWindowInfo -> currentWindowInfo.get(WINDOW_INFO_INDEX_DOCUMENT_URL).trim().equalsIgnoreCase(selectionCriteria),
						String.format("Unable to locate window with URL '%s'", selectionCriteria));
			}
		};

		abstract public void select(WebDriver webDriver, SelectCoordinates selectCoordinates);

		protected static void selectMatching(WebDriver webDriver, Matcher matcher, String error) {
			String startingHandle = null;
			try {
				startingHandle = webDriver.getWindowHandle();
			} catch (NoSuchWindowException e) {
				// Window of current WebDriver instance is already closed
			}

			for (String handle : webDriver.getWindowHandles()) {
				webDriver.switchTo().window(handle);
				if (matcher.match(getCurrentWindowInfo(webDriver))) {
					return;
				}
			}

			if (startingHandle != null) {
				webDriver.switchTo().window(startingHandle);
			}
			throw new SeleniumLibraryNonFatalException(error);
		}
	}

	public static List getWindowIds(WebDriver webDriver) {
		List windowIds = new ArrayList<>();
		for (List windowInfo : getWindowInfos(webDriver)) {
			windowIds.add(windowInfo.get(0));
		}
		return windowIds;
	}

	public static List getWindowNames(WebDriver webDriver) {
		List windowNames = new ArrayList<>();
		for (List windowInfo : getWindowInfos(webDriver)) {
			windowNames.add(windowInfo.get(1));
		}
		return windowNames;
	}

	public static List getWindowTitles(WebDriver webDriver) {
		List windowTitles = new ArrayList<>();
		for (List windowInfo : getWindowInfos(webDriver)) {
			windowTitles.add(windowInfo.get(2));
		}
		return windowTitles;
	}

	public static List> getWindowInfos(WebDriver webDriver) {
		String startingHandle = null;
		try {
			startingHandle = webDriver.getWindowHandle();
		} catch (NoSuchWindowException e) {
			// Window of current WebDriver instance is already closed
		}

		List> windowInfos = new ArrayList<>();
		try {
			for (String handle : webDriver.getWindowHandles()) {
				webDriver.switchTo().window(handle);
				windowInfos.add(getCurrentWindowInfo(webDriver));
			}
		} finally {
			if (startingHandle != null) {
				webDriver.switchTo().window(startingHandle);
			}
		}
		return windowInfos;
	}

	public static void select(WebDriver webDriver, String locator) {
		if (webDriver == null) {
			throw new SeleniumLibraryNonFatalException("WindowManager.select: webDriver is null.");
		}

		SelectCoordinates selectCoordinates = new SelectCoordinates();
		WindowManagerStrategy strategy = parseLocator(selectCoordinates, locator);
		strategy.select(webDriver, selectCoordinates);
	}

	protected static WindowManagerStrategy parseLocator(SelectCoordinates selectCoordinates, String locator) {
		String prefix = null;
		String criteria = locator;
		if (locator != null && locator.length() > 0) {
			String[] locatorParts = locator.split("=", 2);
			if (locatorParts.length == 2) {
				prefix = locatorParts[0].trim().toUpperCase();
				criteria = locatorParts[1].trim();
			}
		}

		if (prefix == null || prefix.equals("name")) {
			if (criteria == null || criteria.toLowerCase().equals("main")) {
				criteria = "";
			}
		}

		WindowManagerStrategy strategy = WindowManagerStrategy.DEFAULT;
		if (prefix != null) {
			strategy = WindowManagerStrategy.valueOf(prefix);
		}
		selectCoordinates.criteria = criteria;
		return strategy;
	}

	@SuppressWarnings("unchecked")
	protected static List getCurrentWindowInfo(WebDriver webDriver) {
		return (List) ((JavascriptExecutor) webDriver)
				.executeScript("return [ window.id, window.name, document.title, document.url ];");
	}

	protected static class SelectCoordinates {
		String criteria;
	}

	protected interface Matcher {
		boolean match(List currentWindowInfo);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy