web测试,vbs自动登陆脚本批量生成器

web测试,vbs自动登陆脚本批量生成器_学海无涯


做这个东西的原因以及其功能:

我们的web项目是个进销存系统,有丰富的权限控制和业务流程,因此测试的时候需要以指定角色的用户名登陆去测试,这时候,要反复输入各用户的用户名和密码就变得非常琐碎了,用户名难以记住,手工输入密码容易出错,另外登陆密码经常变更并且可能各不相同……

我的这些vbs脚本就能很好解决这些问题,首先《模板.vbs》可以自动登陆并且可以自动尝试url、密码,做到判断服务器是否开启、哪个url可用、哪个密码正确,当密码变更或都不对的时候有提示,当尝试出正确的url/密码的时候自动覆盖脚本自身内容,设置正确的url/密码以便下次使用;其次《生成.vbs》能够根据列出的用户名列表批量生成指定文件名、登陆帐号的登陆脚本。

一.使用说明

1.按需要修改《pwd.txt》添加测试环境的密码;修改模板文件《模板.vbs》中的url;修改完后双击测试是否正常
totalurls=totalurls+1:ReDim Preserve urls(totalurls):urls(totalurls)="http://192.77.11.122:5858/"

2.修改《userAndFileName.txt》指定要生成测试帐号的,如……其中,“=”前面的为帐号后面的为脚本文件名。
----------------------------------------------------------------
sa=超管

3.双击《生成.vbs》生成自动登陆脚本,脚本在目录“自动登陆脚本”里面。


特别提醒:
脚本文件将会默认读取 D:\pwd.txt 的密码,如果没有该密码文件将在当前目录和上层目录搜索 pwd.txt
因此请不要随便移动密码文件 pwd.txt
每次有新的密码请用添加而不是修改密码的方式。

==========================================================================

二.自动登陆模板,文件名《模板.vbs》,代码如下

'1.修改《模板.vbs》中的url、pwd.txt的候选密码、userAndFileName.txt中的帐号和脚本文件名
'2.双击《生成.vbs》生成

Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set ie = Wscript.CreateObject("InternetExplorer.Application")


'-------------------------------------------------------------------------------------------------
'可选url
Dim urls():totalurls=0
totalurls=totalurls+1:ReDim Preserve urls(totalurls):urls(totalurls)="http://192.77.11.122:5858/"'urls(totalurls)=readTxtFile("url.txt")
'totalurls=totalurls+1:ReDim Preserve urls(totalurls):urls(totalurls)="http://localhost:8080/drp/"
'totalurls=totalurls+1:ReDim Preserve urls(totalurls):urls(totalurls)="http://localhost/"
'totalurls=totalurls+1:ReDim Preserve urls(totalurls):urls(totalurls)="http://localhost/drp/"
'totalurls=totalurls+1:ReDim Preserve urls(totalurls):urls(totalurls)="http://localhost:8080/"

'首页首选项
Dim currentUrl:currentUrl=1
'保证不越界
If currentUrl>UBound(urls) Then
Call reWriteVbs("Dim currentUrl:currentUrl="+CStr(currentUrl),"Dim currentUrl:currentUrl="++CStr(UBound(urls)))
currentUrl=UBound(urls)
End If

'-------------------------------------------------------------------------------------------------
'可修改模板中的用户名,但不要加换行符号
Dim username:username="sa"
'-------------------------------------------------------------------------------------------------
'可选密码
'Dim passwords():totalPwds=0
'totalPwds=totalPwds+1:ReDim Preserve passwords(totalPwds):passwords(totalPwds)="000000"
'totalPwds=totalPwds+1:ReDim Preserve passwords(totalPwds):passwords(totalPwds)="333333"

'读取文件中的候选密码(文本文件,每行一个密码)
defaultPWDFile="D:\pwd.txt"
If fso.FileExists(defaultPWDFile)=False Then
If fso.FileExists("pwd.txt")=True Then
   defaultPWDFile="pwd.txt"
