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.

4
  • 1
    Looks similar to another question, try that answer: stackoverflow.com/a/7693645/1018192 Commented Dec 9, 2015 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. Commented Dec 9, 2015 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... Commented Dec 9, 2015 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. Commented Dec 9, 2015 at 18:12

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I think I need a using namespace, right? which one should I use?
hey thanks a lot and don't bother about my first comment. the code works although it needs some cleanup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.