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.

Select Window

info This is the recommended way of working with secondary windows.
This API allows selecting a window before performing further actions.

Parameters
$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.
Return Value

Modes Supported :
Raw Script
// 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

Sahi Pro Classic API :_selectWindow




Get Windows

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

Parameters
$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.
Return Value
li_mpArray of window-property objects

Modes Supported :
Raw Script
// 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);

Sahi Pro Classic API :_getWindows


Window Exists

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 Window Exists with activePeriod as 3000. Window Exists should return false for the window.

NOTE that if you pass a larger activePeriod, Window Exists 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, Window Exists will return true if the window was ever opened during the session, even if the window is no longer open.

Parameters
$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.
Return Value
booleantrue if the window exists, else false

Modes Supported :
Raw Script
// 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);

Sahi Pro Classic API :_windowExists


Get Recent Window

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 Get Recent Window 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 Get Recent Window to identify the most recently opened window.

Parameters
None
Return Value
mpMost recent (last opened) window

Modes Supported :
Raw Script
// 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);

Sahi Pro Classic API :_getRecentWindow