The logical progression is of course to write your own JavaScript libraries that you can import instead of sending everything over as a string. Alternatively, maybe you would just like to import an existing library.
Let's write some code that allows you to import a JavaScript library of your choice. It's not particularly complex JavaScript. All that we are going to do is create a new <script> element in a page and then load our library into it. First of all, let's make sure we have access to a driver object in this class:
private RemoteWebDriver driver;
@BeforeMethod
public void setup() {
driver = getDriver();
}
Then we will need to add some code that will let us inject a <script> element:
private void injectScript(String scriptURL) {
driver.executeScript("function injectScript(url) {\n" +
...