CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 853
Alert, tabs, frames, iframes
The guide talks about how to switch between different alert, windows and frame.
For more details, read WebDriverAlert
.
$driver->wait()->until(
WebDriverExpectedCondition::alertIsPresent(),
'I am expecting an alert!'
);
// accepting the alert
$driver->switchTo()->alert()->accept();
// cancel/dismiss the alert
$driver->switchTo()->alert()->dismiss();
// get the text on the alert
$message = $driver->switchTo()->alert()->getText();
// send keys to the alert
$driver->switchTo()->alert()->sendKeys($name);
When you click a link or a button, a new window, tab or popup might appear and you need to switch to that window to fill a form or grant the permission, etc....
<a href='/new_window.php' target='_blank'>
Each window has an unique window handle, a string for WebDriver to identify the window and switch between them.
$current_handle = $driver->getWindowHandle();
// Get all window handles available to the current webdriver session.
// This will returns an array of string
$handles = $driver->getWindowHandles();
// Get current window handles first:
$windowHandlesBefore = $this->driver->getWindowHandles();
// Here do some event which opens new tab/window, like click to link etc.
// Then get list of the new window handles and find out which window is the new one.
// Do not use end($handles)! See note below.
$windowHandlesAfter = $this->driver->getWindowHandles();
$newWindowHandle = array_diff($windowHandlesAfter, $windowHandlesBefore);
// Now you know handle of the new window and can switch to it:
$driver->switchTo()->window(reset($newWindowHandle));
Note: Do not use end($handles)
to find the last open window, because getWindowHandles()
does not necessarily return window in the order in which they were opened.
// Default behavior, without specifying window type
$driver->switchTo()->newWindow();
// Try to open new window
$driver->switchTo()->newWindow(WebDriverTargetLocator::WINDOW_TYPE_WINDOW);
// Try to open new tab
$driver->switchTo()->newWindow(WebDriverTargetLocator::WINDOW_TYPE_TAB);
Note the browser may not open the desired window type (tab or window) if it doesn't support it, and may open different supported type instead.
// using the browser shortcut to create a new tab
$driver->getKeyboard()->sendKeys(
array(WebDriverKeys::CONTROL, 't'),
);
// using the browser shortcut to create a new window
$driver->getKeyboard()->sendKeys(
array(WebDriverKeys::CONTROL, 'n'),
);
In order to interact with elements inside a frame or iframe, switch to that frame first!
$myFrame
is a WebDriverElement
, the iframe element found by $driver->findElement($by)
.
$my_frame = $driver->findElement(WebDriverBy::id('my_frame'));
$driver->switchTo()->frame($myFrame);
You can also identify frame/iframe by its identifier (number)"
$driver->switchTo()->frame(1);
$driver->switchTo()->defaultContent();
Switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected. This matches the semantics of calling document.activeElement
in Javascript.
$active_element = $driver->switchTo()->activeElement();
WebDriverTargetLocator
manages the focus of WebDriver
. Read the source code if you want to know more.