Else
   If fso.FileExists("../pwd.txt")=True Then
    defaultPWDFile="../pwd.txt"
   Else
    MsgBox "密码文件指定错误,当前目录也没有密码文件,脚本将不能正常运行"
   End If
End If
End If
Dim passwords,pwdStr:pwdStr=readTxtFile(defaultPWDFile):passwords=Split(pwdStr,vbCrLf)
'For idx = 1 To UBound(passwords):MsgBox passwords(idx):Next

'帐号首选项
Dim currentPwd:currentPwd=5
'保证不越界
If currentPwd>UBound(passwords) Then
Call reWriteVbs("Dim currentPwd:currentPwd="+CStr(currentPwd),"Dim currentPwd:currentPwd="++CStr(UBound(passwords)))
currentPwd=UBound(passwords)
End If
'-------------------------------------------------------------------------------------------------

'尝试登陆【自动尝试,自动变更脚本文件】
Function tryServer()
loginRst=openServer(urls(currentUrl))

If loginRst=False Then   
   For idx = 1 To UBound(urls)   
    If currentUrl<>idx Then
     loginRst=openServer(urls(idx))
     If loginRst=True Then  
      MsgBox "将自动更改首页首选项为 "+urls(idx)
      Call reWriteVbs("Dim currentUrl:currentUrl="+CStr(currentUrl),"Dim currentUrl:currentUrl="++CStr(idx))
      Exit For
     End If
    End If
   Next   
End If

If loginRst=False Then
   MsgBox "无法显示网页或网页不正常,服务暂停或首页有问题?"
   ie.Visible=1
Else
   toLogin
End If
End Function


Sub toLogin
   tryLogin()
   ie.Visible=1
End Sub

'尝试打开首页
Function openServer(url)
ie.navigate url
SynchronizeIE()
If InStr(ie.Document.body.innerHTML,"无法显示网页") >0 Or InStr(ie.Document.body.innerHTML,"form1")<1 Then
   openServer=False
Else
   openServer=True
End If
End Function

'尝试登陆【自动尝试,自动变更脚本文件】
Function tryLogin()
loginRst=login(currentPwd)

If loginRst=False Then
   For idx = 1 To UBound(passwords) Step 1
    If currentPwd<>idx Then
     loginRst=login(idx)
     If loginRst=True Then
      'temp = msgbox("用户名、密码已经变更为password="+passwords(idx)+",是否自动修改登陆脚本,设置当前成功登陆的帐号为首选项。",32+4)
      'If temp=vbYes Then
      If True Then
       MsgBox "密码已经变更为:password="+passwords(idx)+",下次登陆将优先使用此密码登陆。"
       Call reWriteVbs("Dim currentPwd:currentPwd="+CStr(currentPwd),"Dim currentPwd:currentPwd="++CStr(idx))
      End if
   
      Exit Function
     End If
    End If
   Next
   MsgBox "密码已变更,并且不在预想的可能之中。"
End If
End Function


'替换关键内容后重写自身
Function reWriteVbs(keyForm,keyTo)
strPath = Wscript.ScriptFullName
content=readTxtFile(strPath)
content = ReplaceAll(content,keyForm,keyTo)
Call createNewFile(strPath,content)  
End Function


'用指定索引的用户名、密码登陆
Function login(idx)
Set doc=ie.Document
doc.form1.username.value=username
doc.form1.password.value=passwords(idx)
doc.form1.submit()

SynchronizeIE()  

If InStr(ie.Document.body.innerHTML,"登录失败,请输入正确的用户名和密码!") < 1 Then
   login=True
Else
   login=False
End If
End Function

'Do
'Loop While (InStr(ie.Document.body.innerHTML,"登录失败,请输入正确的用户名和密码!") >0)

'//等待IE操作结束。
Function SynchronizeIE()
    While ie.Busy
          WScript.Sleep(100)
    Wend
End Function

'创建文件,存在则覆盖
Public function createNewFile(dscFile, msgs)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim f
Set f = fso.OpenTextFile(dscFile, ForWriting, True)
f.Write msgs
f.Close
End Function

