2017-02-16 135 views
0
上的所有破碎图像

我正在使用selenium webdriver + TestNG。如果可能,请帮助我解决以下问题:有没有办法在循环中使用断言来查找页面

搜索页面上所有损坏的图像并在测试失败后在控制台中显示它们(使用声明)。

下面的测试失败,首先被分解图像后发现,我需要测试,以检查所有图像,并显示结果当它失败:

public class BrokenImagesTest3_ { 

@Test 
public static void links() throws IOException, StaleElementReferenceException { 

    System.setProperty("webdriver.chrome.driver", "/C: ..."); 
    WebDriver driver = new ChromeDriver(); 
    driver.manage().window().maximize(); 
    driver.get("https://some url"); 

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

    //Find total Number of links on page and print In console. 
    List<WebElement> total_images = driver.findElements(By.tagName("img")); 
    System.out.println("Total Number of images found on page = " + total_images .size()); 

    //for loop to open all links one by one to check response code. 
    boolean isValid = false; 
    for (int i = 0; i < total_images .size(); i++) { 
     String image = total_images .get(i).getAttribute("src"); 


     if (image != null) { 

      //Call getResponseCode function for each URL to check response code. 
      isValid = getResponseCode(image); 

      //Print message based on value of isValid which Is returned by getResponseCode function. 
      if (isValid) { 
       System.out.println("Valid image:" + image); 
       System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); 
       System.out.println(); 
      } else { 
       System.out.println("Broken image ------> " + image); 
       System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); 
       System.out.println(); 
      } 
     } else { 
      //If <a> tag do not contain href attribute and value then print this message 
      System.out.println("String null"); 
      System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); 
      System.out.println(); 
      continue; 
     } 

    } 
    driver.close(); 
} 

//Function to get response code of link URL. 
//Link URL Is valid If found response code = 200. 
//Link URL Is Invalid If found response code = 404 or 505. 
public static boolean getResponseCode(String chkurl) { 
    boolean validResponse = false; 
    try { 
     //Get response code of image 
     HttpClient client = HttpClientBuilder.create().build(); 
     HttpGet request = new HttpGet(chkurl); 
     HttpResponse response = client.execute(request); 
     int resp_Code = response.getStatusLine().getStatusCode(); 
     System.out.println(resp_Code); 
     Assert.assertEquals(resp_Code, 200); 
     if (resp_Code != 200) { 
      validResponse = false; 
     } else { 
      validResponse = true; 
     } 
    } catch (Exception e) { 

    } 
    return validResponse; 
    } 
} 

回答

2

您的代码在第一次失败时停止的原因是因为您使用的Assertresp_Code等于200. TestNG将停止执行第一个失败的断言。

我会做这个有点不同。您可以使用CSS选择器仅使用"img[src]"查找包含src属性的图像,因此您无需处理该情况。当我查找破碎的图像时,我使用naturalWidth属性。如果图像中断,它将为0。使用这两个块,代码看起来像......

List<WebElement> images = driver.findElements(By.cssSelector("img[src]")); 
System.out.println("Total Number of images found on page = " + images.size()); 
int brokenImagesCount = 0; 
for (WebElement image : images) 
{ 
    if (isImageBroken(image)) 
    { 
     brokenImagesCount++; 
     System.out.println(image.getAttribute("outerHTML")); 
    } 
} 
System.out.println("Count of broken images: " + brokenImagesCount); 
Assert.assertEquals(brokenImagesCount, 0, "Count of broken images is 0"); 

然后添加此功能

public boolean isImageBroken(WebElement image) 
{ 
    return !image.getAttribute("naturalWidth").equals("0"); 
} 

我只写出那些破碎的影像。我更喜欢这种方法,因为它保持了日志清理器。编写image将会写出不会有用的乱码,因此我改变了这种方式来编写IMG标记的HTML的outerHTML。

+0

在这种情况下,我经常将无效映像的src属性附加到字符串构建器,并声明其长度为零,并将构建器的内容用作消息的一部分。这样你就可以在日志中获得更具体的内容。 –

+0

如果你像这样遍历一个'WebElement'的列表,你可能会得到一个陈旧的元素。 –

+0

@PeteKirkham这些无效图像将不包含空的src。 CSS选择器将其过滤掉。这些更多的是不存在的图像或不合适的URL。任何失败都会使其IMG标记的完整HTML被记录下来。 – JeffC

0

assertEquals()抛出AssertionError,不是Exception。如果代码在你的情况下不相同,它将抛出AssertionError,你的测试将停止并且以失败告终。 如果您在catch()中捕获Error而不是Exception,它应该可能按照您的预期工作。

0

作为附录JeffC的,我喜欢收集错误的SRC属性并将其报告为失败,而不是记录到一个单独的文件,是这样的:

List<WebElement> images = driver.findElements(By.cssSelector("img[src]")); 
System.out.println("Total Number of images found on page = " + images.size()); 
StringBuilder brokenImages = new StringBuilder(); 
for (WebElement image : images) 
    if (isImageBroken(image)) 
     brokenImages.append(image.getAttribute("src")).append(";"); 
Assert.assertEquals(brokenImages.getLength(), 0, 
    "the following images failed to load", brokenImages); 

(只有一个答案,因为它更容易用代码解释而不是在注释中)

相关问题