| 1 | // Modify global object at the page initialization. |
| 2 | // In this example, effectively Math.random() always returns 0.42. |
| 3 | |
| 4 | "use strict"; |
| 5 | var page = require('webpage').create(); |
| 6 | |
| 7 | page.onInitialized = function () { |
| 8 | page.evaluate(function () { |
| 9 | Math.random = function() { |
| 10 | return 42 / 100; |
| 11 | }; |
| 12 | }); |
| 13 | }; |
| 14 | |
| 15 | page.open('http://ariya.github.com/js/random/', function (status) { |
| 16 | var result; |
| 17 | if (status !== 'success') { |
| 18 | console.log('Network error.'); |
| 19 | } else { |
| 20 | console.log(page.evaluate(function () { |
| 21 | return document.getElementById('numbers').textContent; |
| 22 | })); |
| 23 | } |
| 24 | phantom.exit(); |
| 25 | }); |