适用场景

快速遍历页面所有链接,找出失效链接或低级错误

实现逻辑

/**
     * 判断页面所有的链接是否健康,判断条件 http status 400~469 500~569 600~669
     *
     * @param url     被测网页地址
     * @param waitFor 每次加载页面的最长等待时间
     * @author quqing
     */
    public void testLinksHealth(String url, long... waitFor) {
        boolean isHealthPage;
        String href;
        String pageSource;
        String hasFind;
        List<String> hrefList = new ArrayList<String>();
        Map<String, String> actualResultMap = new LinkedHashMap<String, String>();
        Map<String, String> expectedResultMap = new LinkedHashMap<String, String>();

        try {
            driver.navigate().to(url);
            Thread.sleep(6000);
            List<WebElement> links = driver.findElements(By.xpath("//a"));
            Log.logInfo("links numbers -> " + links.size());

            for (WebElement link : links) {
                if (null != link.getText() && !"".equals(link.getText())) {
                    href = link.getAttribute("href");
                    if (href.startsWith("http:") || href.startsWith("https:") || href.startsWith("/")) {
                        hrefList.add(href + "!=!" + link.getText());
                        Log.logInfo(href);
                    }
                }
            }

            for (String sUrl : hrefList) {
                driver.get(sUrl.split("!=!")[0]);
                if (null != waitFor && waitFor.length > 0)
                    Thread.sleep(waitFor[0]);
                pageSource = driver.getPageSource();
                hasFind = findSubString(pageSource);
                Log.logInfo(sUrl.split("!=!")[0] + " -> " + sUrl.split("!=!")[1]);
                Log.logInfo("Page contains exception information -> " + hasFind);
                expectedResultMap.put(sUrl.split("!=!")[0], null);
                actualResultMap.put(sUrl.split("!=!")[0], hasFind);
                isHealthPage = (null == hasFind) ? true : false;
                Log.logInfo("Is it a healthy page? -> " + isHealthPage);
            }

            Assert.assertEquals(actualResultMap, expectedResultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查找自定义条件的字符串,判断条件用正则表达式描述
     *
     * @param url     被测网页地址
     * @param waitFor 每次加载页面的最长等待时间
     * @param regx    正则表达式(例如:HTTP Status [456][06][0-9]|(?i)exception)
     * @author quqing
     */
    public void testLinksHealth(String url, String regx, long... waitFor) {
        boolean isHealthPage;
        String href;
        String pageSource;
        String hasFind;
        List<String> hrefList = new ArrayList<String>();
        Map<String, String> actualResultMap = new LinkedHashMap<String, String>();
        Map<String, String> expectedResultMap = new LinkedHashMap<String, String>();

        try {
            driver.navigate().to(url);
            Thread.sleep(6000);
            List<WebElement> links = driver.findElements(By.xpath("//a"));
            Log.logInfo("links numbers -> " + links.size());

            for (WebElement link : links) {
                if (null != link.getText() && !"".equals(link.getText())) {
                    href = link.getAttribute("href");
                    if (href.startsWith("http:") || href.startsWith("https:") || href.startsWith("/")) {
                        hrefList.add(href + "!=!" + link.getText());
                        Log.logInfo(href);
                    }
                }
            }

            for (String sUrl : hrefList) {
                driver.get(sUrl.split("!=!")[0]);
                if (null != waitFor && waitFor.length > 0)
                    Thread.sleep(waitFor[0]);
                pageSource = driver.getPageSource();
                hasFind = findSubString(pageSource, regx);
                Log.logInfo(sUrl.split("!=!")[0] + " -> " + sUrl.split("!=!")[1]);
                Log.logInfo("Page contains exception information -> " + hasFind);
                expectedResultMap.put(sUrl.split("!=!")[0], null);
                actualResultMap.put(sUrl.split("!=!")[0], hasFind);
                isHealthPage = (null == hasFind) ? true : false;
                Log.logInfo("Is it a healthy page? -> " + isHealthPage);
            }

            Assert.assertEquals(actualResultMap, expectedResultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查找自定义条件的字符串,判断条件用正则表达式描述
     *
     * @param find 被查找的字符串
     * @param regx 正则表达式(例如:匹配包含换行符(回车)的任意字符串的正则表达式:[\\s\\S]*? )
     * @return 匹配的字符串
     * @author quqing
     */
    public String findSubString(String find, String regx) {
        String str = null;
        try {
            Pattern pattern = Pattern.compile(regx);
            Matcher matcher = pattern.matcher(find);
            while (matcher.find()) {
                str = matcher.group();
                return str;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 查找预定义条件的字符串,判断条件 http status 400~469 500~569 600~669
     *
     * @param find 被查找的字符串
     * @return 匹配的字符串
     * @author quqing
     */
    public String findSubString(String find) {
        return findSubString(find, "HTTP Status [456][06][0-9]");
    }

测试调用

// 默认判断条件
webOperate.testLinksHealth("http://172.18.16.205:8080/api_manage/");
// 默认判断条件,每次加载页面的等待时间
webOperate.testLinksHealth("http://172.18.16.205:8080/api_manage/",6000);
// 自定义判断条件
webOperate.testLinksHealth("http://172.18.16.205:8080/api_manage/","HTTP Status [456][06][0-9]|(?i)exception");
// 自定义判断条件,每次加载页面的等待时间
webOperate.testLinksHealth("http://172.18.16.205:8080/api_manage/","HTTP Status [456][06][0-9]",6000);

本文系原创,转载请注明出处


↙↙↙阅读原文可查看相关链接,并与作者交流