9q. WebDriver – Handling a CAPTCHA

Did someone say captchas? Okay, I heard it! Let’s see how these can be handled using Selenium WebDriver and yeah, we will try getting this done quickly as I will have to shoot off to a contest later today.

Prepare to be lit up

CAPTCHA is a bacronym for “Completely Automated Public Turing test to tell Computers and Humans Apart”.

Oh, don’t get mad at me! That was not a typo! There really is something called a bacronym/backronym. When an abbreviation is formed from the initials of other words, it is called an acronym. But if a new phrase is created to fit already existing initials, then it is called a bacronym.

So now that you know what a bacronym is and what a CAPTCHA stands for, lets get into the details. Captchas are mainly used to determine whether the user is a human or not. It generates images or some form of tests that humans can solve but bots/computer programs cannot.

So its main purpose is to prevent bots and other automated programs from obtaining any sensitive information from the website. Hence captcha exists to kill automation! If we are able to automate it, then the reason of using captchas become worthless.

Do I have anything up my sleeve? Let me see…

  1. For the pupose of testing, we can always request the developers to
    1. Disable captcha validation
    2. Provide a workaround or backdoor for obtaining the captcha value
    3. Tweak captcha such that it accepts one specific input value everytime
    4. Develop a way, where a random text gets stored in the alt attribute of the captcha image which can then be obtained and passed into the captcha text box using available locators

But be very sure that these are ONLY active in the testing environment and are used ONLY for the purpose of automation.

Note that the application security is getting compromised while testing using these methods mentioned above.

  1. If you want to test the application as it is, i.e. without making any modifications to the testing environment for the purpose of automation, then separate all test scripts that involve captcha validation and package them as a single test suite. Run this test suite with human intervention. I know this is partial automation but it is not always possible to automate everything under a given set of circumstances. To get something you should be ready to forego something.
    1. Entire test case except captcha can be automated.
    2. An implicit or an explicit wait can be used and user can be promped to enter the displayed captcha.
    3. When the test script is run, each step will be executed as usual. Once the prompt command is reached, a pop up appears on the browser. User enters the captcha as displayed on the screen.
    4. Once the captcha is entered manually, the test case execution resumes and the subsequent steps are run on the browser.

This was the procedure I followed in one of my previous projects. Because this method makes sure that the application under test is exactly the one that will be pushed into production and also the security is not compromised in any way.

Let us look at an example to get a better understanding.

Scenario

  1. Open Firefox browser
  2. Navigate to https://www.in.ckgs.us/myaccount/
  3. Locate ‘Current Passport Number’ text box by cssSelector: tag and name attribute
  4. Enter ‘123456789’
  5. Prompt the user to intervene and enter the displayed captcha
  6. Locate the captcha text box by id
  7. Send the user entered value to the located captcha text box
  8. Verify JUnit pane for success result in eclipse IDE

JUnit code for this scenario is,

package com.blog.junitTests;

import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import org.junit.After;
import org.junit.Before;
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;

public class CaptchaValidation {
	//Declaring variables
			private WebDriver driver; 
			private String baseUrl;

			@Before
			public void setUp() throws Exception{
				// Selenium version 3 beta releases require system property set up
				System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
				// Create a new instance for the class FirefoxDriver
				// that implements WebDriver interface
				driver = new FirefoxDriver();
				// Implicit wait for 5 seconds
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				// Assign the URL to be invoked to a String variable
				baseUrl = "https://www.in.ckgs.us/myaccount/";
			}
			
			@Test
			public void testPageTitle() throws Exception{
				// Open baseUrl in Firefox browser window
				driver.get(baseUrl);
				// Locate 'Current Passport Number' text box by cssSelector: tag and name attribute
				WebElement passportNo = driver.findElement(By.cssSelector("input[name='currentPassportNo']"));
				// Clear the default placeholder or any value present
				passportNo.clear();
				// Enter/type the value to the text box
				passportNo.sendKeys("123456789");
				//prompting user to enter captcha			
				String captchaVal = JOptionPane.showInputDialog("Please enter the captcha value:");
				//Type the entered captcha to the text box
	driver.findElement(By.id("recaptcha_response_field")).sendKeys(captchaVal);			
			}
			
			 @After
			  public void tearDown() throws Exception{
				// Close the Firefox browser
				driver.close();
			}
	}

Execution result

The comments are clearly provided for each line of code.

String captchaVal = JOptionPane.showInputDialog("Please enter the captcha value:");

JoptionPane pops up a standard dialog box and showInputDialog prompts user for some input. Once the user enters the displayed captcha and clicks ‘OK’, it will be saved to the string “captchaVal”.

driver.findElement(By.id("recaptcha_response_field")).sendKeys(captchaVal);

This value saved in ‘captchaVal’ will be entered in the captcha text box located by id.

captcha_popup

In JUnit pane, green bar shows that the test case is executed successfully.

captcha_output

Below image shows the final output executed in Firefox browser.

captcha_output_firefox

I got you all covered on captchas! See you again in another post.

Have a great day!

Series Navigation<< 9p. WebDriver – Timesaver/Shortcut: Locator validation9s. WebDriver – Handling text boxes and images >>
By | 2017-07-14T22:04:35+00:00 March 1st, 2017|Selenium|0 Comments

About the Author:

Leave A Comment