Integrating Death by Captcha to automate testing of web pages with captcha

We are all familiar with CAPTCHA — acronym for “Completely Automated Public Turing test to tell Computers and Humans Apart”. A CAPTCHA test is designed to determine if an online user is really a human and not a bot. Through Captcha, the user is asked to perform a certain task, such as enter the text written in the image or click on the images from a set of different images that match the required criteria. CAPTCHA helps protect you from spam and password decryption by asking you to complete a simple test that proves you are human and not a computer trying to break into a password protected account.

However, for web applications with Captcha enabled, automated testing is a challenge as the step involving filling out the Captcha will become a hindrance in the test scenario. Thus, it is essential to efficiently handle Captcha in test automation to ensure that tests run seamlessly without any bottlenecks.

To overcome this, we have integrated a captcha resolution service – Death By Captcha in our test automation using Selenium.

The solution

We went about this using Selenium Webdriver, TestNG, Extent Reporting & Rest Assured and all the code snippets you will see in this post will be in Java.

arch

Steps

The first step is finding the iframe tag of Captcha you want to solve, right click on it an select “Edit as HTML” after you’ve done it you will be able to extract the Sitekey from it.

iframe-tag

The Death By Captcha API requires a URL and a Sitekey. The URL contains the Captcha you want to solve. There are two ways to obtain the Sitekey. The first one is finding the data-sitekey attribute in in the HTML tag of the Captcha element.

data-site-key

It’s now time to make POST request to the URL ‘http://api.dbcapi.me/api/captcha’ using your username, password, captcha type and some other parameters (googlekey, pageurl, proxy and proxytype if you’re using proxy) finally send the request. For this you can use any API testing framework. Here, we have used Rest Assured framework. To start with, you need following maven dependencies to include in your pom.xml:

<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.4.21</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy</artifactId> <version>3.0.8</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-json</artifactId> <version>3.0.8</version> </dependency>

Now to send POST request to URL, one can refer the following code:

Response response = given() .config(RestAssured.config() .encoderConfig(EncoderConfig.encoderConfig() .encodeContentTypeAs(“multipart/form-data”, ContentType.TEXT))) .baseUri(“http://api.dbcapi.me/api/captcha”) .formParam(“authtoken”, dbcAuthToken) .formParam(“type”,“4”).formPara(“token_params”, “{ \”proxy\”:\”\”, \”proxytype\”:\”\”, \”googlekey\”:” + “\”” + googleCaptchaKey + “\”” + “, \”pageurl\”:” + “\”” + targetBaseURL + “\”” + “}”) .when().post();

Note: dbcAuthToken is the authorization token that you will get in your DBC account.
In response, you will receive a token which you will be able to insert into the Captcha field using JS or any other of the methods available. For this example we will use following JavaScript:

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(“document.getElementById(‘g-recaptcha-response’).innerHTML=” + “\”” + response_token + “\”;”);

After this, the Captcha will be marked as solved and you will be redirected to the next page.

We believe handling Captcha in automation testing becoming very important because of how frequently Captcha is used on web pages. We recommend a practice to separate the test cases involving Captcha and run them separately. Additionally, if the tests are not conducted on the live code that is deployed already, then the testers could disable the Captcha module when they perform automated UI testing.

About Death by CAPTCHA

Death by CAPTCHA is a third-party CAPTCHA solving service. This service work by using “human automation” i.e., they use human automation to recognize the CAPTCHA characters and send the result back. It offers an API with an average response time of 9 seconds for normal text CAPTCHAs, with an average accuracy rate of 90% or more. It supports new reCAPTCHA / noCAPTCHA (recaptcha V2 & V3) API support, Confident CAPTCHAs, hCAPTCHAS, Fun CAPTCHAs, Russian & Chinese CAPTCHAs and provides case sensitive support as well.

Written By: Shivangi Gupta