How to check broken links on a web page
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebPageLinks {
WebDriver driver;
String url;
@Before
public void atBefore() throws Exception{
driver = new FirefoxDriver();
url = "http://www.google.com";
driver.manage().window().maximize();
}
@Test
public void atTest() throws Exception{
driver.get(url);
driver.findElement(By.id("gbqfq")).sendKeys("JAVA"); //Write 'JAVA' keyword in search text box
driver.findElement(By.id("gbqfb")).click();
Thread.sleep(4000);
List<String> Linkarray = new ArrayList<String>();
List<WebElement> Linklist = driver.findElements(By.tagName("a")); //Finding all element on the page with 'a' tag.
for (WebElement link : Linklist) {
String links = link.getText();
if(links.isEmpty()){
System.out.println("No Text."); //Checking if any link doesn't have text
}
else{
Linkarray.add(links );
System.out.println(links);
}
}
System.out.println("=============================================");
for(int i=0; i<Linkarray.size(); i++)
{
System.out.println("Element at " + i + " is : "+ Linkarray.get(i));
}
for (String linkToTest : Linkarray){
try{
driver.findElement(By.linkText(linkToTest)).click(); //Clicking on the link
}
catch(Exception e){
System.out.println("The link " + linkToTest.substring(0) + " is not found in the page."); //Print if any link not present on the page. Some links might be change in this case when navigate on the main page
}
Thread.sleep(15000L);
if(driver.getTitle().contains("Problem")) { //Checking if page title having 'Problem' word
System.out.println("Fail");
}
else{
System.out.println("pass");
}
driver.navigate().back(); //Navigate on main page
Thread.sleep(5000L);
}
}
}
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebPageLinks {
WebDriver driver;
String url;
@Before
public void atBefore() throws Exception{
driver = new FirefoxDriver();
url = "http://www.google.com";
driver.manage().window().maximize();
}
@Test
public void atTest() throws Exception{
driver.get(url);
driver.findElement(By.id("gbqfq")).sendKeys("JAVA"); //Write 'JAVA' keyword in search text box
driver.findElement(By.id("gbqfb")).click();
Thread.sleep(4000);
List<String> Linkarray = new ArrayList<String>();
List<WebElement> Linklist = driver.findElements(By.tagName("a")); //Finding all element on the page with 'a' tag.
for (WebElement link : Linklist) {
String links = link.getText();
if(links.isEmpty()){
System.out.println("No Text."); //Checking if any link doesn't have text
}
else{
Linkarray.add(links );
System.out.println(links);
}
}
System.out.println("=============================================");
for(int i=0; i<Linkarray.size(); i++)
{
System.out.println("Element at " + i + " is : "+ Linkarray.get(i));
}
for (String linkToTest : Linkarray){
try{
driver.findElement(By.linkText(linkToTest)).click(); //Clicking on the link
}
catch(Exception e){
System.out.println("The link " + linkToTest.substring(0) + " is not found in the page."); //Print if any link not present on the page. Some links might be change in this case when navigate on the main page
}
Thread.sleep(15000L);
if(driver.getTitle().contains("Problem")) { //Checking if page title having 'Problem' word
System.out.println("Fail");
}
else{
System.out.println("pass");
}
driver.navigate().back(); //Navigate on main page
Thread.sleep(5000L);
}
}
}
-----------------------------------------------------------------------------------------------------------
No comments:
Post a Comment