How to assert image present in selenium python web driver? How to assert image present in selenium python web driver? selenium selenium

How to assert image present in selenium python web driver?


I can't help with python, but can give a general suggestion and Java code for those who are looking to do something similar in Java. My approach would be

  1. Get the src attribute of the image and make a quick HTTP GETcall and make sure you get 200 OK response.
  2. Once thats done, you could check the naturalWidthattribute of the WebElement using Javascript. If its more than 0, its rendered fine. However this works differently in IE. IE has .complete attribute. It will be true if the image is rendered (Reference here)

Here is sample Java code,

@Test   public void test()   {      WebDriver driver = new FirefoxDriver();      String url = "http://www.espncricinfo.com/";      driver.get(url);      WebElement img = driver.findElement(By.cssSelector("a[title='ESPN Cricinfo']>img"));      String src = img.getAttribute("src");      Client client = Client.create();      WebResource resource = client.resource(src);      assertThat("Response code is not 200 OK", resource.get(ClientResponse.class).getStatus(), equalTo(200));      assertThat("Image is not rendered correctly", isImageVisible(driver, img),equalTo(true));      driver.quit();   }   public boolean isImageVisible(WebDriver driver, WebElement image)   {      Boolean result = null;      if (driver instanceof InternetExplorerDriver || ((RemoteWebDriver) driver).getCapabilities().getBrowserName().equals("internet explorer"))      {         result = (Boolean) ((JavascriptExecutor) driver).executeScript("return arguments[0].complete;", image);      }      else      { //other browser types use diff method to check         result = (Boolean) ((JavascriptExecutor) driver).executeScript("return (typeof arguments[0].naturalWidth!=\"undefined\" && arguments[0].naturalWidth>0);", image);      }      return result.booleanValue();   }}


There's probably a better way to do this but I decided to retrieve all the image tags.

all_images = self.driver.execute_script("return document.getElementsByTagName('img')")for image in range(0, len(all_images)):    print self.driver.execute_script("return document.getElementsByTagName('img')[{}].src".format(image))


Left out driver setup but this should work once you add yours in. Partial credit to Nilesh for pointing me in right direction with his Java example.

import requestsimport unittestfrom selenium import webdriverfrom selenium.webdriver.common.by import Bydef setUp(self):    self.verificationErrors = []def test_images_for_200_response(self):    driver.get('http:example.com')    example_images = driver.find_elements(By.TAG_NAME, 'img')    for image in example_images:        current_link = image.get_attribute("src")        r = requests.get(current_link)        try: self.assertEqual(r.status_code, 200)        except AssertionError, e: self.verificationErrors.append(current_link + ' delivered response code of ' + r.status_code)def tearDown(self):    driver.quit()    self.assertEqual([], self.verificationErrors)