Tuesday, 15 March 2016

broken links based on response code using selenium webdriver

Broken links based on response code using selenium webdriver:
=================================================
import com.jayway.restassured.response.Response;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

import static com.jayway.restassured.RestAssured.given;

public class HttpResponseCode {
<div style="clear:both; margin-top:0em; margin-bottom:1em;"><a href="http://www.testingexcellence.com/how-to-create-update-and-delete-cookies-in-webdriver/" target="_blank" rel="nofollow" class="u0b585470f054c23045cf8ba03aa2e6e5"><div style="padding-left:1em; padding-right:1em;"><span class="ctaText"></span>  <span class="postTitle">How to Create, Update and Delete Cookies in WebDriver</span></div></a></div>
    WebDriver driver;
    Response response;

    public void checkBrokenLinks() {
        driver = new FirefoxDriver();
        driver.get("http://rameshselenium.blogspot.in/");

        //Get all the links on the page
        List<WebElement> links = driver.findElements(By.cssSelector("a"));

        String href;
       
        for(WebElement link : links) {
            href = link.getAttribute("href");
            response = given().get(href).then().extract().response();

            if(200 != response.getStatusCode()) {
                System.out.println(href + " gave a response code of " + response.getStatusCode());
            }
        }
    }

    public static void main(String args[]) {
        new HttpResponseCode().checkBrokenLinks();
    }
}

No comments:

Post a Comment