Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Dec 7, 2024
2 parents ac0349c + 10e04cf commit 4825800
Showing 1 changed file with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> handles = new HashSet<String>();
Set<String> allContexts = new HashSet<String>();
// 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());
}

Expand All @@ -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());

Expand All @@ -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;
}

Expand Down

0 comments on commit 4825800

Please sign in to comment.