The intent of this blog is to show you an advanced captcha automation class using Selenium RC. So far you must have seen captcha's that only does addition or subtraction. What if in future, multiplication or division captcha's are also introduced. You regression automation script will fail under those circumstances.
This advanced captcha automation class hadles the following operators:
1) Addition (+)
2) Subtration (-)
3) Multiplication (*)
4) Division (/)
5) Modulus (%)
This class is quite easy to understand. Complete code is as follows:
package MyPack;
import java.lang.String;
//This class is responsible for performing the captcha calculations.
public class CalcCaptcha {
int firstWhiteSpaceIndex;
int nextWhiteSpaceIndex;
int lastWhiteSpaceIndex;
String captchaString;
public CalcCaptcha()
{
firstWhiteSpaceIndex = 0;
nextWhiteSpaceIndex = 0;
lastWhiteSpaceIndex = 0;
captchaString = "";
}
public CalcCaptcha(String str)
{
captchaString = str;
}
public String CalculateCaptchaResult()
{
firstWhiteSpaceIndex = captchaString.indexOf(" ");
//+1 from firstWhiteSpaceIndex to get operator
//+1 again for white space after operator so +2(1+1)
nextWhiteSpaceIndex = firstWhiteSpaceIndex + 2;
lastWhiteSpaceIndex = captchaString.lastIndexOf(" ");
String leftOperand = captchaString.substring(0, firstWhiteSpaceIndex);
String rightOperand = captchaString.substring((nextWhiteSpaceIndex+1),lastWhiteSpaceIndex);
int leftNumber = Integer.parseInt(leftOperand),
rightNumber = Integer.parseInt(rightOperand),
result = 0;
//The operator is always next to first white space, thus +1.
char operator = captchaString.charAt((firstWhiteSpaceIndex+1));
switch(operator)
{
case '+':
{
result = leftNumber + rightNumber;
break;
}
case '-':
{
//assuming that leftNumber is always greater than rightNumber.
result = leftNumber - rightNumber;
break;
}
case '*':
{
result = leftNumber * rightNumber;
break;
}
case '/':
{
//division by zero exception handled.
result = leftNumber / rightNumber;
break;
}
case '%':
{
result = leftNumber % rightNumber;
break;
}
}
String final_value = String.valueOf(result);
return final_value;
}
}
How to use this class in Selenium RC is shown below:
package MyPack;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class TestCaptcha
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Selenium selenium = null;
try
{
selenium = new DefaultSelenium("localhost",4444,"firefox","http://timesofindia.indiatimes.com");
selenium.start();
selenium.open("/poll.cms");
selenium.windowMaximize();
selenium.windowFocus();
String str = selenium.getText("//*[@id='mathq2']");
CalcCaptcha obj = new CalcCaptcha(str);
String final_value = obj.CalculateCaptchaResult();
selenium.type("//*[@id='mathuserans2']", final_value);
}
catch(RuntimeException obj)
{
//Catch division by zero exception.
System.out.println("Exception raised is: " + obj);
}
//selenium.close();
//selenium.stop(); // RC Window
}
}
Comments are welcome.
Thanks :)
This advanced captcha automation class hadles the following operators:
1) Addition (+)
2) Subtration (-)
3) Multiplication (*)
4) Division (/)
5) Modulus (%)
This class is quite easy to understand. Complete code is as follows:
package MyPack;
import java.lang.String;
//This class is responsible for performing the captcha calculations.
public class CalcCaptcha {
int firstWhiteSpaceIndex;
int nextWhiteSpaceIndex;
int lastWhiteSpaceIndex;
String captchaString;
public CalcCaptcha()
{
firstWhiteSpaceIndex = 0;
nextWhiteSpaceIndex = 0;
lastWhiteSpaceIndex = 0;
captchaString = "";
}
public CalcCaptcha(String str)
{
captchaString = str;
}
public String CalculateCaptchaResult()
{
firstWhiteSpaceIndex = captchaString.indexOf(" ");
//+1 from firstWhiteSpaceIndex to get operator
//+1 again for white space after operator so +2(1+1)
nextWhiteSpaceIndex = firstWhiteSpaceIndex + 2;
lastWhiteSpaceIndex = captchaString.lastIndexOf(" ");
String leftOperand = captchaString.substring(0, firstWhiteSpaceIndex);
String rightOperand = captchaString.substring((nextWhiteSpaceIndex+1),lastWhiteSpaceIndex);
int leftNumber = Integer.parseInt(leftOperand),
rightNumber = Integer.parseInt(rightOperand),
result = 0;
//The operator is always next to first white space, thus +1.
char operator = captchaString.charAt((firstWhiteSpaceIndex+1));
switch(operator)
{
case '+':
{
result = leftNumber + rightNumber;
break;
}
case '-':
{
//assuming that leftNumber is always greater than rightNumber.
result = leftNumber - rightNumber;
break;
}
case '*':
{
result = leftNumber * rightNumber;
break;
}
case '/':
{
//division by zero exception handled.
result = leftNumber / rightNumber;
break;
}
case '%':
{
result = leftNumber % rightNumber;
break;
}
}
String final_value = String.valueOf(result);
return final_value;
}
}
How to use this class in Selenium RC is shown below:
package MyPack;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class TestCaptcha
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Selenium selenium = null;
try
{
selenium = new DefaultSelenium("localhost",4444,"firefox","http://timesofindia.indiatimes.com");
selenium.start();
selenium.open("/poll.cms");
selenium.windowMaximize();
selenium.windowFocus();
String str = selenium.getText("//*[@id='mathq2']");
CalcCaptcha obj = new CalcCaptcha(str);
String final_value = obj.CalculateCaptchaResult();
selenium.type("//*[@id='mathuserans2']", final_value);
}
catch(RuntimeException obj)
{
//Catch division by zero exception.
System.out.println("Exception raised is: " + obj);
}
//selenium.close();
//selenium.stop(); // RC Window
}
}
Thanks :)