我使用测试框架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脚本不起作用吗?
答案 0 :(得分:1)
从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');