gpt4 book ai didi

java - 有没有办法循环使用断言来查找页面上所有损坏的图像

转载 作者:行者123 更新时间:2023-12-02 03:02:20 24 4
gpt4 key购买 nike

我正在使用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;
}
}

最佳答案

您的代码在第一次失败时停止的原因是因为您使用 Assert 使 resp_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 会编写毫无用处的乱码,因此我将其更改为编写 externalHTML,即 IMG 标记的 HTML。

关于java - 有没有办法循环使用断言来查找页面上所有损坏的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42274067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com