Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirefoxDriver: how to disable javascript,css and make sendKeys type instantly?

Tags:

java

webdriver

While using FirefoxDriver to write tests,

I discovered loading of pages are really slow because of javascript and css being executed. IS there anyway to disable this ? possible to even install Noscript plugin to profile ?

additionally, sendKeys(), actually types out the text. however, this is quite slow for long text, anyway to instantly type all the string int othe input box ?

like image 990
KJW Avatar asked Aug 19 '10 21:08

KJW


2 Answers

You can disable javaScript in FirefoxProfile:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);

I do not think that there's a way to disable CSS and this not what you should do - this may break your web application, and disabling JavaScript may do this too.

There's no way to set the value of the text field directly - WebDriver is designed to simulate the real user "driving" the browser - that's why there's only sendKeys.

However you can set the value of the element via JavaScript call (if you will not disable it, of course). This is faster for the long test, but this is not the emulation of the user interaction so some validations may not be triggered, so use with caution:

private void setValue(WebElement element, String value) {
    ((JavascriptExecutor)driver).executeScript("arguments[0].value = arguments[1]", element, value);
}

and use it:

WebElement inputField = driver.findElement(By...);
setValue(inputField, "The long long long long long long long text......");
like image 196
Sergii Pozharov Avatar answered Oct 19 '22 23:10

Sergii Pozharov


See also Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python

To hide CSS and images:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.stylesheet", 2);
profile.setPreference("permissions.default.image", 2);
FirefoxDriver browser = new FirefoxDriver(profile);
like image 43
marie Avatar answered Oct 20 '22 00:10

marie