Sunday, May 13, 2018

How to verify that Image has been loaded successfully or not with Selenium WebDriver?

How to verify that Image has been loaded successfully or not with Selenium WebDriver?

Solution:-

Today, i will share the way how we can verify that Image has been loaded successfully or not with Selenium WebDriver.

Here, class name is "Testing".

Lets first create the instance of WebDriver :

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();
Open the Webpage:
driver.get("http://www.seleniumstack.in/p/images.html");
Next, create a function "checkImageLoaded" to verify Image Load


public void checkImageLoaded(WebElement images) throws Exception{
 
HttpClient client = HttpClientBuilder.create().build();
 
HttpGet request = new HttpGet(images.getAttribute("src"));
 
HttpResponse response = client.execute(request);
  
 
if(response.getStatusLine().getStatusCode()!=200){
  
      System.out.println(images.getAttribute("alt"));

 }
}
Now, create the List of Image Elements of WebPage in main method and call "checkImageLoaded" function.

public static void main(String[] args) throws InterruptedException {

  Testing test = new Testing();
  
    WebDriver driver;

    System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");

    driver = new ChromeDriver();
  
  List images = driver.findElements(By.tagName("img"));
  
  try {
   for(WebElement ImageElement : images){
   if(ImageElement!=null){
   test.checkImageLoaded(ImageElement);
    }
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
That's all. Hope this helps you.

Thanks

No comments:

Post a Comment

Questions Asked in Interview for Manual Testing Engineer?

1. What is Boundary Value Analysis and Equivalence Partitioning ? 2. What is Responsive web design ? 3. What is Globalization Te...