How to check if link is broken without opening link in browser
To test whether link is broken or not without opening it in browser is tedious task. When we are dealing with large number of links on a single web page and to check status of each link, a JAVA method HttpURLConnection () will rescue.
First identify list of all the links available on webpage. Create a HTTP connection to check whether specified URL gives 200 status or not. If the status is 200 ok the link is not broken. HttpURLConnection () will check in background if link respond with 200 ok or not.
Create below class to create a connection which will return the response 200 Ok or 400:
![Capture](https://qatestinglearn.files.wordpress.com/2018/05/capture.png?w=900)
Create another method which will save list of all the links and get attribute “href” out of it, and pass it to above connection method
![Capture](https://qatestinglearn.files.wordpress.com/2018/05/capture1.png?w=900)
Now check the output. You will get status of each link without opening in browser.
Comments
Post a Comment