Skip to content
Get Free Trial

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.

We handle secondary windows using 2 APIs.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaSAP

_selectWindow([$windowIdentifier])

Arguments

NameTypeDescription
$windowIdentifierstringoptionalthe 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

This function 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
Since
Sahi Pro 3.5
Desktop Engine 7.0.0

Modes BrowserWindowsJava

_popup([$windowIdentifier])

Arguments

NameTypeDescription
$windowIdentifierstringoptionalthe 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

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").

// 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));

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
Since
Sahi Pro 5.1.0
Desktop Engine 7.5.0

Modes BrowserWindowsJava (8.0.0)

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

Arguments

NameTypeDescription
$activePeriodintegeroptionaltime in milliseconds to return windows within the time. Default is 0, which returns all windows. This argument is valid only in Browser Mode.
$includeIframesbooleanoptionalflag 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

Returns an array of window-property objects. A window-property is just an associative array with the following attributes:

Attribute Description
windowName window name
windowTitle window title
windowURL URL of top window
wasOpened ‘0’ or ‘1’ as string. 0 means base window, 1 means popup window
domain document.domain of window, if set
isIFrame ‘0’ or ‘1’ as string. 1 means iframe window
initialTime Timestamp when window was opened
lastTime Timestamp of last ping from that window
sahiWinId sahiWinId 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);
Since
Sahi Pro 6.0.0
Desktop Engine 7.5.0

Modes BrowserWindowsJava (9.0.0)

_windowExists($windowIdentifier[, $activePeriod])

Arguments

NameTypeDescription
$windowIdentifierstringThe windowIdentifier can be the window name, the window title, the window URL or the sahiWinId, or a regular expression of any of these.
$activePeriodintegeroptionalTime 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

Returns true if the window exists.

// 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);
Since
Sahi Pro 6.0.0

Modes Browser

_getRecentWindow()

Arguments

NameTypeDescription
None

Returns

mpMost recent (last opened) window

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

// 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);