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
1 Answer
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");
- The only way to do that in Java/C# is JavaScriptExecutor, but in Javascript just write the script in the test itself.– ManuCommentedJul 22, 2015 at 13:45