Broken links – The link in an HTML object that enables users to migrate from one web page to another when it is clicked on. It means to navigate between different webpages on the internet. A broken link will often be called a dead link. It is the one that does not work, it does not direct or redirect to the web page it is meant to.
This usually occurs because the website or particular web page is down or does not exist. When one clicks on a broken link, an error message will be displayed. To effectively handle such issues in automation testing, enrolling in a Selenium Course can provide the necessary skills to identify and fix such problems in web applications..
A broken link will exist due to some kind of server error, which in turn causes the corresponding page to malfunction and also not yet be displayed. A valid URL will have 2xx HTTP status code. A broken link, which essentially will have invalid HTTP requests, has 4xx and 5xx status codes.
Why we have to check for broken link in selenium?
On clicking a link, users are directed to an error page. This will obviously contribute to subpar user experience. Links will defeat the purpose of having the website in the first place because users who cannot find the information or service they are looking for. Every link in a website must be tested to make sure that it is functioning as expected. That will give the most websites that have links hundreds of links that are required to make them work. Manual testing of each link would need excessive amounts of time, effort, and resources.
There are common reasons to identify broken links in a selenium web driver:
404 page not found-Here the destination web page will be removed by the owner
400 Bad Request-The server may not processes the HTTP request which is triggered by the link as the URL address requested will be wrong.
Due to the userās firewall settings the browser will not access the destination of the web page.
The link is misspelled. All these are the reasons for the broken link.
How we can identify the broken links in selenium Web driver?
We have to check broken links in selenium, the process is simple. Here the simple web page hyperlinks are implemented using the HTML Anchor(<a>) tag. All the script needs to do is locate every anchor tag will be on a web page that is getting corresponding URLs and run through the links to check if any of them are broken.
By using the following steps to identify broken links in selenium
- Collect all the links present on web page depend on a <a> tag
- Send the HTTP request for each link.
- Verify the HTTP response code.
- Determine if the link will be broken that is based on the HTTP response code.
- Repeat the process for any links will be captured with the first step.
Example for broken link
import java.net.HttpURLConnection;
import java.net.URL;
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.firefox.FirefoxDriver;
public class BrokenLinks {
public static void main(String[] args) throws InterruptedException//throws an exception
{
System.setProperty(“webdriver.gecko.driver”, “C:\\Selenium Environment\\Drivers\\geckodriver.exe”);
WebDriver driver = new FirefoxDriver();
//Maximize the browser
driver.manage().window().maximize();
//Implicit wait for 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//To launch iitworkforce .com
driver.get(“https://www.iitworkforce.com”);
//Wait for 5 seconds
Thread.sleep(5000);
//We are using tagName method to collect the list of items with tagName “a”
//findElements – to search all the elements within the current page. It returns a list of all possible webelements or an empty list if nothing matches
List<WebElement> links = driver.findElements(By.tagName(“a”));
//To print the total number of links
System.out.println(“Total links are “+links.size());
//used for loop to
for(int i=0; i<links.size(); i++) {
WebElement element = links.get(i);
//we use “href” attribute, we could get the url of the requried link
String url=element.getAttribute(“href”);
//calling verifyLink() method here. by passing the parameter as url which we collected in the above link
//we can know detailed functionality of the verifyLink(url) method below
verifyLink(url);
}
}
// here the function verifyLink(String urlLink) verifies any broken links and return the server status.
public static void verifyLink(String urlLink) {
//Sometimes we may face exception “java.net.MalformedURLException”. we can keep code in try catch block to continue the broken link analysis
try {
//we can use URL Class – Create object of the URL Class and pass the urlLink as parameter
URL link = new URL(urlLink);
// Create a connection by using URL object (i.e., link)
HttpURLConnection httpConn =(HttpURLConnection)link.openConnection();
//Set the timeout for 2 seconds
httpConn.setConnectTimeout(2000);
//connect using connect method
httpConn.connect();
// by using getResponseCode() to get the response code.
if(httpConn.getResponseCode()== 200) {
System.out.println(urlLink+” – “+httpConn.getResponseMessage());
}
if(httpConn.getResponseCode()== 404) {
System.out.println(urlLink+” – “+httpConn.getResponseMessage());
}
}
//getResponseCode method will return = IOException ā suppose error occurrs connecting to the server.
catch (Exception e) {
//e.printStackTrace();
}
}
}
Questions
- What are the broken links?
- Why is it important to identify broken link in selenium?
- What are the common causes of inaccessible pages?
- How can automation tools detect these types of issues?
- What role do HTTP response codes play in detecting such issues?
- What are the consequences of having multiple inaccessible resources on a site?
- How can automated scripts assist in identifying issues with URLs?
- What are the best practices for maintaining healthy links across a website?
- How does user experience get affected by issues with resources not being found?
- How can invalid links impact user navigation on a website?
Conclusion
IIn Selenium, managing links is crucial for ensuring that automated tests accurately simulate real user interactions and validate the functionality of a website. links can significantly affect the user experience by redirecting users to error pages, which in turn can impact the website’s credibility and performance. Selenium offers powerful tools for automating link checks across a website, helping to identify and report such issues efficiently. By integrating link detection into Selenium test scripts, developers and testers can ensure that all links are working properly before a website is deployed, thus preventing any disruptions in the user journey.
By automating the testing of links using Selenium, organizations can save considerable time and resources compared to manual testing. It ensures that all URLs are functional and meet quality standards, reducing the likelihood of 4xx or 5xx status code errors that typically signify broken links. This proactive approach to link validation helps maintain the integrity of the website, improves the user experience, and strengthens the overall performance of the web application.
Call to Action
If youāre looking to enhance your Selenium skills and ensure seamless website functionality, H2K Infosys offers comprehensive training programs that cover advanced testing techniques, including automating link validation. Their expert-led courses can guide you through the practical steps of integrating automated tests into your web applications, helping you detect and resolve issues efficiently. Join H2K Infosys today and learn how to optimize your testing process for better website performance.
Donāt let any issues go undetected H2K Infosys provides hands-on training and resources that prepare you for real-world Selenium applications. By mastering these essential testing strategies, youāll ensure that your website performs flawlessly and meets high-quality standards. Enroll now with H2K Infosys and start automating your testing today!