Skip to content

Commit

Permalink
Merge pull request #26 from testsigmahq/fix/TE-2415-Broken-link-finde…
Browse files Browse the repository at this point in the history
…r-addon-not-working-as-expected

[3.9.23.CS] [TE-2415] Broken link addon issues fixed
  • Loading branch information
vigneshtestsigma authored Sep 27, 2023
2 parents f6548c1 + 60bd7a6 commit b84327c
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
HttpGet request = new HttpGet(src);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
System.out.println(img.getAttribute("outerHTML") + " has broken image.");
logger.info(img.getAttribute("outerHTML") + " has broken image.");
brokenImages.add(img.getAttribute("src"));
}
}
Expand All @@ -60,10 +60,9 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
}

log("Total images in the page : " + image_list.size() + ", broken images : " + brokenImages.size() + " .");
System.out.println("Total images in the page : " + image_list.size() + ", broken images : " + brokenImages.size() + " .");

if (brokenImages.size() > 0) {
setSuccessMessage(" brokenImages : " + brokenImages);
setSuccessMessage("Broken Images [" + brokenImages.size() + "] : " + brokenImages);
return Result.SUCCESS;
} else {
setSuccessMessage("There are no Broken Images in the page");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public com.testsigma.sdk.Result execute() throws NoSuchElementException {
collectBrokenImages(URL.getValue().toString());
validLinks.forEach(link -> collectBrokenImages(link));
if (brokenImages.size() > 0) {
setSuccessMessage(" Broken Images [" + brokenImages.size() + "] : " + brokenImages.toArray());
setSuccessMessage("Broken Images [" + brokenImages.size() + "] : " + brokenImages);
return Result.SUCCESS;
} else {
setSuccessMessage("There are no Broken Images in the page");
return Result.SUCCESS;
}
} catch (Exception exception) {
logger.info("Exception while finding broken images "+ exception);
log("Exception while finding broken images "+ exception);
setErrorMessage("error while finding Broken Images ");
return Result.FAILED;
}
Expand All @@ -69,18 +69,19 @@ void collectBrokenImages(String URL) {
for (WebElement img : image_list) {
if (img != null) {
String src = img.getAttribute("src");
validatedImages.add(src);
if (validatedImages.contains(src)) {
if (brokenImages.contains(src)) {
System.out.println(img.getAttribute("outerHTML") + " has broken image.");
log(img.getAttribute("outerHTML") + " has broken image.");
}
} else {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(src);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
System.out.println(img.getAttribute("outerHTML") + " has broken image.");
log(img.getAttribute("outerHTML") + " has broken image.");
brokenImages.add(src);
} else{
validatedImages.add(src);
}
}
}
Expand All @@ -91,14 +92,13 @@ void collectBrokenImages(String URL) {
}
}

Boolean collectValidLinks(String URL, Integer nestedIterationsLevel) {
Boolean collectValidLinks(String URL, Integer nestedIterationsLevel) {
if(URL==null) return false;
driver.get(URL);
String href = "";
String url = URL.substring(URL.indexOf("://")+3);
url = url.indexOf("/") != -1 ? url.substring(0, url.indexOf("/")) : url;
List<WebElement> links = driver.findElements(By.tagName("a"));

Iterator<WebElement> it = links.iterator();


Expand All @@ -107,19 +107,17 @@ Boolean collectValidLinks(String URL, Integer nestedIterationsLevel) {

if (href == null || href.isEmpty() || href.startsWith("tel:")) {
anchorTagsWithEmptyURLs++;
System.out.println("URL is either not configured for anchor tag or it is empty");
log("URL is either not configured for anchor tag or it is empty");
continue;
}

if (validatedLinks.contains(href)) {
continue;
}
validatedLinks.add(href);
System.out.println(href);

if (!href.startsWith(url)) {
if (!href.contains(url)) {
skippedURLs.add(href);
System.out.println("URL belongs to another domain, skipping it.");
log("URL belongs to another domain, skipping it.");
continue;
}

Expand All @@ -134,12 +132,12 @@ Boolean collectValidLinks(String URL, Integer nestedIterationsLevel) {

if (respCode >= 400) {
brokenURLs.add(href);
System.out.println(href + " is a broken link");
log(href + " is a broken link");
} else {
if (!href.equals(this.URL.getValue().toString())) {
validLinks.add(href);
}
System.out.println(href + " is a valid link");
log(href + " is a valid link");
}

} catch (MalformedURLException e) {
Expand All @@ -153,11 +151,11 @@ Boolean collectValidLinks(String URL, Integer nestedIterationsLevel) {
nestedIterationsLevel--;
Integer nextNestedIterationsLevel = nestedIterationsLevel;
for(int i= 0; i< links.size();i++){
Boolean collected = collectValidLinks(links.get(i).getAttribute("href"), nextNestedIterationsLevel);
if(collected == false){
logger.info("Skipping Element as its URL is Null, Element - " + links.get(i));
}
return collected;
Boolean collected = collectValidLinks(links.get(i).getAttribute("href"), nextNestedIterationsLevel);
if(collected == false){
log("Skipping Element as its URL is Null, Element - " + links.get(i));
}
return collected;
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ public class FindAllBrokenImagesInPageAndItsImmediateChildPages extends WebActio

@Override
public com.testsigma.sdk.Result execute() throws NoSuchElementException {
try{
collectValidLinks(URL.getValue().toString());
collectBrokenImages(URL.getValue().toString());
System.out.println(validLinks.size());
validLinks.forEach(link -> {
collectBrokenImages(link);
});
if (brokenImages.size() > 0) {
setSuccessMessage(" Broken Images [" + brokenImages.size() + "] : " + brokenImages.toArray());
return Result.SUCCESS;
} else {
setSuccessMessage("There are no Broken Images in the page");
return Result.SUCCESS;
}
}catch (Exception exception){
setErrorMessage("error while finding Broken Images ");
return Result.FAILED;
}
try{
collectValidLinks(URL.getValue().toString());
collectBrokenImages(URL.getValue().toString());

validLinks.forEach(link -> {
collectBrokenImages(link);
});
if (brokenImages.size() > 0) {
setSuccessMessage(" Broken Images [" + brokenImages.size() + "] : " + brokenImages);
return Result.SUCCESS;
} else {
setSuccessMessage("There are no Broken Images in the page");
return Result.SUCCESS;
}
}catch (Exception exception){
setErrorMessage("error while finding Broken Images ");
return Result.FAILED;
}
}

void collectBrokenImages(String URL) {
Expand All @@ -72,18 +72,20 @@ void collectBrokenImages(String URL) {
if (img != null) {
String src = img.getAttribute("src");
if(src != null) {
validatedImages.add(src);

if (validatedImages.contains(src)) {
if (brokenImages.contains(src)) {
System.out.println(img.getAttribute("outerHTML") + " has broken image.");
log(img.getAttribute("outerHTML") + " has broken image.");
}
} else {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(src);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
System.out.println(img.getAttribute("outerHTML") + " has broken image.");
log(img.getAttribute("outerHTML") + " has broken image.");
brokenImages.add(src);
} else{
validatedImages.add(src);
}
}
}
Expand All @@ -103,15 +105,14 @@ Boolean collectValidLinks(String url) {
url1 = url1.indexOf("/") != -1 ? url1.substring(0, url1.indexOf("/")) : url1;
String href = "";
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println(links.size());
Iterator<WebElement> it = links.iterator();

while (it.hasNext()) {
href = it.next().getAttribute("href");

if (href == null || href.isEmpty() || href.startsWith("tel:") || href.startsWith("mailto:") || href.startsWith("javascript:") ){
anchorTagsWithEmptyURLs++;
System.out.println("URL is either not configured for anchor tag or it is empty");
log("URL is either not configured for anchor tag or it is empty");
continue;
}

Expand All @@ -120,9 +121,9 @@ Boolean collectValidLinks(String url) {
}
validatedLinks.add(href);

if (!href.startsWith(url1)) {
if (!href.contains(url1)) {
skippedURLs.add(href);
System.out.println("URL belongs to another domain, skipping it.");
log("URL belongs to another domain, skipping it.");
continue;
}

Expand All @@ -137,12 +138,12 @@ Boolean collectValidLinks(String url) {

if (respCode >= 400) {
brokenURLs.add(href);
System.out.println(href + " is a broken link");
log(href + " is a broken link");
} else {
if (!href.equals(this.URL.getValue().toString())) {
validLinks.add(href);
}
System.out.println(href + " is a valid link");
log(href + " is a valid link");
}

} catch (MalformedURLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Result execute() throws NoSuchElementException {
url = it.next().getAttribute("href");
if (url == null || url.isEmpty()) {
anchorTagsWithEmptyURLs++;
System.out.println("URL is either not configured for anchor tag or it is empty");
log("URL is either not configured for anchor tag or it is empty");
continue;
}

Expand All @@ -58,11 +58,10 @@ public Result execute() throws NoSuchElementException {
}

validatedLinks.add(url);
System.out.println(url);

if (!url.startsWith(url1)) {
if (!url.contains(url1)) {
skippedURLs.add(url);
System.out.println("URL belongs to another domain, skipping it.");
log("URL belongs to another domain, skipping it.");
continue;
}

Expand All @@ -77,9 +76,9 @@ public Result execute() throws NoSuchElementException {

if (respCode >= 400) {
brokenURLs.add(url);
System.out.println(url + " is a broken link");
log(url + " is a broken link");
} else {
System.out.println(url + " is a valid link");
log(url + " is a valid link");
}

} catch (MalformedURLException e) {
Expand All @@ -89,7 +88,7 @@ public Result execute() throws NoSuchElementException {
}
}
if (brokenURLs.size() > 0) {
setSuccessMessage(" brokenURLs : " + brokenURLs);
setSuccessMessage(" Broken URLs : " + brokenURLs);
return Result.SUCCESS;
} else {
setSuccessMessage("There are no Broken links in the page");
Expand Down
Loading

0 comments on commit b84327c

Please sign in to comment.