实训外部数据读写操作
软件测试技术实训外部数据读写操作一、实训目标掌握外部文本文件读写操作方法能够使用外部文件中的数据作为数据驱动测试的数据源二、任务描述使用外部文本文件中的数据作为用户登录Flight飞机订票系统的用户名和密码三、覆盖的知识点VBS对文本文件的追加、修改、删除、读取操作四、实训参考步骤I*'函数功能:检查文件是否存在'输入参数:'pathway-文件全路径返回值:'如果文件存在,返回True,否则返回False'示例调用:'MsgBoxCheckFileExists("D:test.txt"),*FunctionCheckFileExists(FilePath)dimoFSOSetoFSO=CreateObject("Scripting.FileSystemObject")CheckFileExists=oFSO.FileExists(FilePath)SetoFSO=NothingEndFunctionI*'函数功能:如果文件夹不存在则创建文件夹'输入参数:'fldr-文件夹全路径返回值:无'示例调用:'CallCreatFolderIfNotExist("D:test"),*FunctionCreatFolderlfNotExist(fldr)Dimfso,msgSetfso=CreateObject("Scripting.FileSystemObject")IfNot(fso.FolderExists(fldr)ThenSetf=fso.CreateFolder(fldr)EndIfEndFunction*'函数功能:读取指定行内容'输入参数:'pathway-文件全路径'rowcount-行数返回值:该行内容'示例调用:'MsgBoxReadLine("c:c.txt",2)*FunctionReadLine(pathway,rowcount)Dimfso,myfile,i,flagflag=1Setfso=CreateObject("scripting.FileSystemObject")Iffso.FileExists(pathway)ThenSetmyfile=fso.openTextFile(pathway,1,False)Elseflag=0EndIfFori=1torowcount-1IfNotmyfile.AtEndOfLineThenmyfile.SkipLineEndIfNextIfflag=1ThenWkp 4FW4MUI Tkul4U软件测试技术IfNotmyfile.AtEndOfLineThenReadLine=myfile.ReadLineElseReadLine="越界"EndIfmyfile.closeElseReadLine="文件不存在"EndIfEndFunction,*函数功能:计算文本文件总行数'输入参数:'FileName-文件全路径返回值:'该文本文件总行数'示例调用:'MsgBoxNumberOfLines("c:c.txt")*FunctionNumberOfLines(FileName)DimlineCountlineCount=0SetobjFSO=CreateObject("Scripting.FileSystemObject")SetobjTextFile=objFSO.OpenTextFile(FileName,1)DoUntilobjTextFile.AtEndOfStreamRedimPreservearrFileLines(lineCount)arrFileLines(lineCount)=objTextFile.ReadLinelineCount=lineCount+1LoopNumberOfLines=UBound(arrFileLines)objTextFile.CloseEnd Function软件测试技术I*函数功能:向文本文件追加行'输入参数:'pathway-文件全路径'words-新行内容返回值:无'示例调用:'callWriteFile_Append("D:test.txt","HelloWorld"),*PublicFunctionWriteFile_Append(pathway,words)DimfileSystemObj,fileSpec,logFile,waySetfileSystemObj=CreateObject("Scripting.FileSystemObject")fileSpec=pathwaySetlogFile=fileSystemObj.OpenTextFile(fileSpec,8,true)logFile.WriteLine(CStr(words)logFile.CloseSetlogFile=NothingEndFunction*'函数功能:改写文本文件所有内容'输入参数:'pathway-文件全路径'words-文本内容返回值:无'示例调用:'callWriteFile_Change("D:test.txt","HelloWorld")*PublicFunctionWriteFile_Change(pathway,words)DimfileSystemObj,fileSpec,logFile,waySetfileSystemObj=CreateObject("Scripting.FileSystemObject")fileSpec=pathway软件测试技术SetlogFile=fileSystemObj.OpenTextFile(fileSpec,2,True)logFile.WriteLine(CStr(words)logFile.CloseSetlogFile=NothingEndFunction,*函数功能:全部替换文本文件中指定字符串'输入参数:'filepath-文件全路径'from-被改写字符串'too-目标字符串返回值:无'示例调用:'callfileReplace("c:source.txt","2008114","test")*FunctionfileReplace(filepath,from,too)Dimfso,myfileSetfso=CreateObject("scripting.FileSystemObject")Setmyfile=fso.openTextFile(filePath,1,false)cc=myfile.ReadAllmyfile.Closetemper=Replace(cc,from,too)DimfileSystemObj,fileSpec,logFile,waySetfileSystemObj=CreateObject("Scripting.FileSystemObject")fileSpec=pathwaySetlogFile=fileSystemObj.OpenTextFile(filePath,2,true)logFile.WriteLine(CStr(temper)logFile.CloseSetlogFile=NothingEndFunction'函数功能:在指定行之间插入一行'输入参数:'fileFullPath-文件全路径'lineFrom/lineTo指定行'content-插入文本内容返回值:无'示例调用:'callinsertLineBetween("c:bsmain_runtime.txt”,3,4,"TestLine"),*FunctioninsertLineBetween(fileFullPath,lineFrom,lineTo,content)DimtempBefore(),tempAfter(),lineCount,ilineCount=NumberOfLines(fileFullPath)ReDimtempBefore(CInt(lineFrom)-1)ReDimtempAfter(CInt(lineCount)-CInt(lineTo)+1)Fori=1ToCInt(lineFrom)tempBefore(i-1)=ReadLine(fileFullPath,i)NextFori=1ToCInt(lineCount)-CInt(lineTo)+1tempAfter(i-1)=ReadLine(fileFullPath,(i+CInt(lineTo)-1)NextCallWriteFile_Change(fileFullPath,tempBefore(0)Fori=2ToCInt(lineFrom)CallWriteFile_Append(fileFullPath,tempBefore(i-1)NextCallWriteFile_Append(fileFullPath,content)Fori=0ToCInt(lineCount)-CInt(lineTo)+1IftempAfter(i)<>"越界"thenCallWriteFile_Append(fileFullPath,tempAfter(i)EndifNextEndFunctionI*软件测试技术函数功能:替换文本文件指定行'输入参数:'fileFullPath-文件全路径'line-行数'content-替换为返回值:无'示例调用:'callReplaceLineWith("c:bsmain_runtime.txt",5,"TestLine"),*FunctionReplac