mardi 20 octobre 2020

Please help me with JavaScript when we use Selenium , want ot test all broken links from full website

please help me with JavaScript.

So far as now , I can use selenium to test broken links from webpage ,like from homepage etc ,but it test only the broken links on that page, and now I need to know how can I test all broken links from full website . Is there anyway with selenium to test it ? Please. Really need it now .

PS: here is my code to find broken links on one page , so what else can I add on this script to test all broken links from entire website ? Or maybe some nice of you have better script to test it ? Please help me out ..

List<WebElement> links = driver.findElements(By.tagName("a"));

System.out.println(links.size());


for(int i=0;i<links.size();i++)
{   
WebElement element=links.get(i);
String url=element.getAttribute("href");

URL link=new URL(url);

URLConnection httpConn =link.openConnection();

Thread.sleep(2000);

httpConn.connect();

int rescode=((HttpURLConnection) httpConn).getResponseCode();

if(rescode>=400)
{
    System.out.println(url +" - "+" is broken link");
}

else
{
    
    System.out.println(url +" - "+" is valid link");
    
}}  

}
    


  public static void main(String[] args) throws Throwable { 
    
   testbrokenlinks obj= new  testbrokenlinks();
    obj.lanuchBrower();
    
    // TODO Auto-generated method stub

  }

Aucun commentaire:

Enregistrer un commentaire