To answer this question i will take an example of Google search. On a google search page if you enter suppose "Blog" and search it then on a next page you will get multiple links and we have to check each link whether it is working or not. The below code will help you the achieve this task.
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.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrokenLinks {
public static WebDriver driver;
public static HttpURLConnection HUC;
public static String strhref;
public static int actualResponseCode;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.google.co.in");
driver.findElement(By.name("q")).sendKeys("blogs");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
List<WebElement> noOfLinks=driver.findElements(By.xpath("//h3//parent::a"));
System.out.println("No of links available on page are :"+ noOfLinks.size());
for(WebElement link:noOfLinks)
{
strhref=link.getAttribute("href");
if(strhref.isEmpty() || strhref==null)
{
System.out.println("No url found for link name :"+ link.getText());
}
try {
HUC=(HttpURLConnection)(new URL(strhref).openConnection());
HUC.connect();
actualResponseCode=HUC.getResponseCode();
if(actualResponseCode>=400)
{
System.out.println(strhref +": is a broken link");
}
else
{
System.out.println(strhref +": is not a broken link and is working as expected");
}
}
catch(MalformedURLException m)
{
System.out.println(m.toString());
}
catch(IOException io)
{
System.out.println(io.toString());
}
}
}
}
Now, we have to understand the scenario where we can use it. While performing manual testing you have come across a situation multiple times where you have to check and validate that all the visible links on a web page are working or not. To perform this manually would be a cumbersome task if multiple links are available on a web page. So, to ease that work we can use the above code which can validate all the links in few seconds and will provide results quickly.
What is HttpURLConnection?
The HttpURLConnection is java class which is http specific URLConnection. It works for HTTP protocol only. With the help of HttpURLConnection class, we can get the information of any HTTP URL such as header information, status code, response code etc.The java.net.HttpURLConnection is subclass of URLConnection class.
To achieve this task what you should understand the following steps.
1. First identify the number of links available on a web page. For this I have used the below code.
List<WebElement> noOfLinks=driver.findElements(By.xpath("//h3//parent::a"));
System.out.println("No of links available on page are :"+ noOfLinks.size());
2. Get the href attribute value for each link and send that value to the server using HTTP method and get the response code which can help you to identify whether the link is broken or not. For this point I have highlighted the relevant line with bold font.
for(WebElement link:noOfLinks)
{
strhref=link.getAttribute("href");
if(strhref.isEmpty() || strhref==null)
{
System.out.println("No url found for link name :"+ link.getText());
}
try {
HUC=(HttpURLConnection)(new URL(strhref).openConnection());
HUC.connect();
actualResponseCode=HUC.getResponseCode();
if(actualResponseCode>=400)
{
System.out.println(strhref +": is a broken link");
}
else
{
System.out.println(strhref +": is not a broken link and is working as expected");
}
No comments:
Post a Comment