Secondary Windows (Popup Windows)

Applications may open secondary windows as a new window or in a new tab. This section shows how to automate secondary windows.
"Popup" is a loose term used to describe any dialog or window that is opened outside of the base window. In Sahi's case, we refer to secondary windows when we say "popup window".
We handle secondary windows using 2 APIs.

_selectWindow

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
3.537.0.17.0.0NA9.0.0

Available for modes: Browser | Windows | Java | SAP

_selectWindow([$windowIdentifier])

Arguments
$windowIdentifierstring optional the windowIdentifier can be the window name, the window title, or the window URL, or a regular expression of any of these. To reference the base window, omit this parameter or use null. The windowIdentifier can also be the sahiWinId.

Returns
null

Details

info This is the recommended way of working with secondary windows.
This API allows selecting a window before performing further actions.
// switch to popWin popup window
_selectWindow("popWin");
// perform actions on popWin
_assertEqual("Link Test", _getText(_link(0))); // no mention of popWin needed
var $href;
_set($href, _link(0).href); // no mention of popWin needed
...
// switch back to base window
_selectWindow();
// perform actions on base window
_click(_link("Window Open Test"));
_wait(2000);
var $recentWindowId = _getRecentWindow().sahiWinId;
_selectWindow($recentWindowId);
// perform actions on popWin
_click(_link("Form Test"));
_click(_link("Close Self"));
// switch back to base window
_selectWindow();
// perform actions on base window


_popup

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
3.527.0.17.0.0NANA

Available for modes: Browser | Windows | Java

_popup([$windowIdentifier])

Arguments
$windowIdentifierstring optional the windowIdentifier can be the window name, the window title, or the window URL, or a regular expression of any of these. To reference the base window, omit this parameter or use null. The windowIdentifier can also be the sahiWinId.

Returns
null

Details

This was the default way of accessing secondary windows. Any step to be executed on a particular secondary window would be prefixed by _popup("windowIdentifier").

warning _popup is ONLY used as a PREFIX to Sahi Action APIs
// clicks link on popup of name popupWindow
_popup("popupWindow")._click(_link(0));
// popup by name regular expression
_popup("/pop.*Win/")._click(_link(0));
// popup by title
_popup("Popup Title")._click(_link(0));
// popup by title regular expression
_popup("/pup Tit/")._click(_link(0));
// popup by URL
_popup("http://sahi.co.in")._click(_link(0));
// poppu by URL regular expression
_popup("/sahi[.]co[.]in/")._click(_link(0));


warning This API is confusing while fetching attribute values or using assertions.
To fetch a value or asserting. this needs to be used a particular way.
_popup("popWin")._assertEqual("Link Test", _getText(_link(0))); // CORRECT
_assertEqual("Link Test", _popup("popWin")._getText(_link(0))); // WRONG

var $href;
_popup("popWin")._set($href, _link(0).href); // CORRECT
_set($href, _popup("popWin")._link(0).href); // WRONG


_getWindows

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
5.1.0NANA7.5.0NANA

Available for modes: Browser | Windows | Java (8.0.0)

_getWindows([$activePeriod[, $includeIframes]])

Arguments
$activePeriodinteger optional time in milliseconds to return windows within the time. Default is 0, which returns all windows. This argument is valid only in Browser Mode.
$includeIframesboolean optional flag to get all windows including iframes. Default is false, which returns non-iframe windows. This argument is valid only in Browser Mode. This argument is available since Sahi Pro v9.1.0.

Returns
li_mpArray of window-property objects

Details

Returns an array of window-property objects. A window-property is just an associative array with the following attributes:
windowNamewindow name
windowTitlewindow title
windowURLURL of top window
wasOpened'0' or '1' as string. 0 means base window, 1 means popup window
domaindocument.domain of window, if set
isIFrame'0' or '1' as string. 1 means iframe window
initialTimeTimestamp when window was opened
lastTimeTimestamp of last ping from that window
sahiWinIdsahiWinId for that window
Example:
_navigateTo("http://sahi.co.in/demo/");
_click(_link("Window Open Test With Title"));
_wait(2000);
var $windows = _getWindows();
_assertEqual(2, $windows.length);
_assertEqual('With Title', $windows[1].windowTitle);
_assertEqual("1", $windows[1].wasOpened);
info In Windows mode the returned array contains only windowName & windowTitle attribute..


_windowExists

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
6.0.0NANA7.5.0NANA

Available for modes: Browser | Windows | Java (9.0.0)

_windowExists($windowIdentifier[, $activePeriod])

Arguments
$windowIdentifierstring The windowIdentifier can be the window name, the window title, the window URL or the sahiWinId, or a regular expression of any of these.
$activePeriodinteger optional Time interval in milliseconds within which the specified window is checked for activity. Default is -1, which means the specified window is checked amongst all windows opened during the session. Available only for browser.

Returns
booleantrue if the window exists, else false

Details

Returns true if the window exists.

info If you want to check that a window no longer exists after a particular step, do the following. After the step, add a wait for 3000 milliseconds and then call _windowExists with activePeriod as 3000. _windowExists should return false for the window.

NOTE that if you pass a larger activePeriod, _windowExists would return true, because the window was active at that past time.

Refer to the example below for usage.
warning If activePeriod is not passed, _windowExists will return true if the window was ever opened during the session, even if the window is no longer open.
Example:
_navigateTo("/demo/");
_click(_link("Window Open Test"));
_wait(3000);
var $exists = _windowExists("/demo/");
var $exists2 = _windowExists("popWin");
_assertTrue($exists);
_assertTrue($exists2);
var $recentWindowId = _getRecentWindow().sahiWinId;
var $exists3 = _windowExists($recentWindowId);
_assertTrue($exists3);
_selectWindow("popWin");
_closeWindow(); // Closes the popup window
_selectWindow();
_wait(3000); // Wait for 3 seconds
var $exists3 = _windowExists("popWin", 3000); // Now _windowExists returns false.
_assertFalse($exists3);


_getRecentWindow

Since: Sahi ProSahi OSSahi Pro StarterDesktop Add-OnMobile Add-OnSAP Add-On
6.0.0NANANANANA

Available for modes: Browser

_getRecentWindow()

Arguments
None

Returns
mpMost recent (last opened) window

Details

Returns the most recent (last opened) window. Refer to _getWindows for all properties of window object returned.

info NOTE: If you are checking for the recent window just after a step that opens the window, add an explicit wait of 2000 to 3000 milliseconds before the _getRecentWindow call.
info This API is quite useful in the following scenario.

Sometimes, an application opens multiple popup windows with dynamic identifiers but with the same regular expression pattern in the identifier. The requirement is to identify the recently opened popup. Since the identifier is dynamic, one cannot use it as is. Since all windows have the same regular expression pattern, one cannot use the regular expression pattern either.

In such a scenario, one can use _getRecentWindow to identify the most recently opened window.
Example:
_navigateTo("/demo/");
_click(_link("Window Open Test"));
_click(_link("Window Open Test With Title"));
_wait(2000);
var $recentWindow = _getRecentWindow();
_assertEqual('With Title', $recentWindow.windowTitle); // This is the window opened by clicking the link "Window Open Test With Title".
_assertEqual("1", $recentWindow.wasOpened);