Saturday, June 11, 2016

Verifying broken links using Selenium

Hi, I have started my coding sprint for the 2016 Google Summer of Code by writing a test script to check if all the links on the MACC application is working.

So basically I have achieved this task by verifying for broken links(i.e we need to check the link which is pointing to wrong URL or invalid URL).

 So if the link is pointing to a wrong or invalid URL, we get a 404 page not found error an issue in most web applications which is called Broken Link.

Below I have explain the implementation logic and my own approach:

First extract all links on the site as follows, List list=driver.findElement(By.tagName("a")); .

Then I use HttpURLConnection Class and getResponseCode method to verify the status of the response for each URL like so, 



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();

}

Next up, I will be doing thesame for the images. Cheers!

No comments:

Post a Comment