Sahi Pro - 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

_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.

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

_popup

_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.

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

_getWindows([$activePeriod])

Arguments
$activePeriodinteger optional time in milliseconds to return windows within the time. Default is 0, which returns all windows.

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
initialTimeTimestamp when window was opened
lastTimeTimestamp of last ping from 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);