'读运文本文件内容
Private Function readTxtFile(file_path)
Const ForReading = 1, ForWriting = 2
Dim MyFile
Set MyFile = fso.OpenTextFile(file_path, ForReading)
readTxtFile = MyFile.ReadAll()
MyFile.Close
End Function

Function ReplaceAll(content,patrn,replStr)
Dim regEx
Set regEx = New RegExp      ' 建立正则表达式。
regEx.Pattern = patrn      ' 设置模式。
regEx.IgnoreCase = True      ' 设置是否区分大小写。
ReplaceAll = regEx.Replace(content,replStr)    ' 作替换。
End Function

tryServer()

==========================================================================

三、批量生成器,文件名《生成.vbs》,代码

Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set ie = Wscript.CreateObject("InternetExplorer.Application")


'读取模板文件
Dim templateStr:templateStr=readTxtFile("模板.vbs")

Dim userFilename,tmpStr,filename,currentUsername
'读取文件中的用户名和文件名
Dim rows,rowStr:rowStr=readTxtFile("userAndFileName.txt"):rows=Split(rowStr,vbCrLf)

'保存自动生成的脚本的目录
Dim foldName:foldName="自动登陆脚本"
If fso.FolderExists(foldName)=False Then
fso.CreateFolder(foldName)
Else
Dim fold:Set fold = fso.GetFolder(foldName)'文件对象一定要定义、set
If fold.files.Count > 0 Then
   For Each af In fold.files
    fso.DeleteFile(af)
   Next
end If
End If

'查找模板中的用户名
iBg=InStr(templateStr,"Dim username:username="+Chr(34))+Len("Dim username:username="+Chr(34))
iEnd=InStr(iBg,templateStr,Chr(34))
currentUsername=Mid(templateStr,iBg,iEnd-iBg)

vbsNums=0'生成的脚本数目
For idx = 0 To UBound(rows)
'MsgBox rows(idx)
If InStr(rows(idx),"=") >0 Then
   userFilename=Split(rows(idx),"=")
   tmpStr = ReplaceAll(templateStr,"Dim username:username="+Chr(34)+currentUsername+Chr(34),"Dim username:username="+Chr(34)+userFilename(0)+Chr(34))
   filename=foldName+"\"+userFilename(1)+".vbs"
   'If fso.FileExists(filename)=True Then:fso.DeleteFile(filename):End If
   createNewFile filename,tmpStr
   vbsNums=vbsNums+1
End If
Next
'MsgBox CStr(vbsNums)+"个自动登陆脚本已经生成好了"


'替换关键内容后重写自身
Function reWriteVbs(keyForm,keyTo)
strPath = Wscript.ScriptFullName
content=readTxtFile(strPath)
content = ReplaceAll(content,keyForm,keyTo)
Call createNewFile(strPath,content)  
End Function

'创建文件,存在则覆盖
Public function createNewFile(dscFile, msgs)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim f
Set f = fso.OpenTextFile(dscFile, ForWriting, True)
f.Write msgs
f.Close
End Function

'读运文本文件内容
Private Function readTxtFile(file_path)
Const ForReading = 1, ForWriting = 2
Dim MyFile
Set MyFile = fso.OpenTextFile(file_path, ForReading)
readTxtFile = MyFile.ReadAll()
MyFile.Close
End Function

Function ReplaceAll(content,patrn,replStr)
Dim regEx
Set regEx = New RegExp      ' 建立正则表达式。
regEx.Pattern = patrn      ' 设置模式。
regEx.IgnoreCase = True      ' 设置是否区分大小写。
ReplaceAll = regEx.Replace(content,replStr)    ' 作替换。
End Function

==========================================================================

四、用户名列表,文件名《userAndFileName.txt》,内容按需填写,每行一用户名,如

sa

user_a

==========================================================================

五、登陆密码,以便自动尝试密码,文件名《pwd.txt》

评论

此博客中的热门博文

时间管理:每天挤出一小时

你的妈妈已经等了二十几年

教您如何认识植柔皮,头层皮革,修面皮革,油蜡皮、水染皮、摔纹皮、纳帕皮、打蜡皮、压花皮、修面皮、漆光皮、磨砂皮、贴膜皮