Search code examples
scriptingwebpagetest

How to save a variable in Webpagetest script


Is there a way to save variable in a Webpagetest script? e.g. I have this javascript code that works in my developer tools:

var range = document.createRange();
var sel = window.getSelection();
range.setStart(document.getElementById("editable").childNodes[0], 5);
sel.removeAllRanges();
sel.addRange(range);

I need the range variable to be saved as in line 1 so that I can modify it in Line 3, and reuse it in line 5. If i try to execute the following WPT command.

   execAndWait var range=document.createRange() 
   //or execAndWait range=document.createRange()

I get an error. Does this mean there is no way I can modify a variable in Webpagetest script?


Solution

  • If you are using a script, you first need to navigate to the page where you want to run the script (navigate command). Assuming you're doing that, you can just collapse your whole script into a single line and run it all in one command:

    navigate    https://www.example.com/
    execAndWait    var range = document.createRange(); var sel = window.getSelection(); range.setStart(document.getElementById("editable").childNodes[0], 5); sel.removeAllRanges(); sel.addRange(range);