Hello friends,
In this article we will learn that How in Find Broken links in selenium Java
First of all we will store all the Links in one place then by using HttpURLConnection class, we will check the response code of each link.
If response code is OK Then links are goods otherwise links are broken.
Here is the video which elaborate that How in Find Broken links in selenium Java
Below code will verify all the links of the webpage.
When this code will start its execution then it will ask you the URL.
Enter the valid URL & after that this will give you all the links details.
package links;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Scanner;
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.BeforeTest;
import org.testng.annotations.Test;
public class VerifyLink
{
WebDriver driver;
List yourlink;
String userurl;
@SuppressWarnings("resource")
@BeforeTest
public void f()
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter Your website URL to test including http");
String userurl = obj.next();
driver = new FirefoxDriver();
driver.navigate().to(userurl);
driver.manage().window().maximize();
List yourlink = driver.findElements(By.tagName("a"));
System.out.println("Total links are :" + yourlink.size());
}
@Test
public void g() throws Exception
{
for(int i = 0; i<yourlink.size(); i++)
{
WebElement element = yourlink.get(i);
String alllinks = element.getAttribute("href");
try
{
h(alllinks);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
public void h(String alllinks) throws Exception
{
try
{
URL testlink = new URL(alllinks);
HttpURLConnection httpurlconnect = (HttpURLConnection)testlink.openConnection();
httpurlconnect.setConnectTimeout(4000);
httpurlconnect.connect();
if(httpurlconnect.getResponseCode()==200)
{
System.out.println(testlink +"--"+httpurlconnect.getResponseMessage());
}
if(httpurlconnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)
{
System.out.println(testlink + "--" + httpurlconnect.getResponseMessage() + "--" +HttpURLConnection.HTTP_NOT_FOUND);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
String userurl;
@SuppressWarnings("resource")
@BeforeTest
public void f()
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter Your website URL to test including http");
String userurl = obj.next();
driver = new FirefoxDriver();
driver.navigate().to(userurl);
driver.manage().window().maximize();
List
System.out.println("Total links are :" + yourlink.size());
}
@Test
public void g() throws Exception
{
for(int i = 0; i<yourlink.size(); i++)
{
WebElement element = yourlink.get(i);
String alllinks = element.getAttribute("href");
try
{
h(alllinks);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
public void h(String alllinks) throws Exception
{
try
{
URL testlink = new URL(alllinks);
HttpURLConnection httpurlconnect = (HttpURLConnection)testlink.openConnection();
httpurlconnect.setConnectTimeout(4000);
httpurlconnect.connect();
if(httpurlconnect.getResponseCode()==200)
{
System.out.println(testlink +"--"+httpurlconnect.getResponseMessage());
}
if(httpurlconnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)
{
System.out.println(testlink + "--" + httpurlconnect.getResponseMessage() + "--" +HttpURLConnection.HTTP_NOT_FOUND);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Tags :TestNG code to verify links, code to test all links
Another Good Example :-
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class LinkChecker
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\new\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(3000);
}
@Test
public void h() throws IOException
{
List alllinks = driver.findElements(By.tagName("a"));
System.out.println("Total links are :- " + alllinks.size());
for(int i = 0; i<alllinks.size(); i++)
{
WebElement links = (WebElement) alllinks.get(i);
String inputlink = links.getAttribute("href");
URL testlink = new URL(inputlink);
HttpURLConnection huc = (HttpURLConnection)testlink.openConnection();
huc.setConnectTimeout(4000);
huc.connect();
try
{
if(huc.getResponseCode()==200)
{
System.out.println(testlink + "--" +huc.getResponseMessage());
}
if(huc.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)
{
System.out.println(testlink + "--" + huc.getResponseMessage() + "--" + HttpURLConnection.HTTP_NOT_FOUND);
}
}
catch(Exception e)
{
System.out.println("Exception occured--" + e);
}
}
}
}
No comments:
Post a Comment