Finding Broken links using Rest Assured and Selenium




In the previous post, we studied about Response Logging Feature of Rest Assured. In this post, we will learn about verifying broken links using Rest Assured.

We would require to add following selenium dependencies to pom.xml file present in the project:

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
</dependency>  

Let us navigate to the website :http://restservicestesting.blogspot.in/ and find broken links present if any using the following example:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;

public class GetRequestTest {

@Test
public void checkBrokenLinks() {

  System.setProperty("webdriver.chrome.driver", "Resources/chromedriver");

  WebDriver driver = new ChromeDriver();
  driver.get("http://restservicestesting.blogspot.in/");

  List<String> hrefs = new ArrayList<String>();
  List<WebElement> anchors = driver.findElements(By.tagName("a"));

  for (WebElement anchor : anchors) {

    if ( anchor.getAttribute("href") != null ) 
    hrefs.add(anchor.getAttribute("href"));

  }

  for (String href : hrefs) {

    try {

      int responseCode = returnStatusCode(new URL(href));
      if ( responseCode != 200 ) {
        System.out.println("The broken Link is " + href);

      }
      else {

        System.out.println("The working Link is " + href);

      }
    }
   catch (Exception e) {
     System.out.println("URL: " + href + " returned " + e.getMessage());

   }

}

}


public int returnStatusCode(URL url) {
  Response resp = given().

    when().

    get(url);

  return resp.getStatusCode();
  }

}

Explanation of checkBrokenLinks Method:

We have set the path to Chrome Driver which is present in the resources folder of the project. Link to download the chrome driver : Chrome Driver Download.
Then we instantiate an object of Chrome Driver and navigate to site. Later on we create an array list of strings and webelement respectively. The arraylist anchors will store all elements whose tagname is a. 
Now we will iterate through the arraylist of anchors webelement and add all the links to hrefs arraylist which mean we will store the value of href attribute i.e link.
After adding all links to hrefs arraylist we we will iterate through the arraylist and inside the iterator we will call the method returnStatusCode which will return us the status code of each link. If the status code is 200 then we will print that the link is working otherwise not working.
Explanation of returnStatusCode Method:


Rest Assured follows the BDD ( Behaviour Driven Development ) approach for writing tests i.e Given- When - Then structure. In Get method we will mention the Url to check. The response will be stored in resp variable.

Then we will call the statusCode method of Response interface and return the statusCode.

Comments

Popular posts from this blog

Extracting an XML Response with Rest-Assured