FirefoxDriver:如何禁用javascript,css并立即生成sendKeys类型? [英] FirefoxDriver: how to disable javascript,css and make sendKeys type instantly?

查看:78
本文介绍了FirefoxDriver:如何禁用javascript,css并立即生成sendKeys类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用FirefoxDriver编写测试时,

While using FirefoxDriver to write tests,

我发现由于javascript和css正在执行,页面加载速度非常慢。无论如何要禁用它吗?甚至可以将Noscript插件安装到个人资料中?

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 ?

另外,sendKeys()实际上会输出文本。但是,对于长文本来说这很慢,无论如何要立即在输入框中输入所有字符串?

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 ?

推荐答案

你可以禁用中的javaScript

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

我认为没有办法禁用CSS,这不是你应该做的 - 这可能打破你的web应用程序,禁用JavaScript也可以这样做。

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.

没有办法直接设置文本字段的值 - WebDriver旨在模拟真实用户驱动浏览器 - 这就是为什么只有sendKeys。

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.

但是你可以通过JavaScript调用设置元素的值(当然你不会禁用它)。这对于长时间测试来说更快,但这不是用户交互的模拟,因此可能不会触发某些验证,因此请谨慎使用:

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);
}

并使用它:

WebElement inputField = driver.findElement(By...);
setValue(inputField, "The long long long long long long long text......");

这篇关于FirefoxDriver:如何禁用javascript,css并立即生成sendKeys类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