WebDriver只会检查DOM中是否有image 标记,而不能检查这个图片是否不可见或者坏掉。
检查图片是否可见,可通过检查javascript属性naturalWidth是否大于0
@Test
public void Test1() throws Exception{
...
WebElement image=driver.findElement(By.cssSelector("div.img-wrapper>img"));
boolean isVisible=this.IsImageVisible(driver, image);
if(isVisible){
driver.findElement(By.cssSelector("span.img-next > span.img-switch-btn")).click();
driver.switchTo().defaultContent();
System.out.println("----navigate to the next img");
}
...
}
public boolean IsImageVisible(WebDriver driver,WebElement image){
Boolean imageLoaded1 = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image);
if (!imageLoaded1)
{
MyLog.logger.info("----Image is not present.-----");
return false;
}
else
{
MyLog.logger.info("----image is visible.----");
}
return true;
}
主要是参考
http://sqa.stackexchange.com/questions/12912/how-to-check-is-image-is-loaded-or-not-in-selenium
http://watirmelon.com/2012/11/27/checking-an-image-is-actually-visible-using-webdriver/