【问题标题】:Must validate images present on the page必须验证页面上存在的图像
【发布时间】:2021-03-27 09:06:03
【问题描述】:

我有一个特定的 URL 示例 https://www.gnostice.com/nl_article.asp?id=207&t=How_To_Read_A_PDF_File_From_A_URL_In_Java(URL 中的图像不在任何文件夹位置)

要验证的图像规范要求:

  1. 5 英寸宽 = 672
  2. 7 英寸宽 = 840

我有 27 个可用的

  1. 5 英寸宽的图像数量 = 672
  2. 7 英寸宽 = 840 的图像数
  3. 有没有其他尺寸的图片

能否请您告诉我如何使用 java selenium 验证它... 谢谢

【问题讨论】:

    标签: java image selenium testing automation


    【解决方案1】:
    package selenium;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class Tifoso {
        
        // in my case %USERPROFILE%\eclipse-workspace\Selenium
        public static String userDir = System.getProperty("user.dir");
        
        // chromedriver.exe location
        public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";  
        
        public static WebDriver driver;
        
        // standard chromedriver initialization
        public static WebDriver startChromeDriver() {
            System.setProperty("webdriver.chrome.driver", chromedriverPath);
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.addArguments("--ignore-certificate-errors");        
            driver = new ChromeDriver(options);
            
            // setting 30 seconds wait to load pages
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            
            driver.manage().window().maximize();
            return driver;
        }
        
        public static void main(String[] args) {
            
            WebDriver driver = startChromeDriver();
            String url = "https://www.gnostice.com/nl_article.asp?id=207&t=How_To_Read_A_PDF_File_From_A_URL_In_Java";
            driver.get(url);
            
            // empty lists
            List<WebElement> fiveInchWidthImages = new ArrayList<WebElement>();
            List<WebElement> sevenInchWidthImages = new ArrayList<WebElement>();
            List<WebElement> otherInchWidthImages = new ArrayList<WebElement>();
            
            // getting all images
            List<WebElement> allImages = driver.findElements(By.tagName("img"));
            for (WebElement image: allImages) {
                // getting image width
                int width = image.getRect().getWidth();
                if (width == 672) {
                    fiveInchWidthImages.add(image);
                }
                else if (width == 820) {
                    sevenInchWidthImages.add(image);
                }
                else {
                    otherInchWidthImages.add(image);
                }
            }
            
            System.out.println("Number of images with width 672 is " + fiveInchWidthImages.size());
            System.out.println("Number of images with width 820 is " + sevenInchWidthImages.size());
            System.out.println("Number of images with other width is " + otherInchWidthImages.size());
            
            driver.close();
        }
    
    }
    

    输出:

    Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 4826
    Only local connections are allowed.
    Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
    ChromeDriver was started successfully.
    Pro 17, 2020 8:25:10 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Number of images with width 672 is 0
    Number of images with width 820 is 0
    Number of images with other width is 27
    

    【讨论】:

    • 非常感谢!
    • 请将答案标记为有用。谢谢。
    猜你喜欢
    • 2012-04-17
    • 2017-11-11
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多