Home | History | Annotate | Download | only in webpagereplay
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // webpagereplay/start.js - Start Web Page Replay (WPR) test.
      6 //
      7 // This script is included by webpagereplay/start.html.
      8 // The query parameter "test=TEST_NAME" is required to load the
      9 // test configuration from webpagereplay/tests/TEST_NAME.js
     10 // That JavaScript file defines a global, "pageSets", as a list of lists:
     11 //       [ [url_1, url_2], [url_3], ...],
     12 //       - Before each sublist:
     13 //         Run chrome.browingData.remove and close the connections.
     14 //       - Before each url in a sublist:
     15 //         Close the connections.
     16 //
     17 // These WPR tests use a Chrome extension to load the test URLs.
     18 // The extension loads the test configuration via a DOM elemment
     19 // (id=json).  This script sets the content of that DOM element.
     20 //
     21 // The test runs immediately after being loaded.
     22 //
     23 
     24 
     25 var options = location.search.substring(1).split('&');
     26 function getopt(name) {
     27   var r = new RegExp('^' + name + '=');
     28   for (i = 0; i < options.length; ++i) {
     29     if (options[i].match(r)) {
     30       return options[i].substring(name.length + 1);
     31     }
     32   }
     33   return null;
     34 }
     35 
     36 function LoadTestConfigurationScript(testUrl, callback) {
     37   var testjs = document.createElement('script');
     38   testjs.type = 'text/javascript';
     39   testjs.async = true;
     40   testjs.src = testUrl
     41   var s = document.getElementsByTagName('script')[0];
     42   testjs.addEventListener('load', callback, false);
     43   s.parentNode.insertBefore(testjs, s);
     44 }
     45 
     46 function ReloadIfStuck() {
     47   setTimeout(function() {
     48       var status = document.getElementById('status');
     49       // The status text is set to 'STARTING' by the extension.
     50       if (status.textContent != 'STARTING') {
     51         console.log('Benchmark stuck?  Reloading.');
     52         window.location.reload(true);
     53       }
     54     }, 30000);
     55 }
     56 
     57 function RenderForm() {
     58   var form = document.createElement('FORM');
     59   form.setAttribute('action', 'start.html');
     60 
     61   var label = document.createTextNode('Iterations: ');
     62   form.appendChild(label);
     63 
     64   var input = document.createElement('INPUT');
     65   var iterations = getopt('iterations');
     66   input.setAttribute('name', 'iterations');
     67   input.setAttribute('value', iterations ? iterations : '5');
     68   form.appendChild(input);
     69 
     70   form.appendChild(document.createElement('P'));
     71 
     72   var label = document.createTextNode('Test: ');
     73   form.appendChild(label);
     74 
     75   var input = document.createElement('INPUT');
     76   input.setAttribute('name', 'test');
     77   var test = getopt('test');
     78   input.setAttribute('value', test ? test : '');
     79   form.appendChild(input);
     80 
     81   var input = document.createElement('INPUT');
     82   input.setAttribute('name', 'auto');
     83   var auto = getopt('auto');
     84   input.setAttribute('value', 1);
     85   input.setAttribute('type', 'hidden');
     86   form.appendChild(input);
     87 
     88   form.appendChild(document.createElement('P'));
     89 
     90   input = document.createElement('INPUT');
     91   input.setAttribute('type', 'submit');
     92   input.setAttribute('value', 'Start');
     93   form.appendChild(input);
     94 
     95   document.getElementById('startform').appendChild(form);
     96 }
     97 
     98 
     99 var iterations = getopt('iterations');
    100 var test_name = getopt('test');
    101 var is_auto_start = getopt('auto');
    102 
    103 RenderForm();
    104 if (test_name) {
    105   var testUrl = 'tests/' + test_name + '.js';
    106   LoadTestConfigurationScript(testUrl, function() {
    107       var testConfig = {};
    108       if (iterations) {
    109         testConfig['iterations'] = iterations;
    110       }
    111       // The pageSets global is set by test configuration script.
    112       testConfig['pageSets'] = pageSets;
    113 
    114       if (is_auto_start) {
    115         testConfig['shouldStart'] = 1;
    116         ReloadIfStuck();
    117       }
    118       // Write testConfig to "json" DOM element for the Chrome extension.
    119       document.getElementById('json').textContent = JSON.stringify(testConfig);
    120     });
    121 } else {
    122   console.log('Need "test=TEST_NAME" query parameter.');
    123 }
    124