How to find out broken links in Selenium Webdriver using java?.

More HTTP Status Code

 import java.io.IOException;  
 import java.net.HttpURLConnection;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.util.List;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.*;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 public class Brokenlinks {  
   private static int statusCode;  
   public static void main(String[] args) throws IOException {  
     // Initialize web driver   
     WebDriver driver = new FirefoxDriver();  
     //Maximize browser window  
     driver.manage().window().maximize();  
     //Go to URL   
     driver.get("http://www.google.com");  
     //Set timeout   
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
 // Get all links web driver  
  List<WebElement> links = driver.findElements(By.tagName("a"));  
     for (int i = 0; i < links.size(); i++) {  
 //remove null and empty links  
 if (!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))) {  
  if (links.get(i).getAttribute("href").contains("http")) {  
  // Find HTTP Status-Code  
 statusCode= getResponseCode(links.get(i).getAttribute("href").trim());  
 // Check broken link  
 if (statusCode== 404) {  
 System.out.println("Broken of Link# "+i+" "+links.get(i).getAttribute("href"));  
           }  
         }  
       }  
     }  
     driver.close();  
   }  
 public static int getResponseCode(String urlString) throws MalformedURLException, IOException {      
     URL u = new URL(urlString);  
     HttpURLConnection huc = (HttpURLConnection) u.openConnection();  
     huc.setRequestMethod("GET");  
     huc.connect();  
     return huc.getResponseCode();  
  }  
 }  



11 comments:

  1. Thanks !! This is what i was finding

    ReplyDelete
  2. Hi Hiro san,

    Line no 24 showing error "webelement cannot be resolved to a type" please help

    ReplyDelete
    Replies
    1. use 'WebElement' instand of 'webelement' in Line no 24

      Delete
    2. sir i am finding problem in List that The type List is not generic; it cannot be parameterized with arguments

      Delete
  3. Is there a way to test for authentication. Currently is shows broken link when encountered with authentication. Is it possible?

    ReplyDelete
    Replies
    1. Hi Puventhira Mannen,
      What type authentication, you face problem to find broken link?. Do your mean browser authentication?.

      Delete
  4. Yea...browser authentication. For an instance, if I'm testing mail.google.com the results shown is from google.com due to authentication issue.

    ReplyDelete
    Replies
    1. For browser authentication, You can follow url: http://hiromia.blogspot.com/2015/01/how-to-handle-browser-authentication-in.html . If server can't allowed to access any url or file that can possible to check. Please write status code is 401 in if condition. For more status code please check below url http://download.java.net/jdk7/archive/b123/docs/api/java/net/HttpURLConnection.html

      Delete
  5. This tool can be used to locate broken links on website to find the flaws and rectifying it by just simply entering the URL.It is vary useful check on
    broken link finder

    ReplyDelete
  6. Hi,

    I am trying the same. But after getting the first link, I am getting connection timeout error.

    Unstructured data solutions for eDiscovery, compliance, and records management - ZL Technologies
    http://www.zlti.com/#genesis-nav-primary
    FAILED: f
    java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at one.LinksInPage.f(LinksInPage.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)


    ===============================================


    I have added " huc.setConnectTimeout(10000);" also before connection , then also no luck.

    ReplyDelete