Handling Captcha Using WebDriver - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Tuesday 15 October 2013

Handling Captcha Using WebDriver

A CAPTCHA is a program that  protects  websites against bots  by generating and grading tests that humans can pass but current computer programs cannot.

Here I will be Using loops to let the User Enter Captcha Manually

import java.awt.Frame;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.internal.seleniumemulation.IsTextPresent;
import org.openqa.selenium.remote.server.handler.MaximizeWindow;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


public class handlecaptcha {

public WebDriver driver;
@Test
public void captchahandling()throws Exception
{
driver=new FirefoxDriver();
driver.manage().window().maximize();
WebDriverWait wait=new WebDriverWait(driver,50);
driver.navigate().to("http://www.qualitytesting.info/main/authorization/signUp?"); //(A Sample website)
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

driver.findElement(By.id("signup_email")).sendKeys("email@gmail.com");
driver.findElement(By.id("signup_password")).sendKeys("password");
driver.findElement(By.id("signup_password_confirm")).sendKeys("password");
WebElement element=driver.findElement(By.id("dob-month"));
Select selectmonth=new Select(element);
selectmonth.selectByValue("1");

WebElement element1=driver.findElement(By.name("birthdateDay"));
Select selectday=new Select(element1);
selectday.selectByValue("1");

WebElement element2=driver.findElement(By.name("birthdateYear"));
Select selectyear=new Select(element2);
selectyear.selectByValue("1988");

//
WebElement captcha=driver.findElement(By.cssSelector("input#recaptcha_response_field"));
captcha.click();

String test1= JOptionPane.showInputDialog("Please enter the captch value: ");// Pop up where the user enters the Captcha
captcha.sendKeys(test1);
Thread.sleep(2000);
driver.findElement(By.cssSelector(".button.dy-action-primary")).click();
Thread.sleep(1000);
if(driver.getPageSource().contains("Your response did not match the words"))
 {
   System.out.println("Fail");
   for(int i=0;i<6;i++)
{

      System.out.println("here"+i);
Thread.sleep(3000);
WebElement captcha1=driver.findElement(By.cssSelector("input#recaptcha_response_field"));
captcha1.click();
System.out.println("nowhere"+i);

String test2= JOptionPane.showInputDialog("Please enter the captch value: ");
captcha1.sendKeys(test2);
Thread.sleep(2000);
driver.findElement(By.cssSelector(".button.dy-action-primary")).click();
Thread.sleep(1000);

if(driver.getPageSource().contains("Your response did not match the words"))
 {
   System.out.println("Fail");
 }
}

}
else
 {
   System.out.println("Pass");
 }

}

}

No comments:

Post a Comment