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.
Select Window
Section titled “Select Window”Modes BrowserWindowsJavaSAP
This action allows selecting a window before performing further actions.
Parameters
| Name | Type | Description |
|---|---|---|
$windowIdentifier | stringoptional | 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. |
Raw script
// switch to popWin popup window_selectWindow("popWin");// perform actions on popWin_assertEqual("Link Test", _getText(_link(0))); // no mention of popWin neededvar $href;_set($href, _link(0).href); // no mention of popWin needed...// switch back to base window_selectWindow();// perform actions on base windowSahi Pro Classic API _selectWindow
Get Windows
Section titled “Get Windows”Modes BrowserWindowsJava
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 |
Parameters
| Name | Type | Description |
|---|---|---|
$activePeriod | integeroptional | time in milliseconds to return windows within the time. Default is 0, which returns all windows. This argument is valid only in Browser Mode. |
$includeIframes | booleanoptional | 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_mp | Array of window-property objects |
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
Section titled “Window Exists”Modes BrowserWindowsJava
Returns true if the window exists.
Parameters
| Name | Type | Description |
|---|---|---|
$windowIdentifier | string | The windowIdentifier can be the window name, the window title, the window URL or the sahiWinId, or a regular expression of any of these. |
$activePeriod | integeroptional | 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
boolean | true if the window exists, else false |
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 secondsvar $exists3 = _windowExists("popWin", 3000); // Now _windowExists returns false._assertFalse($exists3);Sahi Pro Classic API _windowExists
Get Recent Window
Section titled “Get Recent Window”Modes Browser
Returns the most recent (last opened) window. Refer to _getWindows for all properties of window object returned.
Parameters
| Name | Type | Description |
|---|---|---|
None |
Returns
mp | Most recent (last opened) window |
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

