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.

Tuesday 21 January 2014

Java convert string to int value

Java provides two method parseInt() and valueOf() method to convert string into integer. Below example shows the usage of two methods to convert string to integer.

public class StringtoNumber {

public static void main(String[] args) {
String String1 = "000456";
  try{
  // using Integer.parseInt method
       int intvalue = Integer.parseInt(String1);
       System.out.println("With parseInt method, intvalue = " + intvalue );
        } catch(NumberFormatException e) {
        System.err.println("NumberFormatException in parseInt, "+ e.getMessage());
        }
         
        try{
        // using Integer.valueOf method
             int intvalue = Integer.valueOf(String1);
             System.out.println("With valueOf method, intvalue = " + intvalue );
             
          } catch(NumberFormatException e) {
              System.err.println("NumberFormatException in valueOf, "+ e.getMessage());
        }
     }
}

Output -
      With parseInt method, intvalue = 456

      With valueOf method, intvalue = 456

Note - Catch section gets executed in case, Java fails to convert string to integer.

Thursday 9 January 2014

Selenium WebDriver: How to Handle Ajax Application

A common problem we face during automation using selenium WebDriver is to handle the ajax calls. It is difficult to know when the call is complete and page has been updated. There are several ways to handle the ajax calls. This blog describes them one by one.
We can handle Ajax calls in two ways:


1. Hard Wait - this is most commonly used method but not recommended while doing the automation. Only in some exceptional cases use or for short time use it
·    JAVA: Use java method Thread.Sleep( to suspend execution for specified time. Syntax –

Thread.sleep(1000) // time unit is millisecond

·   Implicit Wait – An implicit wait is to tell WebDriver to stop the DOM for a certain amount of time. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. Syntax –

driver.manage().timeouts().Implicitly Wait(10, TimeUnit.SECONDS); 
// Time is in  seconds or we can also change Unit in second argument

2.   Explicit Wait: An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. Sample code is as follows -

WebDriver driver = new FirefoxDriver();
driver.get("http://google.co.in");
WebDriverWait myDElement = new WebDriverWait(driver, 10)

.until(ExpectedConditions.presenceOfElementLocated(By.xpath("xxxxx")));

Tuesday 7 January 2014

HP QTP : What is a Run-Time Data Table? Where can I find and view this table


Data like parameterized output, checkpoint values, and output values are stored in the Run-time Table. It is an XLS file which is stored in the Test Results Folder.  It can also be viewed in the Test Report. Click on View -> Data Table to display data table.