英文:
Validate Images are loaded properly and visible on website using Selenium
问题
验证场景如下:
在进行网站检查时,使用URL响应,我们会得到200 OK作为响应。有时候我们会观察到,由于某些CSS文件或JS文件加载错误,我们无法看到网站上的图像加载情况。
我需要使用Selenium和Java在给定的场景中进行验证。
英文:
Validation Scenario as follows :
During website check using URL response we get 200 OK as response. There are instances observed when due to some CSS file or JS file loading error we are not able to see images on website getting loaded.
I need to do validation in given scenario using Selenium and Java.
答案1
得分: 1
以下是翻译好的部分:
List<String> imageAlts = {"imageAlt1", "imageAlt2"};
List<WebElement> images = driver.findElements(By.tagName("img"));
for (WebElement image: images) {
String imageAlt = image.getAttribute("alt");
if (imageAlts.contains(imageAlt)) {
int x = image.getLocation().getX();
int y = image.getLocation().getY();
int w = image.getRect().getWidth();
int h = image.getRect().getHeight();
if (x > 0 && y > 0 && w > 0 && h > 0) {
System.out.println("image with alt " + imageAlt + " is visible");
}
else {
System.out.println("image with alt " + imageAlt + " is NOT visible");
}
}
else {
System.out.println("image with alt " + imageAlt + " NOT found");
}
}
英文:
What about this:
List<String> imageAlts = {"imageAlt1", "imageAlt2"};
List<WebElement> images = driver.findElements(By.tagName("img");
for (WebElement image: images) {
String imageAlt = image.getAttribute("alt");
if (imageAlts.contains(imageAlt)) {
int x = image.getLocation().getX();
int y = image.getLocation().getY();
int w = image.getRect().getWidth();
int h = image.getRect().getHeight();
if (x > 0 && y > 0 && w > 0 && h > 0) {
System.out.println("image with alt " + imageAlt + " is visible");
}
else {
System.out.println("image with alt " + imageAlt + " is NOT visible");
}
}
else {
System.out.println("image with alt " + imageAlt + " NOT found");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论