日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚本识别的难度也越来越高。这对我们自动化脚本编写带了非常的不便,那么如何解决登录时的验证码问题呢?经常有初学自动化脚本编写的小主们问及此问题。
此文主要针对如何解决自动化测试脚本中含登录态的操作问题,即如何降低验证码对自动化脚本编写过程中的解决方法进行分析和解决,并以实例演示(基于易迅网易迅账号登录)。解决验证码的问题,一般有以下几种方法:
1、 验证码识别技术(OCR)。通过验证码识别技术,识别登录过程中出现的验证码。此种方法不建议使用,浪费大量的人力物力,得不偿失。
2、万能验证码。例如:规定接收到的 ”aaron“ 为万能验证码,若服务器接收到了此验证码,则返回正确登录信息。此方法的缺陷是无法在线上使用,因为无法保证万能验证码的安全性。因而此方法一般应用于测试环境。
3、Cookie 越界登录。通过将用户登录的 cookie 信息添加至浏览器,直接越过登录操作(间接的跳过验证码输入),将用户的登录态添加到浏览器,即可进行相应的登录态用户操作。虽然此种方法有一定的局限性(受 cookie 的有效时间限制),俺还是比较推崇此种方法。
上述第一种方法,非常简单,在此就不再赘述了;第二种方法,有兴趣的小主大牛们可以尝试一下。下面主要以第三种方法为实例演示,基于易迅网易迅账号登录的 cookie 添加。
闲话少述,小二上码。。。敬请各位小主参阅,希望能对您在日常的 WebUI 自动化脚本编写有一定的启发和帮助。若有不足或错误之处,敬请大神指正,不胜感激!
1
/**
2
* Aaron.ffp Inc.
3
* Copyright (c) 2004-2015 All Rights Reserved.
4
*/
5
package
main.aaron.demo.cookie;
6
7
import
java.io.BufferedReader;
8
import
java.io.File;
9
import
java.io.FileReader;
10
import
java.util.Date;
11
12
import
main.aaron.sele.core.TestCase;
13
14
import
org.openqa.selenium.Cookie;
15
import
org.testng.Assert;
16
import
org.testng.annotations.AfterClass;
17
import
org.testng.annotations.Test;
18
19
/**
20
* Add cookie to web browser
21
*
22
*
@author
Aaron.ffp
23
*
@version
V1.0.0: autoSeleniumDemo main.aaron.demo.cookie UseCookieLogin.java, 2015-6-19 15:53:53 Exp $
24
*/
25
public
class
UseCookieLogin
extends
TestCase{
26
private
final
String f_cookie =
this
.PROJECTHOME +
this
.FILESEPARATOR + "cookie" +
this
.FILESEPARATOR + "browser.data"
;
27
private
String baseUrl = "http://www.yixun.com/"
;
28
29
@Test
30
public
void
useCookieLogin(){
31
this
.webdriver.get(
this
.baseUrl);
32
33
try
{
34
//
clean the invalid cookie
35
this
.webdriver.manage().deleteAllCookies();
36
37
//
create file object
38
File f =
new
File(
this
.f_cookie);
39
40
//
read file
41
FileReader fr =
new
FileReader(f);
42
BufferedReader br =
new
BufferedReader(fr);
43
44
//
store row cookie info
45
String line;
46
47
//
add cookie to web browser
48
while
((line = br.readLine()) !=
null
) {
49
String[] item = line.split(";"
);
50
51
if
(item.length != 6
) {
52
continue
;
53
}
54
55
for
(
int
i = 0; i < item.length; i++
) {
56
String name = item[0
];
57
String value = item[1
];
58
String domain = item[2
];
59
String path = item[3
];
60
Date expiry =
null
;
61
boolean
isSecure =
new
Boolean(item[5
]);
62
63
//
create cookie object
64
Cookie cookie =
new
Cookie(name, value, domain, path, expiry, isSecure);
65
66
//
add cookie to web browser
67
this
.webdriver.manage().addCookie(cookie);
68
}
69
}
70
71
br.close();
72
}
catch
(Exception e) {
73
e.printStackTrace();
74
Assert.fail((e.getMessage() ==
null
)? ""
:e.getMessage());
75
}
76
77
this
.webdriver.get(
this
.baseUrl);
78
}
79
80
@AfterClass
81
public
void
afterClass(){
82
this
.webdriver.close();
83
this
.webdriver.quit();
84
}
85
}
关于如何获取登录的 cookie 信息数据并保存,请参阅 Selenium2学习-017-WebUI自动化实战实例-015-获取浏览器中的 cookie 信息
PS:小主们在应用以上源码时,需要将源码中的 cookie 文件位置修改为本地合法路径才可:private final String f_cookie = this.PROJECTHOME + this.FILESEPARATOR + "cookie" + this.FILESEPARATOR + "browser.data";
至此, WebUI 自动化功能测试脚本 第 016-自动化脚本编写过程中的登录验证码问题 顺利完结,希望此文能够给初学 Selenium 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

