《实训外部数据读写操作》由会员分享,可在线阅读,更多相关《实训外部数据读写操作(10页珍藏版)》请在金锄头文库上搜索。
1、软件测试技术实训外部数据读写操作一、实训目标掌握外部文本文件读写操作方法能够使用外部文件中的数据作为数据驱动测试的数据源二、任务描述使用外部文本文件中的数据作为用户登录Flight飞机订票系统的用户名和密码三、覆盖的知识点VBS对文本文件的追加、修改、删除、读取操作四、实训参考步骤I*函数功能:检查文件是否存在输入参数:pathway-文件全路径返回值:如果文件存在,返回True,否则返回False示例调用:MsgBoxCheckFileExists(D:test.txt),*FunctionCheckFileExists(FilePath)dimoFSOSetoFSO=CreateObjec
2、t(Scripting.FileSystemObject)CheckFileExists=oFSO.FileExists(FilePath)SetoFSO=NothingEndFunctionI*函数功能:如果文件夹不存在则创建文件夹输入参数:fldr-文件夹全路径返回值:无示例调用:CallCreatFolderIfNotExist(D:test),*FunctionCreatFolderlfNotExist(fldr)Dimfso,msgSetfso=CreateObject(Scripting.FileSystemObject)IfNot(fso.FolderExists(fldr)Th
3、enSetf=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,F
4、alse)Elseflag=0EndIfFori=1torowcount-1IfNotmyfile.AtEndOfLineThenmyfile.SkipLineEndIfNextIfflag=1ThenWkp 4FW4MUI Tkul4U软件测试技术IfNotmyfile.AtEndOfLineThenReadLine=myfile.ReadLineElseReadLine=越界EndIfmyfile.closeElseReadLine=文件不存在EndIfEndFunction,*函数功能:计算文本文件总行数输入参数:FileName-文件全路径返回值:该文本文件总行数示例调用:MsgBox
5、NumberOfLines(c:c.txt)*FunctionNumberOfLines(FileName)DimlineCountlineCount=0SetobjFSO=CreateObject(Scripting.FileSystemObject)SetobjTextFile=objFSO.OpenTextFile(FileName,1)DoUntilobjTextFile.AtEndOfStreamRedimPreservearrFileLines(lineCount)arrFileLines(lineCount)=objTextFile.ReadLinelineCount=lineC
6、ount+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.F
7、ileSystemObject)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)
8、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-目标字符
9、串返回值:无示例调用: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,waySetfileSyst
10、emObj=CreateObject(Scripting.FileSystemObject)fileSpec=pathwaySetlogFile=fileSystemObj.OpenTextFile(filePath,2,true)logFile.WriteLine(CStr(temper)logFile.CloseSetlogFile=NothingEndFunction函数功能:在指定行之间插入一行输入参数:fileFullPath-文件全路径lineFrom/lineTo指定行content-插入文本内容返回值:无示例调用:callinsertLineBetween(c:bsmain_r
11、untime.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(fileFull
12、Path,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