We have a test that is clicking a link, and going to the newly opened tab to check if it has gone to the right page. Is there a way to close this newly opened tab?
The step definitions currently look like this:
/**
* @Given /^the (?:|current )(?:url|page address|address|address of page) (?:should|does)(n\'t| not)? match "([^"]*)"$/
*/
public
function urlMatch($not, $compare)
{
$actual = $this->getSession()->getDriver()->getCurrentUrl();
is_numeric(strpos($not, ' not')) || is_numeric(strpos($not, 'n\'t')) ? assertFalse(is_numeric(strpos($actual, $compare)), "Address is " . $compare) : assertTrue(is_numeric(strpos($actual, $compare)), "Address is " . $actual . ", not " . $compare);
}
/**
* @Given /^(?:|.*)(?:A|a) new (?:window|tab)(?:|.*)$/
*/
public
function checkForNewWindow()
{
$windowNames = $this->getSession()->getWindowNames();
if (count($windowNames) > 1) {
return true;
} else {
throw new Exception("A new tab has not been opened");
}
}
/**
* @Given /^I switch to the new (?:window|tab)$/
*/
public
function switchToNewWindow()
{
$windowNames = $this->getSession()->getWindowNames();
if (count($windowNames) > 1) {
$this->getSession()->switchToWindow(end($windowNames));
} else {
throw new Exception("There is not a tab to switch to");
}
}
/**
* @Given /^I close the current (?:window|tab)(?:|.*)$/
*/
public
function closeCurrentWindow()
{
$window = $this->getSession()->getWindowName();
$this->getSession()->stop($window);
}
The final one of which does not work, as it simply stops the entire session.
Any suggestions?