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

net.serenitybdd.screenplay.actions.SwitchToNewWindow Maven / Gradle / Ivy

There is a newer version: 4.2.8
Show newest version
package net.serenitybdd.screenplay.actions;

import java.util.Set;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Task;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
import net.serenitybdd.annotations.Step;

/**
 * If you just opened a (second?) new window and want to switch to it.
 */
public class SwitchToNewWindow implements Task {

    private static final Logger log = LoggerFactory.getLogger(SwitchToNewWindow.class);

    /**
     * Call this from {@link Switch#toNewWindow()}.
     */
    SwitchToNewWindow() { }

    @Override
    @Step("{0} switches to new window")
    public  void performAs(T actor) {

	WebDriver driver = BrowseTheWeb.as(actor).getDriver();

	Set allHandles = driver.getWindowHandles();
	if (allHandles.size() < 2) {
	    log.error("Not enough windows open!");
	    return;
	}

	String firstHandle = driver.getWindowHandle();
	Dimension windowSize = driver.manage().window().getSize();

	for (String handle : allHandles) {
	    if (!handle.equals(firstHandle)) {
		driver.switchTo().window(handle);
		break;
	    }
	}

	// new window is different size when headless
	if (!driver.manage().window().getSize().equals(windowSize)) {
	    log.warn("Window size does not match!");
	    driver.manage().window().setSize(windowSize);
	}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy