package zacksolutions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class BrokenLinks { WebDriver driver; @Test public void checksLinks(){ System.getProperty("web-driver.gecko", "/home/ilyes/SeleniumPractice/Zenful/drivers/geckodriver"); /* FirefoxOptions options = new FirefoxOptions(); options.addArguments("--headless"); driver = new FirefoxDriver(options); */ driver = new FirefoxDriver(); driver.get("https://www.reddit.com/"); List links = driver.findElements(By.tagName("a")); System.out.println(links.size()); //let's check the numbers of broken links List urlList = new ArrayList<>(); for(WebElement e : links) { String url = e.getAttribute("href"); urlList.add(url); //brokenLinks(url); } long strtTime = System.currentTimeMillis(); //urlList.forEach(this::brokenLinks); //Time of test is: 141916 urlList.parallelStream().forEach(this::brokenLinks); //Time of test is: 79616 long endTime = System.currentTimeMillis(); System.out.println("Time of test is: " + (endTime - strtTime)); } public void brokenLinks(String urlLink){ try { URL url = new URL(urlLink); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5000); httpURLConnection.connect(); if (httpURLConnection.getResponseCode() >= 400){ System.out.println("The following link " + urlLink + " has a code " + httpURLConnection.getResponseMessage() + " == Brken link"); } else{ System.out.println("links working fine"); } }catch (Exception e){ } } @AfterTest public void tearDown(){ driver.close(); } }