php-phantomjs
php-phantomjs copied to clipboard
Problem with custom scripts (`.proc` file), Do not return anything
I use Custom Scripts.
The .proc file work well, But the .php file don't works.
You can see all files below and results.
PHP 7.0.4 phantomjs 2.1.1 php-phantomjs 4.6.1
test.php
<?php
require_once __DIR__.'/vendor/autoload.php';
use JonnyW\PhantomJs\Client;
use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;
use JonnyW\PhantomJs\Message\Request;
$location = './';
$fileName = 'test';
$serviceContainer = ServiceContainer::getInstance();
$procedureLoader = $serviceContainer->get('procedure_loader_factory')->createProcedureLoader($location);
$client = Client::getInstance();
$client->setProcedure($fileName);
$client->getProcedureLoader()->addLoader($procedureLoader);
$request = $client->getMessageFactory()->createRequest();
$response = $client->getMessageFactory()->createResponse();
$request->setMethod('GET');
$request->setUrl('http://localhost/hello.htm');
$client->send($request, $response);
echo $response->getContent();
test.proc
var wasSuccessful = phantom.injectJs('waitfor.js');
var page = require('webpage').create();
//var url = '{{ input.getUrl() }}';
var url = 'http://localhost/hello.htm';
page.open(url, function (status) {
waitFor(function () {
return page.evaluate(function () {
if ('complete' === document.readyState) {
return true;
}
return false;
});
}, function () {
page.evaluate(function () {
document.getElementById('hello').click();
});
console.log(page.content);
phantom.exit();
});
});
hello.htm
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#hello").click(function() {
$("#result").html("Hello!");
});
});
</script>
</head>
<body>
<a href="#" id="hello">hello</a><br>
Result: <div id="result"></div>
</body>
</html>
waitfor.js
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
// console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condi>
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
}
phantomjs test.proc
output: (You can see effective click on hyperlink and see Hello! in result.)
<!DOCTYPE html><html><head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#hello").click(function() {
$("#result").html("Hello!");
});
});
</script>
</head>
<body>
<a href="#" id="hello">hello</a><br>
Result: <div id="result">Hello!</div>
</body></html>
php test.php
output: (Nothing in the output is displayed.)
Oh.I have same your problem...If you code 'phantom.exit();'. The print 'null'