我使用测试框架DalekJS来测试用户界面。为了执行我的测试脚本mytest.js,我在shell中输入:
cd /Applications/XAMPP/xamppfiles/htdocs/tests
dalek mytest.js
,它工作得很好。现在我想用PHP执行相同的脚本。我的代码:
<?php
chdir('/Applications/XAMPP/xamppfiles/htdocs/tests');
$command = 'dalek mytest.js';
exec($command, $return, $return_var);
var_dump($return);
var_dump($return_var);
在浏览器中运行php脚本,输出如下:
array(0) { } int(127)
DalekJS脚本在shell中执行时会生成一个屏幕截图,但在PHP中运行时不会发生任何事情。我也尝试过shell_exec(), system()和passthru()没有成功。你知道为什么PHP脚本不工作吗?
从PHP调用dalek对我来说很好。您的PHP进程是否可能在与用户不同的环境中运行(包含PATH
等)?也许是apache用户之类的?
<?php
// change current working directory of the current PHP process,
// this will subsequently change the initial CWD of exec()
$whereMyTestsAre = __DIR__;
chdir($whereMyTestsAre);
// locate dalek
$dalek = exec('which dalek');
// abort if there is no dalek,
// you may want to check PATH or supply the full path yourself
if (!$dalek) {
echo "could not find dalek executable, please check your path";
$PATH = exec('echo $PATH');
echo '$PATH is ', $PATH, "n";
exit(1);
}
// relative to $whereMyTestsAre
// exec() blocks until the child process (dalek) exits
exec('dalek mytest.js');