0

I'm new to using Selenium 2 (via the Firefox Plugin) -- I haven't found this answer elsewhere on the net.

Is it possible to verify if a <script> element is present in the code of a page?

I need to frequently check a number of pages that require a range of script links and on page script block elements. I haven't been able to figure this part out. One example is to check to see if the Google Analytics script is on a page. Here's the sample code that is expected:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||'
   function(){
 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'xx-x-0000-00', 'somedomain.com');
</script>

Thank you in advance!

SiKing
  • 10,003
  • 10
  • 39
  • 90
JPFotoz
  • 95
  • 4
  • 14
  • This answer suggests you can use can use `getAttribute("innerHTML")`:http://stackoverflow.com/questions/20497607/how-to-get-the-contents-of-a-script-tag-in-selenium – Richard Jul 17 '14 at 18:47
  • I'm using the Selenium IDE Firefox Plugin to create the HTML script. How / Where would I include the use of the getAtribute? – JPFotoz Jul 17 '14 at 19:04
  • Does anyone have a solution? @Richard - can you elaborate? – JPFotoz Jul 18 '14 at 19:50

1 Answers1

0

With Selenium IDE, you can get attributes like so:

<tr>
<td>storeAttribute</td>
<td>//script@innerHTML</td>
<td>x</td>
</tr>
<tr>
<td>echo</td>
<td>${x}</td>
<td></td>
</tr>

You may need a better selector for the <script> element.

I hope this helps.

Richard
  • 8,961
  • 3
  • 38
  • 47
  • Trying what you suggested. Not getting desired result. Get error messages when using code such as: storeAttribute //script[contains(@src, 'filename.js')] x echo ${x} The error message is: [error] Invalid xpath [2]: //script[contains( Can you use my example as reference, given that it is located in the HEAD of a page. Thanks! – JPFotoz Jul 21 '14 at 19:16
  • I managed to get it to work, but not sure if this is the proper code. I figure I can duplicate this for each script I need to test for: type xpath=//script[@src='//path-to/script/location.js'] IS THIS CORRECT? It fails if I run it against a page known not to have the script present - thus proving it validates. – JPFotoz Jul 21 '14 at 19:53
  • @photoman I'm only passingly familiar with Selenium IDE, but that looks right from the reading I've done. – Richard Jul 21 '14 at 20:05