Handling Broken Links in Selenium
Links in web applications are used to navigate from one page to another.
Sometimes these links may be broken or dead, leading to pages that do not
exist. Identifying broken links is crucial in web testing to ensure a seamless
user experience.
ex: google link - it
opens google.com
gmail link - if we click , it opens gmail.com
when we click link- it is
not opening respective page -- we can
say that link is broken or also called "dead link"
When a link is clicked, a request is sent to the server, and the server
responds with a status code. By analyzing these status codes, we can determine
if a link is broken.
------------------
FAQ :how can you identify all broken links in page ?
When we send request by entering some url, it send request to
Server. Server will display the respective page and Gives some response code / http status code.
http Status code (or) response code :
--------------------------------
10x : 100
, 101 ---- 199
20x : 200
, 201, 299
30x : 300
40x :
50x :
100 : information response
200 : Successfull -
ok
300 : Redirecting Page
400 : Client error / bad
net work error
ex: if we give invalid url in browser,
This site can’t
be reached facebookrename12345.com’s server IP address could not be found.
500 : server errors
if server is down,
it does not give any reponse( 500)
Note:
Server Errors: If you encounter
500 series errors, inform the developer to check the application or server
status. These errors might occur due to server downtime or maintenance.
while testing appl, if we get 500 series error, inform devleoper -
check the appl/ server is down or send
mail to developer.
While
testing an application, if you encounter a 500 series error, it indicates a
server-side issue. Inform the developer to check if the application/server is
down.
For
example, the development team might perform deployments, causing the server to
be temporarily down. In such cases, the developer might inform you that the
deployment will take 30 minutes to an hour.
Refer:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
403 Forbidden:
The client does not have access rights to the content; that is, it
is unauthorized, so the server is refusing to give the requested resource.
Unlike 401 Unauthorized, the client's identity is known to the server.
-----------------------------------------------
package UploadfileBasics;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public class BrokenLinkBasics {
public static
void main(String[] args) throws IOException {
//
TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// send url to Server and get status code -
400,500 -> broken link
// 200, 300 --
not a broken link
// String
myurl ="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status";
//
// httpResponseCode =200 Ok -succesfull
//
invalid url
String
myurl
="https://developer.mozilla.org/en-US/docs/Web2/HTTP233/Status";
//
httpResponseCode =404
//
create Object for URL
URL
url = new URL(myurl);
//
import java.net pakcage
//
throws MalformedURLException
//
We cannot create obj for Abstract class
and Interface
//
Dont use this
// HttpURLConnection
htttpurlCon = new HttpURLConnection(url);
//
HttpURLConnection - is predefiend abstarct class
//
so we cannot create obj for
HttpURLConnection- class
HttpURLConnection
htttpurlCon = (HttpURLConnection)url.openConnection();
htttpurlCon.connect();
//to
get http status code no --> Method
int
htttpStatusCode = htttpurlCon.getResponseCode();
System.out.println("status
code ="+htttpStatusCode);//status code =200
//
200 ok -succesfull - ok
// if 400 , 500 series error- broken link else not broken link
if(htttpStatusCode
>=400)
{// 404 >=400
System.out.println("Given
Link ="+myurl + " is broken link.");
}
else
{
System.out.println("Given
Link ="+myurl + " is not
broken link.");
}
System.out.println("ends
here");
}
}
HW Write a separate reusable method to verifyBrokenLinks()?
public static
void verifyBrokenLinks(String url) throws IOException
{
}
Call :
verifyBrokenLinks("https://www.amazon.com/");
verifyBrokenLinks("https://www.facebook.com/");
verifyBrokenLinks("https://www.google.com/");
--------------------------------------
package
CookiesHandling;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrokenLinkBasics2 {
public static
void verifyBrokenLinks(String myurl)
throws IOException
{
int
Responsecode;
try
{
}
catch
(UnknownHostException uhe)
{
System.out.println("Got
UnknownHostException.Given Link ="+ myurl + " is broken link");
}
}
public static
void main(String[] args) throws IOException {
//
check link - https://www.amazon.in/ broken link
//
check link -https://www.facebook.com/ broken link
//invalid
link url
// verifyBrokenLinks("https://www.facebook12345A.com/");
verifyBrokenLinks("https://developer.mozilla.org/en-US/docs/Web2/HTTP233/Status");
System.out.println("ends
here");
}
}
o/p:
-----
Responsecode=200
Given Link =https://www.amazon.in/ is not broken link
Responsecode=200
Given Link =https://www.facebook.com/ is not broken link
Responsecode=404
Given Link
=https://developer.mozilla.org/en-US/docs/Web2/HTTP233/Status is broken link
ends here
-------------------------------
HW: FAQ VImp How can u
check all links from web page are broken
or not ?
hint : get all links from web page and call VerifyBrokenLinks(url);
No comments:
Post a Comment