Skip to content

Commit

Permalink
Change Message for SwitchToWindow, adding initial context, and all co…
Browse files Browse the repository at this point in the history
…ntexts available
  • Loading branch information
bcivel committed Nov 12, 2024
1 parent 2d3a0eb commit b009c29
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public enum MessageEventEnum {
ACTION_SUCCESS_GETPAGESOURCE(200, "OK", "Page Source has been recorded.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_MOUSEDOWN(200, "OK", "Mouse Left Click pressed on Element '%ELEMENT%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_MOUSEUP(200, "OK", "Mouse Left Click Released on Element '%ELEMENT%'.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_SWITCHTOWINDOW(200, "OK", "Focus of Selenium was changed to Window '%WINDOW%'", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_SWITCHTOWINDOW(200, "OK", "Focus of Selenium was changed to Window '%WINDOW%'. Initial window '%INITIALCONTEXT%'. All contexts available : '%ALLCONTEXTS%'", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_SWITCHTOCONTEXT(200, "OK", "Context was successfully changed to %CONTEXT%.", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_CLOSE_ALERT(200, "OK", "Alert popup is closed !", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
ACTION_SUCCESS_KEYPRESS_ALERT(200, "OK", "Keypress '%KEY%' done on alert popup !", false, false, false, MessageGeneralEnum.EXECUTION_PE_TESTSTARTED),
Expand Down Expand Up @@ -301,7 +301,7 @@ public enum MessageEventEnum {
ACTION_FAILED_SELECT_REGEX_INVALIDPATTERN(281, "FA", "Pattern '%PATTERN%' is not valid. Detailed error : %ERROR%", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_MOUSEUP_NO_SUCH_ELEMENT(282, "FA", "Failed to release click because could not find element '%ELEMENT%'!", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_MOUSEDOWN_NO_SUCH_ELEMENT(283, "FA", "Failed to left click because could not find element '%ELEMENT%'!", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT(280, "FA", "Failed to switch to window because could not find element '%WINDOW%'!", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT(280, "FA", "Failed to switch to window because could not find element '%WINDOW%'! Initial window '%INITIALCONTEXT%'. All contexts available : '%ALLCONTEXTS%'", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_SWITCHTOCONTEXT_NO_SUCH_ELEMENT(280, "FA", "Failed to switch to context '%CONTEXT%' because could not find it! Available contexts are: '%CONTEXTS%'. Detailed error: %ERROR%", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_CLOSE_ALERT(280, "FA", "Failed to close the alert popup ! Please either specify 'ok' or 'cancel'.", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
ACTION_FAILED_KEYPRESS_ALERT(280, "FA", "Failed to keypress '%KEY%' on the alert popup !", true, true, true, MessageGeneralEnum.EXECUTION_FA_ACTION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i
String windowTitle = identifier.getLocator();

String currentHandle;
Set<String> handles = new HashSet<String>();
// Current serial handle of the window.
// Add try catch to handle not exist anymore window (like when popup is closed).
try {
Expand All @@ -1047,15 +1048,18 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i

try {
// Get serials handles list of all browser windows
Set<String> handles = session.getDriver().getWindowHandles();
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));
message.setDescription(message.getDescription()
.replace("%WINDOW%", windowTitle)
.replace("%INITIALCONTEXT%", currentHandle)
.replace("%ALLCONTEXTS%", String.join("-", handles)));
return message;
}
}
Expand All @@ -1076,7 +1080,10 @@ public MessageEvent doSeleniumActionSwitchToWindow(Session session, Identifier i
return parseWebDriverException(exception);
}
message = new MessageEvent(MessageEventEnum.ACTION_FAILED_SWITCHTOWINDOW_NO_SUCH_ELEMENT);
message.setDescription(message.getDescription().replace("%WINDOW%", windowTitle));
message.setDescription(message.getDescription()
.replace("%WINDOW%", windowTitle)
.replace("%INITIALCONTEXT%", currentHandle)
.replace("%ALLCONTEXTS%", String.join("-", handles)));
return message;
}

Expand Down

0 comments on commit b009c29

Please sign in to comment.