Links Validation

package com.link.validation;

import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import utility.Helper;

public class LinkValidationDemo {
   
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = Helper.launchBrowser("chrome");
       
        driver.get("http://anish-selenium.blogspot.in/");
        Thread.sleep(5000);
        List<WebElement> allLinks=driver.findElements(By.tagName("a"));

        System.out.println("Total number of links are: " + allLinks.size());
        //get the contents between the HTML tags we will use getText() method
        //Collection for loop
        for (Iterator<WebElement> iterator = allLinks.iterator(); iterator.hasNext();) {
            WebElement webElement = iterator.next();
            String valueInsideAnchorTag = webElement.getText();
            System.out.println(valueInsideAnchorTag);   
        }
       
        //get the attribute value inside HTML tags we will use getAttribute(String attributeName) method
        //foreach loop
        for (WebElement webElement : allLinks) {
            String url = webElement.getAttribute("href");
            System.out.println(url);
        }
       
        //get the attribute value inside HTML tags we will use getAttribute(String attributeName) method
        //Simple for loop
        for(int i=0;i < allLinks.size(); i++) 

            WebElement link = allLinks.get(i);
            System.out.println(link.getAttribute("href"));
        }
       
        //Link Validation
        for (WebElement webElement : allLinks) {
            String url = webElement.getAttribute("href");       
            System.out.println(Helper.linkValidation(url));
        }
    }
}

No comments:

Post a Comment