-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBroken_Links
52 lines (45 loc) · 1.46 KB
/
Broken_Links
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package automationFramework;
import org.openqa.selenium.chrome.ChromeDriver;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class broken_links {
public static void main(String args[])
{
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demoqa.com/broken");
//Storing the links in a list and traversing through the links
List<WebElement> links = driver.findElements(By.tagName("a"));
for(int i=0;i<links.size();i++)
{
WebElement E1= links.get(i);
String url= E1.getAttribute("href");
verifyLinks(url);
}
driver.quit();
}
public static void verifyLinks(String linkUrl)
{
try
{
URL url = new URL(linkUrl);
//Now we will be creating url connection and getting the response code
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(5000);
httpURLConnect.connect();
if(httpURLConnect.getResponseCode()>=400)
{
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage()+"is a broken link");
}
//Fetching and Printing the response code obtained
else{
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
}
}catch (Exception e) {
}
}
}