1

I'm using visual studio to write a c# test unit using the selenium webdrive, at some point I've decided to implement a javascript file.

The Javascript file is a little too long to write it directly in the c# code, so I've added an empty Javascript file in Visual studio and copied the code there.

The .js file is visible in the solution explorer but I don't know how to access it from the c# code.

Can you guys provide a sample code on how to access the .js file? thanks.

roni1800
  • 117
  • 2
  • 11
  • 1
    Looks similar to another question, try that answer: http://stackoverflow.com/a/7693645/1018192 – maryum375 Dec 09 '15 at 16:52
  • @maryum375 sorry but I don't think I can use that it. they use the selenium IDE which has a runscript tag and a src= attribute, but in the selenium webdrive there other commands and attributes. – roni1800 Dec 09 '15 at 17:52
  • Maybe you can load the javascript file to a variable as a string and when you call the `ExecuteScript` method pass that variable... – maryum375 Dec 09 '15 at 17:57
  • @maryum375 if you meant with load the .js file to a string, accessing the file via path, then that's exactly what I was thinking about. yet I can't figure out a way on how to load the content of a file via path. if you you were thinking about `string script = "script....";` then I would prefer to avoid that method. – roni1800 Dec 09 '15 at 18:12

1 Answers1

1

Try that:

using System.IO;


IJavaScriptExecutor selenium = _driver as IJavaScriptExecutor;
string path = @"Path to your file";
string jsFileContent = File.ReadAllText(path);
selenium.ExecuteScript (jsFileContent);
maryum375
  • 727
  • 1
  • 10
  • 22