
Nobody wants that his/her application is having even a single broken link. Now again a question arises that,
Then What should we do about that? Do we need to click every single link and check if its working or not? Definitely no one will do that tedious thing. As a result we’ll not be able to complete our deadlines.
In Today’s world every application have hundreds of links and will not be possible at all to check each and every single link by clicking one by one.
Can we check for the broken links?
Here you go with the solution that is SELENIUM and JAVA! Yes you read it right, by using selenium and java you can get all the broken links in just a few steps. Now let’s see how can we achieve that!
How to check the Broken Links and images with Selenium and JAVA?
For checking the broken links, you will need to follow the below mentioned steps.
- Collect all the links in the web page based on <a> tag.
- Send HTTP request for the link and read HTTP response code.
- Find out whether the link is valid or broken based on HTTP response code.
- Repeat this for all the links captured.
package practiceSelenium;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrokenLinks {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Programming tools\\drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize();
driver.get("http:xyz/");
driver.navigate().refresh();
Thread.sleep(2000);
List urlList = driver.findElements(By.tagName("a"));
System.out.println("Total links are " + urlList.size());
Iterator it = urlList.iterator();
while (it.hasNext()) {
for (int i = 0; i < urlList.size(); i++) {
WebElement link = it.next();
if (link != null) {
String url = link.getAttribute("href");
checkConnectivity(url);
}
}
}
}
public static void checkConnectivity(String linkUrl) {
try {
URL url = new URL(linkUrl);
HttpURLConnection httpURLConnect = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(false);
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
switch (httpURLConnect.getResponseCode()) {
case 200:
System.out.println(linkUrl + " - " + httpURLConnect.getResponseMessage());
default:
System.out.println(linkUrl + " - " + httpURLConnect.getResponseMessage() + " - " + HttpURLConnection.HTTP_NOT_FOUND);
httpURLConnect.disconnect();
}
} catch (Exception e) {
System.out.println("CODE STUCK SOMEWHERE or" + linkUrl + " is malformer");
e.printStackTrace();
}
}
}
Step 1: Collect all links on the webpage
Firstly, identify all links in a webpage and store them in a List
List links = driver.findElements(By.tagName("a"));
Secondly, get Iterator to traverse through the List.
Iterator<WebElement> it = urlList.iterator();
Step 2: Identifying and Validating URL
In this part, we will get the href URLs one by once and check if they have valid links or not and parse URLs to our checkConnectivity method.
while (it.hasNext()) {
for (int i = 0; i < urlList.size(); i++) {
WebElement link = it.next();
if (link != null) {
string url = link.getAttribute("href");
checkConnectivity(url);
}
Step 3 Setup a method to create connection and verify the links
Firstly, create an object of URL class and parse href urls as an argument
URL url = new URL(linkUrl);
then, we have HttpURLConnection class which has methods to send HTTP request and capture response code. So, output of openConnection() method (HttpURLConnection) is type casted to HttpURLConnection.
HttpURLConnection httpURLConnect = (HttpURLConnection) url.openConnection();
After that, handle the Too many redirections exception
HttpURLConnection.setFollowRedirects(false);
At this point, set the connection timouts and invoke the connection
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
Step 4: Validating Links
Firstly, define a switch case and assert the response code
statusCode= conn.getResponseCode();switch (httpURLConnect.getResponseCode()) {
case 200:
System.out.println(linkUrl + " - " +
httpURLConnect.getResponseMessage());
default:
System.out.println(linkUrl + " - " +
httpURLConnect.getResponseMessage() + " - "
+
HttpURLConnection.HTTP_NOT_FOUND);
}
After that, close the connection
httpURLConnect.disconnect();
At the last, handle the exceptions
catch (Exception e) {
System.out.println("CODE STUCK SOMEWHERE or" + linkUrl + " is malformer");
e.printStackTrace();
}
So after following the above steps, we can obtain all links from web page and print whether links are valid or broken.
Hope this blog will help you in future whenever you’re checking for Broken links using Selenium and Java
For further reference :
Lastly, do it
He’s a very nice guy. He took us out to dinner yesterday, for instance
beacause of this, we can move ahead
As a result of
Now we can, move further
As a result, we met
Finally, did this
As a result then we can
Above All , do this
Lastly, do this
As a result