Php 如何使用Behat和Mink关闭单个选项卡,而不关闭所有选项卡?

Php 如何使用Behat和Mink关闭单个选项卡,而不关闭所有选项卡?,php,bdd,behat,mink,Php,Bdd,Behat,Mink,我们有一个测试,单击一个链接,然后转到新打开的选项卡,检查它是否转到了正确的页面。有没有办法关闭这个新打开的选项卡 步骤定义当前如下所示: /** * @Given /^the (?:|current )(?:url|page address|address|address of page) (?:should|does)(n\'t| not)? match "([^"]*)"$/ */ public function urlMatch($not, $com

我们有一个测试,单击一个链接,然后转到新打开的选项卡,检查它是否转到了正确的页面。有没有办法关闭这个新打开的选项卡

步骤定义当前如下所示:

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

    }
最后一个选项不起作用,因为它只是停止整个会话


有什么建议吗?

如果您有一个支持执行JavaScript代码的Mink会话,您可以通过以下步骤关闭当前选项卡

/**
 * @Given /^I close the current (?:window|tab)$/
 */
public function closeCurrentWindow()
{
    $this->getSession()->executeScript("window.open('','_self').close();");
}

要讨论哪一段JavaScript代码在哪种浏览器中最有效,请转到那里:

据我所知,这还没有解决,因为它需要启用JavaScript。是貂皮公开发行的链接。如何将CMD+W发送到chromedriver?这可能行得通,因为这是关闭MacOSX选项卡的快捷方式。。。