Skip to content

Commit

Permalink
WebDriver: added actions for managing tabs (Codeception#3929)
Browse files Browse the repository at this point in the history
* WebDriver: added actions for managing tabs

* replaced ctrl-t with JS open target=_blank page, fixes Firefox compatibility
  • Loading branch information
DavertMik authored Jan 19, 2017
1 parent 2de51cc commit 28ff813
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2755,4 2755,88 @@ public function scrollTo($selector, $offsetX = null, $offsetY = null)
$y = $el->getLocation()->getY() $offsetY;
$this->webDriver->executeScript("window.scrollTo($x, $y)");
}

/**
* Opens a new browser tab (wherever it is possible) and switches to it.
*
* ```php
* <?php
* $I->openNewTab();
* ```
* Tab is opened by sending `ctrl-t` key into browser.
* If ctrl-t does not open a tab in current browser, nothing happens.
*
*/
public function openNewTab()
{
$this->executeJS("window.open('about:blank','_blank');");
$this->switchToNextTab();
}

/**
* Closes current browser tab and switches to previous active tab.
*
* ```php
* <?php
* $I->closeTab();
* ```
*/
public function closeTab()
{
$prevTab = $this->getRelativeTabHandle(-1);
$this->webDriver->close();
$this->webDriver->switchTo()->window($prevTab);
}

/**
* Switches to next browser tab.
* An offset can be specified.
*
* ```php
* <?php
* // switch to next tab
* $I->switchToNextTab();
* // switch to 2nd next tab
* $I->switchToNextTab();
* ```
*
* @param int $offset 1
*/
public function switchToNextTab($offset = 1)
{
$tab = $this->getRelativeTabHandle($offset);
$this->webDriver->switchTo()->window($tab);
}

/**
* Switches to next browser tab.
* An offset can be specified.
*
* ```php
* <?php
* // switch to previous tab
* $I->switchToNextTab();
* // switch to 2nd previous tab
* $I->switchToNextTab(-2);
* ```
*
* @param int $offset 1
*/
public function switchToPreviousTab($offset = 1)
{
$this->switchToNextTab(0 - $offset);
}

protected function getRelativeTabHandle($offset)
{
if ($this->isPhantom()) {
throw new ModuleException($this, "PhantomJS doesn't support tab actions");
}
$handle = $this->webDriver->getWindowHandle();
$handles = $this->webDriver->getWindowHandles();
$idx = array_search($handle, $handles);
return $handles[($idx $offset) % count($handles)];
}


}
24 changes: 24 additions & 0 deletions tests/web/WebDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,4 872,28 @@ public function testRightClick()
$this->module->clickWithRightButton('#element2', 70, 75);
$this->module->see('context, offsetX: 78 - offsetY: 183');
}

public function testBrowserTabs()
{
$this->notForPhantomJS();
$this->module->amOnPage('/form/example1');
$this->module->openNewTab();
$this->module->amOnPage('/form/example2');
$this->module->openNewTab();
$this->module->amOnPage('/form/example3');
$this->module->openNewTab();
$this->module->amOnPage('/form/example4');
$this->module->openNewTab();
$this->module->amOnPage('/form/example5');
$this->module->closeTab();
$this->module->seeInCurrentUrl('example4');
$this->module->switchToPreviousTab(2);
$this->module->seeInCurrentUrl('example2');
$this->module->switchToNextTab();
$this->module->seeInCurrentUrl('example3');
$this->module->closeTab();
$this->module->seeInCurrentUrl('example2');
$this->module->switchToNextTab(2);
$this->module->seeInCurrentUrl('example1');
}
}

0 comments on commit 28ff813

Please sign in to comment.