Monday, 10 February 2014

How to Verify Broken Links using Selenium WebDriver

This program reads all link from a web page, sends open connection request to URL, checks for response code. Based on response code, broken link is identified & get printed on console.

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Links_Broken {

    @Test
      public void saveAllLinks(){
          FirefoxDriver firefoxDriver = new FirefoxDriver(); //Starts Firefox browser
                  
          firefoxDriver.navigate().to("
http://google.co.in"); //opens Web Page
               
           List <WebElement>linksList = firefoxDriver.findElements(By.tagName("a")); // finds link  elements & stores in a list

//traverse each link from collection
          for(WebElement linkElement: linksList){
              String link =linkElement.getAttribute("href");
              if(link!=null){
                verifyLinkActive(link);
              }
          }
          firefoxDriver.quit(); // close Firefox browser
      }
          
      /**
       * This method verifies that link is active
       * @param link - link(URL)
       * @return - true/false
       */
       public 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());
              }
            if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)  

             {
                 System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage() + " - "+                  HttpURLConnection.HTTP_NOT_FOUND);
              }
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
}

Tuesday, 4 February 2014

Working with Selenium WebDriver and Cucumber without Maven

Download jar files

List of jar file needed to configure Cucumber is as follows -
1.       cucumber-core-1.1.5.jar
2.       cucumber-html-0.2.3.jar
3.       cucumber-java-1.1.5.jar
4.       cucumber-junit-1.1.5.jar
5.       cucumber-jvm-deps-1.0.3.jar
6.       gherkin-2.12.2.jar
7.       hamcrest-core-1.3.jar
8.       jchronic-0.2.6.jar
9.       Junit-4.11.jar
10.     Selenium-server-standalone-2.39.0.jar


Create Cucumber Project in Eclipse

1. Go to [File -> New -> Java Project]

2. Enter project name in [Project Name] field

3. Click on [Finish] button

4. Delete [src ] folder of this project

5. [Right click on Project folder] & select [Source Folder]

6. Enter value [src/test/resources] in [Folder Name] field

7. Click on [Finish] button

8. [Right click on Project folder] & select [Source Folder]

9. Enter value [src/test/java] in [Folder Name] field

10. Click on [Finish] button

11. [Right click on Project folder] & select [Source Folder]

12. Enter value [src/main/java] in [Folder Name] field

13. Click on [Finish] button


14.  Add above downloaded jar file into Build path of your Selenium Project.