Thursday, 24 December 2015

Find Broken Links / Images on web page (404 error)

package fb_google;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Broken_link_404_final_working
{
//    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://watirmelon.com/2012/11/27/checking-an-image-is-actually-visible-using-webdriver/");
       //Set  timeout
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
    List<String> href=new ArrayList<String>();
    List<WebElement> links=driver.findElements(By.tagName("a"));
    System.out.println("printing hyper links count:" + links.size());
 
    for(int i=0;i<=links.size();i++)
    {
    //System.out.println("printin links names :" +links.get(i).getText());
    System.out.println("printin links names :" +links.get(i).getAttribute("href"));
   
    boolean isValid = getResponceCode(links.get(i).getAttribute("href"));
  if(isValid)
  {
 System.out.println("Valid Links are : " + links.get(i).getAttribute("href"));

  }
  else
  {
 System.out.println("Invalid links are : " + links.get(i).getAttribute("href"));
  }
 
 
    }

 
 
    driver.quit();
     
    }
    public static boolean getResponceCode(String urlString)
    {
    boolean isValid=false;
    try
    {
    URL u=new URL(urlString);
    HttpURLConnection h=(HttpURLConnection) u.openConnection();
    h.setRequestMethod("GET");
    h.connect();
    System.out.println(h.getResponseCode());
    if(h.getResponseCode() >=404)
    {
    isValid=true;
    }
    }
    catch(Exception e)
    {
   
    }
    return isValid;
    }
 
}
++++++++++++++++++++++++++++++++++++++
package fb_google;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Broken_Links_1 {

private WebDriver driver;
private int invalidLinksCount;

@BeforeTest
public void setUp() {

driver = new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
}

@Test
public void validateInvalidLinks() {

try {
invalidLinksCount = 0;


List<String> hrefs = new ArrayList<String>();
List<WebElement> links =driver.findElements(By.tagName("a"));
System.out.println("Total links are : "+links.size());

for(WebElement ele:links)
{
hrefs.add(ele.getAttribute("href"));
}
for ( String href : hrefs ) {
   driver.get(href);
   System.out.println("opening link"+driver.getCurrentUrl());
 //  driver.navigate().back();

}




}
catch (Exception e) {
// TODO: handle exception
}

}

}
+++++++++++++++++ Find only Broken images 404++++++++++

/////find broken links
package fb_google;
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 Broken_404 {

    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://watirmelon.com/2012/11/27/checking-an-image-is-actually-visible-using-webdriver/");
       //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();
  }
}
++++++++++++++++++++++++++


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.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Broken_Links_On_Webpage {
    public static void main(String[] args) throws InterruptedException, IOException {
       
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("http://watirmelon.com/2012/11/27/checking-an-image-is-actually-visible-using-webdriver/");
        List<WebElement> elements = driver.findElements(By.tagName("a"));
        System.out.println(elements.size());
       
        for(WebElement linkElement: elements){
            String link = linkElement.getAttribute("href");
            System.out.println(link);
            if(link!=null){
                 if(!isLink(link)){
                     continue;
                 }
            }
            verifyLinkActive(link);
        }
        driver.quit();
    }
    public static boolean isLink(String link){
       
        return link.contains("http://") ;
    }
    public static void verifyLinkActive(String linkUrl) throws IOException{
        try {
            URL url = new URL(linkUrl);
            HttpURLConnection httpURLConnect = (HttpURLConnection)url.openConnection();
            httpURLConnect.setConnectTimeout(3000);
            httpURLConnect.connect();
            if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND){
                System.out.println(linkUrl+"-"+httpURLConnect.getResponseMessage()+"-"+
            HttpURLConnection.HTTP_NOT_FOUND);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

   
}

No comments:

Post a Comment