Can I use JavaScript libraries?

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) { " +
" var script =
document.createElement('script');
" +
" script.src = url; " +
" var head =
document.getElementsByTagName('head')[0];
" +
" head.appendChild(script); " +
"} " +
" " +
"var scriptURL = arguments[0]; " +
"injectScript(scriptURL);"
, scriptURL);
}

We have again set arguments[0] to a variable before injecting it for clarity, but you can inline this part if you want to. All that remains now is to inject this into a page and check whether it works. Let's write a test!

We are going to use this function to inject jQuery into the Google website. The first thing that we need to do is write a method that can tell us whether jQuery has been loaded or not, as follows:

private Boolean isjQueryLoaded() {
return (Boolean) driver.executeScript("return typeof jQuery
!= 'undefined';"
);
}

We are also going to write an ExpectedCondition to help us work out when the script has been successfully injected (you will notice that the JavaScript we use is identical to the preceding one):

private static ExpectedCondition<Boolean> jQueryHasLoaded() {
return webDriver -> {
JavascriptExecutor js = (JavascriptExecutor) webDriver;
return Boolean.valueOf(js.executeScript("return typeof
jQuery !=
'undefined';"
).toString());
};
}

Now you will see that the expected condition is a little bit more complex than you may have expected. This is because this expected condition is going to be plugged into the .until() method of a WebDriverWait object. Unfortunately, WebDriverWait objects are instantiated with a WebDriver object, not a RemoteWebDriver object. If you remember, we talked about this at the start of the chapter. We need to cast the WebDriver object into something that has the .executeScript() method. Next we need to put all of this together in a test, as follows:

@Test
public void injectjQueryIntoGoogle() {
WebDriverWait wait = new WebDriverWait(driver, 15, 100);

driver.get("http://www.google.com");

assertThat(isjQueryLoaded()).isEqualTo(false);

injectScript("https://code.jquery.com/jquery-latest.min.js");
wait.until(jQueryHasLoaded());

assertThat(isjQueryLoaded()).isEqualTo(true);
}

It's a very simple test. We loaded the Google website. Then, we checked whether jQuery existed. Once we were sure that it didn't exist, we injected jQuery into the page. Finally, we waited for jQuery to load, and then for completeness we have an assertion to be absolutely positive that jQuery now exists.

We have used jQuery in our example, but you don't have to use jQuery. You can inject any script that you desire.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.13.255