How to find total links and broken links in selenium web driver:
=================================================
package blog;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class LinksDemo {
WebDriver webDriver;
Actions actions;
//declare variables for count sake
int enabledLinks;
int disabledLinks;
@BeforeSuite
public void launchingBrowser() {
System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
webDriver = new InternetExplorerDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}
@Test
public void testScript() throws InterruptedException {
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();
//get all the links in a page
List<WebElement> links = webDriver.findElements(By.tagName("a"));
Iterator<WebElement> iterator = links.iterator();
while (iterator.hasNext()) {
WebElement link = (WebElement)iterator.next();
if (link.isDisplayed()&&link.isEnabled()) {
enabledLinks++;
}
else
{
disabledLinks++;
}
}
System.out.println("total number of working links are :" +enabledLinks);
System.out.println("total number of disabled links are :" +disabledLinks);
}
@AfterSuite
public void closeBrowser() {
webDriver.quit();
}
}
=================================================
package blog;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class LinksDemo {
WebDriver webDriver;
Actions actions;
//declare variables for count sake
int enabledLinks;
int disabledLinks;
@BeforeSuite
public void launchingBrowser() {
System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
webDriver = new InternetExplorerDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
}
@Test
public void testScript() throws InterruptedException {
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();
//get all the links in a page
List<WebElement> links = webDriver.findElements(By.tagName("a"));
Iterator<WebElement> iterator = links.iterator();
while (iterator.hasNext()) {
WebElement link = (WebElement)iterator.next();
if (link.isDisplayed()&&link.isEnabled()) {
enabledLinks++;
}
else
{
disabledLinks++;
}
}
System.out.println("total number of working links are :" +enabledLinks);
System.out.println("total number of disabled links are :" +disabledLinks);
}
@AfterSuite
public void closeBrowser() {
webDriver.quit();
}
}
No comments:
Post a Comment