PHP Mink/Zombie-致命例外后处理悬挂过程?
2016-05-27
864
我正在使用带有 Mink+Zombie 驱动程序的 PHP 脚本(安装在 nodejs 无法在 PHP mink 中找到模块“zombie” 上)来自这里: PHP Mink/Zombie - 页面访问返回状态代码 0? ;重新发布以保证完整性:
<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules";
# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';
$URL = "https://demo.centreon.com/centreon";
$USERNAME = "admin";
$PASSWORD = "centreon";
$LFID = "useralias";
$PFID = "password";
$BFID = "submitLogin";
$zsrv = new \Behat\Mink\Driver\NodeJS\Server\ZombieServer();
$zsrv->setNodeModulesPath($nodeModPath . "/"); # needs to end with a trailing '/'
$driver = new \Behat\Mink\Driver\ZombieDriver( $zsrv );
$session = new \Behat\Mink\Session($driver);
// start the session
$session->start();
$session->setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36');
$statcode = 0;
while ($statcode != 200) {
$isleep = rand(2, 7); echo "sleeping $isleep sec...\n";
sleep($isleep);
$session->visit($URL);
// $session->wait(20000, '(0 === jQuery.active)'); # Uncaught exception 'RuntimeException' with message 'Could not establish connection: Connection refused (111)'
$session->wait(20000, '(browser.statusCode > 0)'); ### THIS makes things work?!
$statcode = $session->getStatusCode();
echo " current URL: " . $session->getCurrentUrl() ."\n";
echo " status code: " . $statcode ."\n";
}
$page = $session->getPage();
$el_login = $page->findField($LFID);
$el_password = $page->findField($PFID);
$el_button = $page->find('xpath', '//*[@name="'.$BFID.'"]');//findById($BFID);//findField($BFID);
$el_login->setValue($USERNAME);
$el_password->setValue($PASSWORD);
echo " pressing/clicking login button\n";
$el_button->click();
echo "Page URL after click: ". $session->getCurrentUrl() . "\n";
$page = $session->getPage();
?>
问题是,当脚本运行并执行登录点击时,Mink 无法检测到页面使用的某些 JavaScript 库,并引发异常:
$ php test_php_mink.php
sleeping 4 sec...
current URL: https://demo.centreon.com/centreon/
status code: 200
pressing/clicking login button
PHP Fatal error: Uncaught exception 'Behat\Mink\Exception\DriverException' with message 'Error while processing event 'click': "ReferenceError: Effect is not defined\n at https://demo.centreon.com/centreon/include/common/javascript/modalbox.js:517:1\n at Object.exports.runInContext (vm.js:44:17)\n at window._evaluate (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/lib/document.js:253:75)\n at Object.DOM.languageProcessors.javascript (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/lib/dom/scripts.js:26:12)\n at define.proto._eval (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1477:47)\n at /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:32:22\n at Object.item.check (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:188:11)\n at Object.item.check (/home in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/ZombieDriver.php on line 880
异常为
ReferenceError:未定义效果
,这是由于页面使用了这个 javascript:
<script type="text/javascript" src="./include/common/javascript/scriptaculous/scriptaculous.js?load=effects,dragdrop"></script>
...
new Effect.toggle('header', 'appear', { afterFinish: function() {
但是,更大的问题是,在这次崩溃发生之后,当我第二次运行脚本时,它几乎立即崩溃,这次出现
Error: listen EADDRINUSE 127.0.0.1:8124
:
$ php test_php_mink.php
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Server process has been terminated: (1) [events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE 127.0.0.1:8124
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at Server._listen2 (net.js:1231:14)
at listen (net.js:1267:10)
at net.js:1376:9
at doNTCallback3 (node.js:440:9)
at process._tickCallback (node.js:346:17)
at Function.Module.runMain (module.js:473:11)
at startup (node.js:117:18)
at node.js:951:3
]' in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php:413
Stack trace:
#0 /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php(306): Behat\Mink\Driver\NodeJS\Server->checkAvailability()
#1 /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/ZombieDriver. in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php on line 413
似乎在第一次崩溃之后,仍然有
mink
进程徘徊 - 事实上,那里是:
$ pgrep -fl mink
7659 sh
7660 node
$ ps axf | grep mink
7687 pts/0 S+ 0:00 | \_ grep --color=tty mink
7659 pts/0 S 0:00 sh -c 'node' '/path/to/test_php_mink/vendor/behat/mink-zombie-driver/bin/mink-zombie-server.js'
7660 pts/0 Sl 0:03 \_ node /path/to/test_php_mink/vendor/behat/mink-zombie-driver/bin/mink-zombie-server.js
此时,如果我终止这些进程(例如,使用
pkill -f mink
),那么它们将从进程树中消失,然后我可以再次运行脚本,结果与 OP 中第一次调用的结果相同(然后我必须再次终止进程等)。
处理此问题的最佳/推荐方法是什么,这样我就不必在每次脚本遇到致命异常时手动终止进程?Mink 库中是否有可以实现此功能的东西?
2个回答
为避免致命异常,首先要做的是实现/使用具有适当错误处理的方法。 您可以创建一个方法来检查元素是否不为空,以及是否要抛出自定义异常。
例如,在您的情况下,如果未找到元素, findField 方法将返回 null,在这种情况下, setValue 将在非对象上使用,从而导致致命错误。
lauda
2016-05-31
异常处理的一个例子是:
public function fillWith($locator, $text){
$element = $this->getSession()->getPage()->find("xpath", $locator);
if($element === null){
throw new Exception("Element $locator not found");
}else{
$element->setValue($text);
}
}
lauda
2016-06-01