use of org.openqa.selenium.NoSuchWindowException in project cerberus-source by cerberustesting.
the class WebDriverService method doSeleniumActionSwitchToWindow.
@Override
public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier identifier) {
MessageEvent message;
String windowTitle = identifier.getLocator();
String currentHandle;
// Add try catch to handle not exist anymore window (like when popup is closed).
try {
currentHandle = session.getDriver().getWindowHandle();
} catch (NoSuchWindowException exception) {
currentHandle = null;
LOG.debug("Window is closed ? " + exception.toString());
}
try {
// Get serials handles list of all browser windows
Set<String> handles = session.getDriver().getWindowHandles();
// Loop into each of them
for (String windowHandle : handles) {
if (!windowHandle.equals(currentHandle)) {
session.getDriver().switchTo().window(windowHandle);
if (checkIfExpectedWindow(session, identifier.getIdentifier(), identifier.getLocator())) {
message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWITCHTOWINDOW);
message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));
return message;
}
}
LOG.debug("windowHandle=" + windowHandle);
}
} catch (NoSuchElementException exception) {
LOG.debug(exception.toString());
} catch (TimeoutException exception) {
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_TIMEOUT);
message.setDescription(message.getDescription().replace("%TIMEOUT%", String.valueOf(session.getCerberus_selenium_wait_element())));
LOG.warn(exception.toString());
return message;
} catch (WebDriverException exception) {
LOG.warn(exception.toString());
return parseWebDriverException(exception);
}
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT);
message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));
return message;
}
use of org.openqa.selenium.NoSuchWindowException in project spring-boot by spring-projects.
the class WebMvcTestWebDriverIntegrationTests method shouldBeADifferentWebClient.
@Test
public void shouldBeADifferentWebClient() throws Exception {
this.webDriver.get("/html");
WebElement element = this.webDriver.findElement(By.tagName("body"));
assertThat(element.getText()).isEqualTo("Hello");
try {
ReflectionTestUtils.invokeMethod(previousWebDriver, "getCurrentWindow");
fail("Did not call quit()");
} catch (NoSuchWindowException ex) {
// Expected
}
assertThat(previousWebDriver).isNotNull().isNotSameAs(this.webDriver);
}
use of org.openqa.selenium.NoSuchWindowException in project cerberus-source by cerberustesting.
the class WebDriverService method doSeleniumActionManageDialog.
@Override
public MessageEvent doSeleniumActionManageDialog(Session session, Identifier identifier) {
MessageEvent message;
try {
if ("ok".equalsIgnoreCase(identifier.getLocator())) {
// Accept javascript popup dialog.
session.getDriver().switchTo().alert().accept();
session.getDriver().switchTo().defaultContent();
message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSE_ALERT);
return message;
} else if ("cancel".equalsIgnoreCase(identifier.getLocator())) {
// Dismiss javascript popup dialog.
session.getDriver().switchTo().alert().dismiss();
session.getDriver().switchTo().defaultContent();
message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLOSE_ALERT);
return message;
}
} catch (NoSuchWindowException exception) {
// Add try catch to handle not exist anymore alert popup (like when popup is closed).
LOG.debug("Alert popup is closed ? " + exception.toString());
} catch (TimeoutException exception) {
LOG.warn(exception.toString());
} catch (WebDriverException exception) {
LOG.debug("Alert popup is closed ? " + exception.toString());
return parseWebDriverException(exception);
}
return new MessageEvent(MessageEventEnum.ACTION_FAILED_CLOSE_ALERT);
}
use of org.openqa.selenium.NoSuchWindowException in project selenium_java by sergueik.
the class PlunkerTest method testFullScreeenInNewWindow.
// Failed tests: afterMethod(com.mycompany.app.PlunkerTest): no such window:
// target window already closed(..)
@Test(enabled = false)
public void testFullScreeenInNewWindow() {
projectId = "WFJYrM";
driver.get(String.format("https://plnkr.co/edit/%s/?p=info", projectId));
WebElement runButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > nav button i.icon-play")));
assertThat(runButton, notNullValue());
highlight(runButton);
runButton.click();
WebElement previewIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("iframe[name='plunkerPreviewTarget']")));
assertThat(previewIframe, notNullValue());
WebDriver iframe = driver.switchTo().frame(previewIframe);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
// System.err.println(iframe.getPageSource());
// the fullscreen button is not in the preview frame
driver.switchTo().defaultContent();
WebElement fullScreenButton = null;
try {
fullScreenButton = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
Optional<WebElement> e = d.findElements(By.cssSelector("button#expand-preview")).stream().filter(o -> {
String t = o.getAttribute("title");
return (Boolean) (t.contains("Launch the preview in a separate window"));
}).findFirst();
return (e.isPresent()) ? e.get() : (WebElement) null;
}
});
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
}
assertThat(fullScreenButton, notNullValue());
String currentHandle = null;
try {
currentHandle = driver.getWindowHandle();
// System.err.println("Thread: Current Window handle: " + currentHandle);
} catch (NoSuchWindowException e) {
}
// open fullscreen view in a new browser window.
String openinLinkNewBrowserWindow = Keys.chord(Keys.SHIFT, Keys.RETURN);
fullScreenButton.sendKeys(openinLinkNewBrowserWindow);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
// confirm it opens in a new browser window.
windowHandles = driver.getWindowHandles();
assertThat(windowHandles.size(), equalTo(2));
Iterator<String> windowHandleIterator = windowHandles.iterator();
while (windowHandleIterator.hasNext()) {
String handle = windowHandleIterator.next();
if (!handle.equals(currentHandle)) {
driver.switchTo().window(handle);
assertThat(getBaseURL(), equalTo("https://run.plnkr.co"));
// System.err.println(getBaseURL());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
driver.close();
// System.err.println("After close");
try {
driver.switchTo().defaultContent();
System.err.println("After defaultcontext");
} catch (NoSuchWindowException e) {
}
}
}
}
use of org.openqa.selenium.NoSuchWindowException in project selenium_java by sergueik.
the class PlunkerTest method testFullScreeen.
@Test(enabled = true)
public void testFullScreeen() {
projectId = "WFJYrM";
driver.get(String.format("https://plnkr.co/edit/%s/?p=info", projectId));
WebElement runButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > nav button i.icon-play")));
assertThat(runButton, notNullValue());
highlight(runButton);
runButton.click();
WebElement previewIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("iframe[name='plunkerPreviewTarget']")));
assertThat(previewIframe, notNullValue());
WebDriver iframe = driver.switchTo().frame(previewIframe);
sleep(500);
// System.err.println(iframe.getPageSource());
// the fullscreen button is not in the preview frame
driver.switchTo().defaultContent();
WebElement fullScreenButton = null;
try {
fullScreenButton = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
Optional<WebElement> e = d.findElements(By.cssSelector("button#expand-preview")).stream().filter(o -> {
String t = o.getAttribute("title");
return (Boolean) (t.contains("Launch the preview in a separate window"));
}).findFirst();
return (e.isPresent()) ? e.get() : (WebElement) null;
}
});
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
}
assertThat(fullScreenButton, notNullValue());
// confirm it opens in a new tab.
// fullScreenButton.click();
// alternatively, enforce open in a new tab:
String openinLinkNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
fullScreenButton.sendKeys(openinLinkNewTab);
sleep(500);
// confirm it opens in a new tab.
String currentHandle = null;
try {
currentHandle = driver.getWindowHandle();
// System.err.println("Thread: Current Window handle: " + currentHandle);
} catch (NoSuchWindowException e) {
}
windowHandles = driver.getWindowHandles();
assertThat(windowHandles.size(), equalTo(2));
// String handle = windowHandleIterator.next();
for (String handle : windowHandles) {
if (!handle.equals(currentHandle)) {
driver.switchTo().window(handle);
assertThat(getBaseURL(), equalTo("https://run.plnkr.co"));
// System.err.println(getBaseURL());
driver.switchTo().defaultContent();
}
}
}
Aggregations