1

I'm trying to write a Javascript test script. I'm unsure how I can reference and use the JavascriptExecutor in an Javascript file. How do I reference / instantiate it in Javascript? I'm using FF. Any sample code would be appreciated. thx

0

    1 Answer 1

    1

    Just insert your JavaScript code inside the tests, you don't need JavaScriptExecutor for that.

    Actually, JavaScriptExecutor is used to run JavaScript inside the browsers by WebDriver in Java environment as Java is a server side language and do not deal directly with browsers. This is done using the executeScript and executeAsyncScript methods.

    Same thing is implemented in C# using IJavaScriptExecutor.

    Edit:

    To scroll to an ID, you can use one of the below code directly in your tests:

    window.scroll(horizontalOffset, verticalOffset);

    window.location.hash = '#ID';

    document.getElementById('ID').scrollIntoView(true);

    var divPosition = $('#divId').offset(); $('html, body').animate({scrollTop: divPosition.top}, "slow"); 
    8
    • I need the feature to scroll to an ID. That is not visible.
      – Jon
      CommentedJul 22, 2015 at 13:10
    • I think the only way to do that is with the JavaScriptExecutor?
      – Jon
      CommentedJul 22, 2015 at 13:10
    • The only way to do that in Java/C# is JavaScriptExecutor, but in Javascript just write the script in the test itself.
      – Manu
      CommentedJul 22, 2015 at 13:45
    • I get window not defined
      – Jon
      CommentedJul 22, 2015 at 17:36
    • window.scroll(horizontalOffset, verticalOffset);
      – Jon
      CommentedJul 22, 2015 at 17:36

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.