File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
java/src/org/openqa/selenium/support Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ package org .openqa .selenium .support ;
2+
3+ import java .io .IOException ;
4+ import java .net .HttpURLConnection ;
5+ import java .net .URI ;
6+ import java .net .URL ;
7+ import org .openqa .selenium .WebElement ;
8+
9+ public final class BrokenLinkChecker {
10+
11+ private BrokenLinkChecker () {
12+ // Utility class - prevent instantiation
13+ }
14+
15+ public static boolean isBroken (WebElement element ) {
16+ String url = element .getAttribute ("href" );
17+ if (url == null || url .trim ().isEmpty ()) {
18+ return false ;
19+ }
20+ return isBroken (url .trim ());
21+ }
22+
23+ public static boolean isBroken (String linkURL ) {
24+ try {
25+ URL url = URI .create (linkURL ).toURL ();
26+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
27+ connection .setRequestMethod ("HEAD" );
28+ connection .setConnectTimeout (5000 ); // 5 sec timeout
29+ connection .setReadTimeout (5000 );
30+ connection .connect ();
31+
32+ int responseCode = connection .getResponseCode ();
33+ connection .disconnect ();
34+
35+ return responseCode >= 400 ;
36+ } catch (IOException e ) {
37+ System .err .println ("Error checking link: " + linkURL + " → " + e .getMessage ());
38+ return true ;
39+ }
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments