Friday, August 5, 2016

Broken links code using Selenium Java

package pack1;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrokenLinks {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

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

driver.get("http://www.newtours.demoaut.com/");

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

System.out.println("Total links are " + links.size());

for (int i = 0; i < links.size(); i++) {

WebElement ele = links.get(i);

String url = ele.getAttribute("href");

verifyLinkActive(url);

}

}

public static void verifyLinkActive(String linkUrl) {
try {
URL url = new URL(linkUrl);

HttpURLConnection httpURLConnect = (HttpURLConnection) url
.openConnection();

httpURLConnect.setConnectTimeout(3000);

httpURLConnect.connect();

if (httpURLConnect.getResponseCode() == 200) {
System.out.println(linkUrl + " - "
+ httpURLConnect.getResponseMessage() + " - Code :"
+ httpURLConnect.getResponseCode());
}
if (httpURLConnect.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
System.out.println(linkUrl + " - "
+ httpURLConnect.getResponseMessage() + " - "
+ HttpURLConnection.HTTP_NOT_FOUND);
}
} catch (Exception e) {

}
}

}

Selenium Exceptions with Description

Exception Type
Description
ElementNotSelectableException
Thrown when trying to select an unselectable element.
ElementNotVisibleException
Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
ErrorInResponseException
Thrown when an error has occurred on the server side.
ImeActivationFailedException
Thrown when activating an IME engine has failed.
ImeNotAvailableException
Thrown when IME support is not available
InvalidCookieDomainException
Thrown when attempting to add a cookie under a different domain than the current URL
InvalidElementStateException

InvalidSelectorException
Thrown when the selector which is used to find an element does not return a WebElement.
InvalidSwitchToTargetException
Thrown when frame or window target to be switched doesn’t exist.
MoveTargetOutOfBoundsException
Thrown when the target provided to the ActionsChains move() method is invalid, i.e. out of document
NoAlertPresentException
Thrown when switching to no presented alert.
NoSuchAttributeException
Thrown when the attribute of element could not be found.
NoSuchElementException
Thrown when element could not be found.
NoSuchFrameException
Thrown when frame target to be switched doesn’t exist.
NoSuchWindowException
Thrown when window target to be switched doesn’t exist.
RemoteDriverServerException

StaleElementReferenceException
Stale means the element no longer appears on the DOM of the page.
TimeoutException
Thrown when a command does not complete in enough time.
UnableToSetCookieException
Thrown when a driver fails to set a cookie.
UnexpectedAlertPresentException
Thrown when an unexpected alert is appeared.
UnexpectedTagNameException
Thrown when a support class did not get an expected web element.
WebDriverException
Base webdriver exception.

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...