Skip to content

Commit a330353

Browse files
committed
Added BrokenLinkChecker utility to verify broken links
1 parent bea8f71 commit a330353

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)