前文《使用AutoIt做广东省英语A级与计算机水平考试的考前测试》介绍的方法在经过多次的测试使用后,觉得仍然有不完善的地方,改进了一点:添加了一个配置文件server.ini用于读取服务器配置,这样可以灵活的对不同的服务器进行测试。因为估计大部分的学校都是准备了多台服务器进行测试或可能还准备了备用服务器,按原来的做法,就必须得每个服务器重新编译一次,比较麻烦。现将编译后的可执行文件及代码打包供下载。
测试程序使用说明:
1.本测试程序供广东省英语与计算机应用水平考试的考前测试使用,实现考前测试的自动登录与自动交卷功能。
2.运行测试程序时,Autologin.exe ,server.ini ,studentid.xls三个文件必须放在一起;
3.autologin.exe文件为测试主程序;
4.server.ini文件为AutoLogin.exe运行时需要的配置信息,如登录考试服务器所需的Ip地址,端口号,考生名单的文件名称以及测试时间(毫秒单位)等,为保证测试的可靠,测试时间请不要少于30分钟;
5.studentid.xls文件保存了考生的登录账号与密码,此文件名称可以更改,但更改后必须在server.ini文件里也做相应的更改。
6.程序运行完毕后,将会删除程序自身及其他两个配置文件,不留垃圾文件,请备份好原始文件。
代码重新黏贴如下:
;###############################################################
;
; Author: badegg
; Email: 11323650@qq.com
; Last Edit: 2013-06-14
;
;###############################################################
#include<IE.au3>
#include<excel.au3>
Global $serverinfo = @ScriptDir & "\server.ini"
If( Not FileExists($serverinfo) ) Then
MsgBox(48,"ERROR","配置文件 "&$serverinfo&" 丢失!")
Exit
EndIf
Dim $ser_ip = IniRead($serverinfo, "serverinfo", "ip", "")
Dim $ser_port = IniRead($serverinfo,"serverinfo","port","")
Dim $test_Time = IniRead($serverinfo,"serverinfo","time","")
Dim $filename = IniRead($serverinfo,"serverinfo","filename","")
Dim $ser_url = "http://" & $ser_ip & ":" & $ser_port
;打开考试服务器的页面
$oIE=_IECreate($ser_url,0,1,1,1)
$oForm= _IEFormGetObjByName($oIE,"login")
$oQueryname= _IEformelementgetobjByname($oForm,"SID")
$oQueryPasswd= _IEFormElementGetObjByName($oForm,"password")
sleep(3000) ; Give it some time to load the web page
;打开包含登录帐号与密码的Excel文件
;Excel文件格式:第一列为机器名称,第二列为登录帐号,第三列为考生姓名,第四列为登录密码
$sFilepath1=@ScriptDir & "\" & $filename ;this file already exist
$oExcel=_excelbookopen($sFilepath1) ;open this file
if @error=1 Then
MsgBox(0,"错误!","不能建立新的工作表.")
Exit
ElseIf @error=2 Then
MsgBox(0,"错误!","找不到指定的文件!")
Exit
EndIf
;转换计算机名称为大写字符
$osname1= StringUpper(@ComputerName)
;从第二行开始遍历Excel文件,提取计算机名称一致的帐号与密码,并填入登录页面的表单中
$i=1
while 1
If _excelreadcell($oExcel,$i,1) Then
$scellValue= _excelreadcell($oExcel,$i,1)
If $scellValue==$osname1 then
$username= _excelreadcell($oExcel,$i,2)
$userpawd= _excelreadcell($oExcel,$i,4)
_IEFormElementSetValue($oQueryname,$username)
_IEFormElementSetValue($oQueryPasswd,$userpawd)
ExitLoop
EndIf
$i=$i+1
EndIf
WEnd
;提交按钮,_IEFormSubim函数的第二个参数必须为0,也就是不等待页面下载完毕,如果设置为1,则有错误。
$slogin=_IEFormSubmit($oForm,0)
;判断提交页面后是否有错误返回,如果没有错误,则关闭Excel文件; _IEFormSubmit函数提交成功后返回值为-1,有错误则为0并设置@error.
;如果将_IEFormSubmit函数第二个参数设置为0,立即返回,不等待页面下载结束,后面的错误类型判断还有没有意义?
If $slogin=-1 Then
_ExcelBookClose($oExcel,0,0) ;close Excel
Else
Switch @error
Case 1
msgbox(0,"Errorinfo","General Error")
case 3
msgbox(0,"Errorinfo","Invalid Data Type")
case 4
msgbox(0,"Errorinfo","Invalid Object Type")
case 6
msgbox(0,"Errorinfo","Load Wait Timeout")
case 8
msgbox(0,"Errorinfo","Access Is Denied")
case 9
msgbox(0,"Errorinfo","Client Disconnected")
case Else
MsgBox(0,"Errorinfo","提交信息有错误!")
EndSwitch
EndIf
;等待35分钟(毫秒单位)
Sleep($test_time)
;自动交卷
If WinActive("考试系统客户端(WEB版)V4.0-《英语应用能力A级》","") Then
WinActivate("考试系统客户端(WEB版)V4.0-《英语应用能力A级》","")
ControlClick("考试系统客户端(WEB版)V4.0-《英语应用能力A级》","","Button7")
if not WinActive("系统信息","交卷后将不能再进入考试,你真的要交卷吗?") Then WinWaitActive("系统信息","交卷后将不能再进入考试,你真的要交卷吗?")
send("!Y")
if not WinActive("系统信息","你的身份证号码是") Then WinWaitActive("系统信息","你的身份证号码是")
ControlClick("系统信息","确定","[CLASS:Button; INSTANCE:1]")
if not WinActive("系统信息","已成功把你的数据上传到服务器") Then WinWaitActive("系统信息","已成功把你的数据上传到服务器")
ControlClick("系统信息","确定","[CLASS:Button; INSTANCE:1]")
ElseIf WinActive("考试系统客户端(WEB版)V4.0-《计算机应用考试模块》","") Then
WinActivate("考试系统客户端(WEB版)V4.0-《计算机应用考试模块》","")
ControlClick("考试系统客户端(WEB版)V4.0-《计算机应用考试模块》","","Button7")
if not WinActive("系统信息","交卷前如果已打开了Word") Then WinWaitActive("系统信息","交卷前如果已打开了Word")
ControlClick("系统信息","是","[CLASS:Button; INSTANCE:1]")
$ExcelPID=ProcessExists("Excel.exe")
$WordPID=ProcessExists("Word.exe")
$PptPID=ProcessExists("Powerpnt.exe")
If $ExcelPID Then ProcessClose($ExcelPID)
If $wordPid Then ProcessClose($wordpid)
If $pptpid then ProcessClose($pptpid)
sleep(5000)
If WinActive("系统信息","请保存打开的OFFICE操作题目") Then ;WinWaitActive("系统信息","请保存打开的OFFICE操作题目")
ControlClick("系统信息","是","[CLASS:Button; INSTANCE:1]")
EndIf
Sleep(5000)
if WinActive("系统信息","交卷后将不能再进入考试,你真的要交卷吗?") Then ;WinWaitActive("系统信息","交卷后将不能再进入考试,你真的要交卷吗?")
send("!Y")
EndIf
sleep(5000)
if not WinActive("系统信息","你的身份证号码是") Then WinWaitActive("系统信息","你的身份证号码是")
ControlClick("系统信息","确定","[CLASS:Button; INSTANCE:1]")
if not WinActive("系统信息","已成功把你的数据上传到服务器") Then WinWaitActive("系统信息","已成功把你的数据上传到服务器")
ControlClick("系统信息","确定","[CLASS:Button; INSTANCE:1]")
Else
MsgBox(0,"Info","无法自动交卷!请手工提交试卷!")
EndIf
;删除脚本程序自身
FileDelete($sFilepath1)
FileDelete(@scriptdir & "\" & $serverinfo)
Run(@ComSpec&' /c ping 127.0.0.1 -n 3&del /q "'&@ScriptFullPath&'"',@ScriptDir,@SW_HIDE)
;退出程序
exit
配置文件截图如下:
学生账号与密码文件截图如下: