diff --git a/source/src/main/java/org/cerberus/core/service/webdriver/impl/WebDriverService.java b/source/src/main/java/org/cerberus/core/service/webdriver/impl/WebDriverService.java index 99609a830..e34868488 100644 --- a/source/src/main/java/org/cerberus/core/service/webdriver/impl/WebDriverService.java +++ b/source/src/main/java/org/cerberus/core/service/webdriver/impl/WebDriverService.java @@ -1035,14 +1035,19 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i MessageEvent message; String windowTitle = identifier.getLocator(); - String currentHandle; + String currentWindowId; + String initialContext = ""; + String targetContext = ""; Set handles = new HashSet(); + Set allContexts = new HashSet(); // Current serial handle of the window. // Add try catch to handle not exist anymore window (like when popup is closed). try { - currentHandle = session.getDriver().getWindowHandle(); + currentWindowId = session.getDriver().getWindowHandle(); + initialContext = "URL:"+session.getDriver().getCurrentUrl()+" | Title:"+session.getDriver().getTitle(); } catch (NoSuchWindowException exception) { - currentHandle = null; + currentWindowId = null; + initialContext = "Page has been closed."; LOG.debug("Window is closed ? " + exception.toString()); } @@ -1051,21 +1056,30 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i handles = session.getDriver().getWindowHandles(); // Loop into each of them + String targetHandle = null; for (String windowHandle : handles) { - if (!windowHandle.equals(currentHandle)) { + //if (!windowHandle.equals(currentWindowId)) { session.getDriver().switchTo().window(windowHandle); + allContexts.add("URL:"+session.getDriver().getCurrentUrl()+" | Title:"+session.getDriver().getTitle()); + if (checkIfExpectedWindow(session, identifier.getIdentifier(), identifier.getLocator())) { - message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWITCHTOWINDOW); - message.setDescription(message.getDescription() - .replace("%WINDOW%", windowTitle) - .replace("%INITIALCONTEXT%", currentHandle) - .replace("%ALLCONTEXTS%", String.join("-", handles))); - return message; + targetHandle = windowHandle; + targetContext = "URL:"+session.getDriver().getCurrentUrl()+" | Title:"+session.getDriver().getTitle(); } - } + //} LOG.debug("windowHandle=" + windowHandle); } + if (!StringUtil.isEmptyOrNull(targetHandle)){ + session.getDriver().switchTo().window(targetHandle); + message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SWITCHTOWINDOW); + message.setDescription(message.getDescription() + .replace("%WINDOW%", targetContext) + .replace("%INITIALCONTEXT%", initialContext) + .replace("%ALLCONTEXTS%", String.join("-", allContexts))); + return message; + } + } catch (NoSuchElementException exception) { LOG.debug(exception.toString()); @@ -1081,9 +1095,9 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i } message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT); message.setDescription(message.getDescription() - .replace("%WINDOW%", windowTitle) - .replace("%INITIALCONTEXT%", currentHandle) - .replace("%ALLCONTEXTS%", String.join("-", handles))); + .replace("%WINDOW%", targetContext) + .replace("%INITIALCONTEXT%", initialContext) + .replace("%ALLCONTEXTS%", String.join("-", allContexts))); return message; }