How to dynamically add a script to the page using watir-webdriver -
How to dynamically add a script to the page using watir-webdriver -
i have *.js script want execute using watir-webdriver during test runs. question there way upload script page using watir-webdriver?
you utilize execute_script method add together script element src attribute js file. method phone call like:
browser.execute_script( "var the_script = document.createelement('script'); the_script.setattribute('src','your_script.js'); document.head.appendchild(the_script);" ) to see works, allow assume have page:
<html> <body> <input type="text" id="field" value="100"> </body> </html> if seek execute jquery on page (the '$' in script), exception occurs because page not know jquery is:
field = browser.text_field p browser.execute_script('return $(arguments[0]).val();', field) #=> $ not defined (selenium::webdriver::error::javascripterror) if add together jquery script file (http://code.jquery.com/jquery-1.10.2.js), via execute_script, able utilize jquery:
browser.execute_script( "var the_script = document.createelement('script'); the_script.setattribute('src','http://code.jquery.com/jquery-1.10.2.js'); document.head.appendchild(the_script);" ) field = browser.text_field p browser.execute_script('return $(arguments[0]).val();', field) #=> "100" watir-webdriver
Comments
Post a Comment