0

i am trying to test if my script is loaded on a given website and if the script is actually working without any errors onload (later on i will have to do the same for onclick)

So far i have

$testResult = array();
$homepage = 'http://www.example.dk/';
$data = file_get_contents($homepage);
if (strpos($data,'example_script.js'))
{
    $testResult['scriptLoaded'] = true;
    print_r("win");
}else{
    $testResult['scriptLoaded'] = false;
}

Now this loads the page and checks if the javascript is on the page. But how can i read from the console to check if there is any errors while loading the script?

Also is this the right way to check if the script is on the page? The only restriction i have is that i HAVE to use PHP.

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • You're not loading anything, all you're doing there is downloading the source code of the page and verifying if it contains the string `example_script.js`, that does not execute JavaScript or anything like a browser does. – Prix Aug 19 '13 at 09:46
  • @Prix is it possible to emulate it using php? – Marc Rasmussen Aug 19 '13 at 09:48
  • With pure PHP, no, you will need either a headless browser or a javascript emulator or a browser. – Prix Aug 19 '13 at 09:49
  • @MarcRasmussen: Read my answer: there's a link to a headless browser, that uses JS, and a link to ScriptableBrowser. This won't actually validate any JS (that's just not possible), but you could attach an event listener, to the some _"testMe"_ link, and try `$scriptableBrowser->click('testMe');` if your JS is ok, that just might work – Elias Van Ootegem Aug 19 '13 at 09:53

1 Answers1

1

The only thing you can check with your code is weather or not somewhere in the code/contents you've gotten from the given url, there is a string example_script.js. If you were to use the url to this page, you'd get true and "win", too, because the substring will be found.

The JS might be riddled with fauklts, but since PHP doesn't understand Js, you won't be able to see that.
If you want to test your site, without a browser, the only thing I can think of is using phantomjs:

Which can be found Here

Using PHP alone, you might be able to do a couple of checks using scriptable browser, cUrl, and the DOMDocument class (to parse and validate the markup).

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • So with PHP alone i will never be able to read the console of a webbrowers? – Marc Rasmussen Aug 19 '13 at 09:58
  • @MarcRasmussen: Of course not! Think of the console as a private property of the browser. The way it is implemented depends on each browser individually. They're not even required to implement a console (it's not gouverned by ECMA in any way). the browser console is something client-side. A server-side language can't access the client-side software in any meaningful way. You can't install a new OS on a client, nor can you change the system settings from your web page (not in a legit way, anyways). You'll have to resort to a headless browser, or stick to traditional debugging methods. – Elias Van Ootegem Aug 19 '13 at 10:54
  • is it possible to call Phantomjs from php and use its functionaility? – Marc Rasmussen Aug 19 '13 at 11:11
  • Technically, yes, using `proc_open`, or `passthru`, `shell_exec` or `exec` or even `(``)`(backticks). But why would you want to? Just write the JS for phantomjs, execute the script and look at the output – Elias Van Ootegem Aug 19 '13 at 11:13
  • The thing about this is i have to check unknown websites if they have a certain javascript on them and if that script is loaded correctly. Is this possible using Phantomjs – Marc Rasmussen Aug 19 '13 at 11:25
  • @MarcRasmussen: That's exactly why one would use Phantomjs. Just [check how you can monitor the networking activity of a page](https://github.com/ariya/phantomjs/wiki/Network-Monitoring). It's all there. You can get about as much detail as chromium's networking tab in the console gives you. [check this question](http://stackoverflow.com/questions/14905664/getting-more-information-from-phantomjs-syntaxerror-parse-error-message) and spend some time on the docs of phantomjs. There's a lot of interesting projects (jasmine-phantom combo for one) – Elias Van Ootegem Aug 19 '13 at 11:27