Monday, May 2, 2016

Handling Captcha in WebDriver

As definition itself says:
"A CAPTCHA is a program that  protects  websites against bots  by generating and grading tests that humans can pass but current computer programs cannot"

So these pages can't be automated fully. In fact Captcha itself is implemented to prevent automation. we can not completely automate the page having captcha.

While automation testing we can handle it by 3 ways as given below:
1- By disabling of captchas on test environments.

2- Hard code the captcha.
3- Handle by entering the captcha manually during execution.
 

3rd point can be done using below

WedriverPackage;

import java.util.Scanner;

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

public class HandlingCaptcha 

{    
        public static void main(String[] args)     
          {        
          
               WebDriver driver = new FirefoxDriver();        
               driver.get("https://www.irctc.co.in/");
               driver.findElement(By.id("usernameId")).sendKeys("vishal");
               driver.findElement(By.className("loginPassword")).sendKeys("vishal");  
               Scanner scan = new Scanner(System.in);
               System.out.println("Enter Captcha"); 
               String captcha = scan.nextLine(); 
               driver.findElement(By.className("loginCaptcha")).sendKeys(captcha);   
               driver.findElement(By.id("loginbutton")).click();    
           }
}



In case anyone has any other solution, or way to handle it, please share in comments. Thanks.


1 comment :