尝试使用 Codeception 和 Yii2 接受本机 js 弹出窗口时出错
Posted
技术标签:
【中文标题】尝试使用 Codeception 和 Yii2 接受本机 js 弹出窗口时出错【英文标题】:Error when trying to accept a native js popup with Codeception and Yii2 【发布时间】:2015-06-28 05:59:46 【问题描述】:我正在尝试关闭 Yii2 生成的确认 js 弹出窗口,以确认删除记录,在本例中为用户,使用 Codeception 和他的。
以下是错误:
[WebDriverException] 远程响应的 JSON 解码失败。 错误代码:4 响应:'无效的命令方法 - 请求 => "headers":"Accept":"application/json","Content-Length":"0","Content-Type":"application/json;charset= UTF-8","Host":"127.0.0.1:4444","httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":"anchor ":"","query":"","file":"alert_text","directory":"/","path":"/alert_text","relative":"/alert_text","port": "","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":" /alert_text","queryKey":,"chunks":["alert_text"],"urlOriginal":"/session/cac855f0-e7f8-11e4-ae75-8baa74cf41b1/alert_text"'
下面是我的代码:
<?php
$username = 'foobar';
$email = 'foo@bar.com';
$I = new AcceptanceTester($scenario);
$I->wantTo('Check that users can update their passwords');
$I->haveInDatabase('user', array('username' => $username, 'email' => $email));
$id = $I->grabFromDatabase('user', 'id', array('username' => $username, 'email' => $email));
$I->amOnPage("/backend/web/index.php/user/$id");
$I->see('Borrar');
$I->click('Borrar');
$I->wait(3);
## This line throws the error
$I->seeInPopup('eliminar este usuario');
## Trying to change to the popup. This doesn't throw any error
$I->executeInSelenium(function (Webdriver $webdriver)
$handles=$webdriver->getWindowHandles();
$last_window = end($handles);
$webdriver->switchTo()->window($last_window);
);
$I->pressKey('body', \WebDriverKeys::ENTER);
## This throwed the error before
$I->acceptPopup();
$I->wait(1);
$I->seeInCurrentUrl('user/list');
$I->dontSeeInDatabase('user', array('username' => $username, 'email' => $email));
【问题讨论】:
【参考方案1】:据我所知,下面的代码告诉 Codeception 完全改变浏览器窗口。我实际上只是用这个确切的代码块编写了一个测试来更改浏览器窗口。也许尝试删除它或评论它并尝试再次运行测试?就我所见,弹出部分看起来还不错
$I->executeInSelenium(function (Webdriver $webdriver)
$handles=$webdriver->getWindowHandles();
$last_window = end($handles);
$webdriver->switchTo()->window($last_window);
);
【讨论】:
以上是关于尝试使用 Codeception 和 Yii2 接受本机 js 弹出窗口时出错的主要内容,如果未能解决你的问题,请参考以下文章
在 yii2/codeception 数据文件中引用夹具记录
Codeception 中的“expect_file()”函数有啥用?
尝试使用 Internet Explorer 11 / IE11 运行 Codeception / Selenium 时出现 PHPUnit_Framework_Exception