Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
221 views
by

https://www.corteva.ca/en/products-and-solutions/crop-protection/herbicides.html is a broken link on https://www.corteva.ca/en/products-and-solutions/crop-protection.html

However this script is not identifying broken links.

package ca.corteva.tests;

import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class WBBrokenLinks {

    private static WebDriver driver = null;

public static void main(String[] args) throws InterruptedException {

//Instantiating FirefoxDriver

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "/src/main/java/resources/chromedriver.exe");

driver = new ChromeDriver();

driver.manage().window().maximize();

//Implicit wait for 10 seconds

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

List<String> brokenLinks = getBrokenURLs(driver, "https://www.corteva.ca/", 1, new ArrayList<String>());

for(String brokenLink : brokenLinks){

System.out.println("Broken Link :" + brokenLink);

}

}

public static List<String> getBrokenURLs(WebDriver driver, String appURL, int depth, List<String> links){

{

driver.navigate().to(appURL);

System.out.println("Depth of " + appURL +": " + depth);

while(depth > 0){

//List<WebElement> linkElems = driver.findElements(By.tagName("a"));

List<WebElement> linkElems = driver.findElements(By.xpath("//a[@href!='']"));

//Add all links to links variable 

for(WebElement linkElement : linkElems)

if(!links.contains(linkElement))

links.add(linkElement.getAttribute("href"));

for(String link : links)

getBrokenURLs(driver, link, --depth, links);

}

}

return getBrokenURLs(driver, links, new ArrayList<String>()) ;

}

public static List<String> getBrokenURLs(WebDriver driver, List<String> links, List<String> brokenLinks){

{

for(String link : brokenLinks){

driver.navigate().to(link);

if(driver.getTitle().contains("404 Page Not Found")){

brokenLinks.add(link);

}

}

}

return brokenLinks ;

}

}

2 Answers

0 like 0 dislike
by The go-to Tester (142 points)

In getBrokenURLs(WebDriver driver, List<String> links, List<String> brokenLinks) function, the title of page is expected to be "Corteva 404 Page Not Found" and not, "404 Page Not Found".

Kindly check and try it again.

0 like 0 dislike
by Master (1.2k points)

What is the error that you are facing?

I suspect that the code, if(!links.contains(linkElement)) is invalid.

because, links is of type List<String> and linkElement is WebElement.

Instead, you can rewrite it like this,

if(!links.contains(linkElement.getAttribute("href"))) 

try it and let us know. Also, post any error stack trace. It will be easy to diagnose.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...