Php Codeception接受测试以接受弹出窗口

Php Codeception接受测试以接受弹出窗口,php,testing,automated-tests,codeception,Php,Testing,Automated Tests,Codeception,我目前正在使用Codeception 2.2来测试应用程序。到目前为止,我采取的步骤如下: <?php $I = new AcceptanceTester($scenario); $I->wantTo('perform actions and see result'); $I->amOnPage('/index.php'); $I->fillField('username', 'admin'); $I->fillField('password', 'passwor

我目前正在使用Codeception 2.2来测试应用程序。到目前为止,我采取的步骤如下:

<?php 
$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');
$I->amOnPage('/index.php');
$I->fillField('username', 'admin');
$I->fillField('password', 'password');
$I->click('Sign in');
$I->amOnPage('/index.php?module=CustomReports&view=Edit');
$I->fillField('relatedclient', '******');
$I->fillField('policynumber', '****');
$I->click('Save');
$I->see('You are being redirected to the clients isa report.');
$I->click('OK');    // This is where it fails
$I->see('Client ISA Statement');
?>

目前我正在使用PHP和内联JS,这就是错误发生的地方。我想知道如何接受window.alert以进入下一页。我尝试了
$I->单击('OK')
,但似乎不起作用


谢谢

这在codeception中是一件非常令人困惑的事情。 你可以试一下

$I->acceptPopup()

不幸的是,它可能不起作用。这是由硒驱动程序引起的。有时,当浏览器发出警报时,他们无法获得钩子。关于这些警报,我看到了非常令人困惑的东西。

试试这种方法:

    $I = $this->tester;

    $I->amGoingTo("check whether any JS alert appears and accept it");
    $I->executeInSelenium(function (\Facebook\WebDriver\WebDriver $webdriver) {
        try {
            $webdriver->wait(1.5, 200)->until(
                WebDriverExpectedCondition::alertIsPresent()
            );
            $webdriver->switchTo()->alert()->accept();

        } catch (Exception $e) {
            echo("###ERROR: oops, didnt manage to find the alert. Exception: '" . $e->getTraceAsString() . "' 
            Please contact test developers for investgation");
        }
    });
    if (strlen($this->errorMessage) > 0) {
        $I->comment($this->errorMessage);
    }
希望这对你有帮助。
关于,

另一个可能有效的方法是为“OK”使用更具体的选择器 按钮

这是一个对我有用的方法:

$I->click("//button[contains(@class, 'x-btn-text') and text() = 'OK']");
在本例中,“button”是我要单击的HTML标记,“x-btn-text”是按钮的类,“OK”是标记内的文本

如果按钮具有唯一的id(或者您可以添加一个),则更简单:

<button id="button_id" type="button">OK</button>

$I->click('#button_id');
OK
$I->单击(“#按钮_id”);
请注意,除非您使用WebDriver进行验收测试,否则这可能不起作用。

请尝试一下

 try {
    $I->click('i.e. CLOSE BUTTON OF YOUR POPUP ELEMENT');
    } catch (\PHPUnit_Framework_Exception $e) {
       // failed assertion handled
    }   

这就是最终成功的原因:

$I->waitForElementVisible("//button[contains(@class, 'x-btn-text') and text() = 'Yes']");
$I->click("//button[contains(@class, 'x-btn-text') and text() = 'Yes']");