找出哪些资源没有用Selenium成功加载
Find out which resources are not loaded successfully with Selenium
我必须用Selenium测试一个应用程序。该应用程序包含外部内容,如广告。在我的测试中,我要等待好几次,直到加载文档。这看起来像这样:
private static final String DOCUMENT_READY_STATE_COMPLETE = "complete";
protected void waitUntilDocumentLoaded() {
wait.until(input -> getDocumentReadyState().equals(DOCUMENT_READY_STATE_COMPLETE));
}
private String getDocumentReadyState() {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString();
}
有时,浏览器仍在加载某些资源。据我所知,如果readyState是交互式,而不是完整,我有时可以接受。例如,如果一些广告没有及时加载,因为我的测试完全没有意思。
是否可以以某种方式获得一个包含尚未加载的URL的资源列表然后我可以构建一个断言,检查哪些资源是测试必需的,哪些不是。
我在Linux下使用Selenium Java WebDriver 2.53.1和Firefox 46。
这就是我自己编的。这可以检测未加载的图像。我使用了非常酷的网络服务http://www.deelay.me和https://placekitten.com.所以至少对于图像,我有一些东西。
<html>
<body>
<img src="http://deelay.me/1000/https://placekitten.com/g/500/500"/>
<img src="http://deelay.me/10000/https://placekitten.com/g/500/501"/>
<script>
function isImageOk(img) {
//Function taken from http://stackoverflow.com/a/1977898/337621
// During the onload event, IE correctly identifies any images that
// weren’t downloaded as not complete. Others should too. Gecko-based
// browsers act like NS4 in that they report this incorrectly.
if (!img.complete) {
return false;
}
// However, they do have two very useful properties: naturalWidth and
// naturalHeight. These give the true size of the image. If it failed
// to load, either of these should be zero.
if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) {
return false;
}
// No other way of checking: assume it’s ok.
return true;
}
function checkState(){
var documentState = document.readyState;
if ( documentState != "complete") {
console.log("Document is still having readystate " + documentState + ". Pending images: " );
var images = document.getElementsByTagName("img");
for(i = 0;i < images.length; i++)
{
if ( !isImageOk( images[i] ) ) {
console.log("Image URL: " + images[i].src );
}
}
setTimeout(checkState,500);
}
else {
console.log("Document is loaded successfully.");
}
}
checkState();
</script>
</body>
</html>
控制台输出:
Document is still having readystate loading. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/1000/https://placekitten.com/g/500/500
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is still having readystate interactive. Pending images:
Image URL: http://deelay.me/10000/https://placekitten.com/g/500/501
Document is loaded successfully.
我可以分享3种方法来检查元素是否存在。希望通过使用这种方法,你可以得到平息。
方法#1:
/**
* Checks if is element present or not.
*
* @param element
* @return true, if is element present, otherwise return false.
* @throws Exception
*/
public boolean isElementPresent(WebElement element) throws Exception {
try {
waitForElement(element, Integer.valueOf(PropertyLoader
.loadProperty("implicit_timeout_second")));
return true;
} catch (NoSuchElementException e) {
return false;
}
}
方法#2:
/**
* waits for element to be present.
*
* @param element
* @param timeOut
* The timeout in seconds when an expectation is called
* @return
*/
public WebElement waitForElement(WebElement element, int timeOut) {
WebDriverWait wait = new WebDriverWait(driverW, timeOut);
wait.until(ExpectedConditions.elementToBeClickable(element));
return element;
}
方法#3:
/**
* Waits for the element to be present BY.
*
* @param dinamic
* element
* @return true, if is element present, otherwise return false.
* @throws Exception
*/
protected boolean waitForDynamicElementPresent(By by) throws IOException {
try {
WebDriverWait wait = new WebDriverWait(driverW,
Integer.valueOf(PropertyLoader
.loadProperty("implicit_call_timeout_second")));
wait.until(ExpectedConditions.elementToBeClickable(by));
return true;
} catch (NoSuchElementException e) {
return false;
}
}
- 等待出现特定元素
- 一种解决方法是使用browsermob代理或类似的smthn,在browsermob的情况下,有一种方法可以让你等待网络流量停止,并等待一定的时间,以确保浏览器不会进行任何额外的调用
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- WebDriverException:tinyMCE未在selenium Web driver java中定义
- python-selenium-点击上升链接
- Selenium Web驱动程序和javascript
- 门户网站:当地时间有多有用
- Selenium WebDriver and JavaScript change
- Selenium驱动程序不反映单击()后的页面更改
- 使用ScriptObjectMirror将数组结果从JavaScript转换为有用的东西
- Javascript中的模块模式和揭示模块模式是否仅在创建API时有用;s
- 带有CSS的数据属性就没那么有用了
- Selenium无法在浏览器DOM中定位元素
- 无法使用Selenium创建本地windows目录结构
- Xpath对Selenium Web驱动程序无效”;Xpath未找到”;
- 运行Selenium Webdriver Test时,不会出现警报
- modernizr.js还有用吗
- Selenium RemoteWebDriver中的批处理/原子操作
- 比较Selenium IDE中的日期
- 在selenium python中执行脚本
- 在selenium中加载一个包含有用测试函数的外部js文件
- 找出哪些资源没有用Selenium成功加载