1

I am trying to execute a javascript file through webdriver in C#. The following is what i have so far:

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
(string)js.ExecuteScript("var s = window.document.createElement(\'script\'); s.src = \'E:\\workspace\\test\\jsPopup.js\'; window.document.head.appendChild(s); ");
js.ExecuteScript("return ourFunction");

The content of jsfile.js are

 document.ourFunction =  function(){ tabUrl = window.location.href;
 imagesPath = chrome.extension.getURL("images"); 
 var optionsPopupPath = chrome.extension.getURL("options.html");
 }

However when i execute

js.ExecuteScript("return ourFunction");

It throws an exception of ourFunction not found. What i want to do is run a complete javascript file through js injection or any other method that would let me access the data generated by the js file. Any help ?

Abdul
  • 77
  • 3
  • 9
  • I've never used selenium so apologies if this is dumb, but I would expect you need to include the parens: `js.ExecuteScript("return ourFunction()");` – Crowcoder Mar 22 '16 at 14:45
  • I don't think that is causing the exception. i am following http://stackoverflow.com/questions/17385779/how-do-i-load-a-javascript-file-into-the-dom-using-selenium and the author also does not seem to mind (). – Abdul Mar 22 '16 at 14:47

1 Answers1

1

There are three problems here:

  1. As @Crowcoder points out, you need ourFunction(), not ourFunction, otherwise you'll get an error along the lines of Uncaught ReferenceError: ourFunction is not defined(…)
  2. Next, ourFunction is added to document not the global scope, so you'd need document.ourFunction(), otherwise you'll get the same error.
  3. Finally, the function doesn't return anything, so executing it will return undefined. If you try to return the 'value' of it, you'll get something like Uncaught SyntaxError: Illegal return statement(…) in the browser, or probably null back in your code.

You can test all of this from your browser console without needing to fire up WebDriver.

If you changed the method to:

document.ourFunction = function(){ tabUrl = window.location.href;
  imagesPath = chrome.extension.getURL("images"); 
  var optionsPopupPath = chrome.extension.getURL("options.html");
  return optionsPopupPath;  // return here!
}

Then js.ExecuteScript("return document.ourFunction()"); ought to work.

Update:

(You could maybe try: js.ExecuteScript("return document.ourFunction();"); (added semicolon) but that shouldn't make a difference.)

I'd suggest (in addition to adding the return statement) temporarily commenting out the chrome.extension lines in case these are throwing errors and causing the function to fail to be created. I think that's far the most likely source of failure.

Having done that, this works fine for me in Firefox and Chrome without any explicit or implicit waits at all.

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • Adding line var path = js.ExecuteScript("return document.ourFunction()"); throws the following exception [code]An unhandled exception of type 'System.InvalidOperationException' occurred in WebDriver.dll Additional information: unknown error: document.ourFunction is not a function. However the following line works. var path = js.ExecuteScript("return document.ourFunction"); but it still generated null in Path. I have changed the jsPopup.js function to include return statement. – Abdul Mar 23 '16 at 18:52
  • I'll give this a more detailed try later on. – Andrew Regan Mar 23 '16 at 19:20
  • i think you are right with () comment and probably everything else also because i just figured out that my javascript is not loading at all. So far to me it looks like it is trying to make the file path an extension of the web url when i inspect the web elements in chrome inspector. and hence the javascript is not loaded. and as a result ourFunction() returns no such function. I have added the following lines to check var test = js.ExecuteScript("return typeof(ourFunction)"); which is returning undefined. – Abdul Mar 24 '16 at 18:38