使用FirefoxDriver编写测试时
我发现由于javascript和css被执行,页面的加载速度非常慢.无论如何要禁用它吗?有可能甚至安装Noscript插件到配置文件?
另外,sendKeys()实际上输出了文本.但是,对于长文本来说这很慢,无论如何要立即在输入框中键入所有字符串?
Ser*_*rov 17
您可以在FirefoxProfile
以下位置禁用javaScript :
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);
Run Code Online (Sandbox Code Playgroud)
我不认为有一种方法可以禁用CSS,这不是你应该做的 - 这可能会破坏你的web应用程序,禁用JavaScript也可能会这样做.
没有办法直接设置文本字段的值 - WebDriver旨在模拟真实用户"驱动"浏览器 - 这就是为什么只有sendKeys.
但是,您可以通过JavaScript调用设置元素的值(当然,如果您不禁用它).这对于长时间测试来说速度更快,但这不是用户交互的模拟,因此可能不会触发某些验证,因此请谨慎使用:
private void setValue(WebElement element, String value) {
((JavascriptExecutor)driver).executeScript("arguments[0].value = arguments[1]", element, value);
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
WebElement inputField = driver.findElement(By...);
setValue(inputField, "The long long long long long long long text......");
Run Code Online (Sandbox Code Playgroud)
小智 13
另请参阅使用Python在Selenium WebDriver测试中加载图像以及在Firefox上渲染CSS
隐藏CSS和图像:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.stylesheet", 2);
profile.setPreference("permissions.default.image", 2);
FirefoxDriver browser = new FirefoxDriver(profile);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12709 次 |
最近记录: |