我使用测试框架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脚本不能工作吗?
发布于 2014-08-19 22:52:49
从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');https://stackoverflow.com/questions/25362985
复制相似问题