catchpoint / WebPageTest.agent

Cross-platform WebPageTest agent
Other
214 stars 138 forks source link

Conditional statement not working #192

Open anildani opened 6 years ago

anildani commented 6 years ago

I am trying to run following script. I want to launch google in first view and yahoo in repeat view but it's not working. Can you please check this and provide support

if      cached  0
navigate       google.com
else
navigate       yahoo.com
endif
wildlyinaccurate commented 6 years ago

if/else/endif are among the script commands that are not supported as per the README.

anildani commented 6 years ago

Agreed. Patrick informed me to report an issue for this.

FYI - I have been able to proceed with my work using following Javascript using local storage and ternary operator as suggested by Patrick.

navigate            google.com
execAndWait      document.getElementsByTagName('input')[0].value=localStorage.getItem('test2')?'Jan':'Feb'
execAndWait      document.getElementsByTagName('input')[0].dispatchEvent(new Event("input"));
execAndWait      localStorage.test2=1
pmeenan commented 6 years ago

Yep. This is basically a placeholder for adding support. Filtering and modifying the script at runtime to handle the if/else/endif support shouldn't be that difficult, it's just not a feature that gets used much so it hasn't been a priority.

kaushal-potdar commented 5 years ago

Hey, Could you please help by a sample example ? i use the following script and it doesnt wotrk :

logdata 1 execAndWait if(localStorage.getItem('test2') == 2){ navigate https://www.raffles.com/ execAndWait }else{ navigate https://www.google.co.in/ execAndWait localStorage.test2=2 execAndWait }

Could you please correct me ?

pmeenan commented 5 years ago

You are mixing and matching javascript (which runs in-browser) and the scripting engine (which runs on the agent). I'd say you should just use an execAndWait command and run whatever javascript logic you want to do in the browser but localstorage is not available outside the context of a page.

Without knowing exactly what you are trying to do, something like this might work:

logData 0
navigate    https://www.webpagetest.org/blank.html
logData 1
execAndWait if(localStorage.getItem('test2') == 2){window.location='https://www.raffles.com/'}else{localStorage.test2=2;window.location='https://www.google.co.in/'}
kaushal-potdar commented 5 years ago

Thanks @pmeenan ! Your solution helps!