自動化テスト、javasriptはJSONオブジェクトとXMLオブジェクトを遍歴しています.


web開発で自動化テストを行う場合、通常はテストの予想結果を定義する必要があります.jsがajax要求を実行した後、戻り結果と予想される結果を比較して試験が合格したかどうか判断します.
テストの結果は、一般的にjson形式またはxml形式ですので、jsonとxmlの解析が必要です.
解析JSON-JSONエルゴード
//  JSON         
function compareJSONResult(expectResult, returnResult){
	var type = typeof expectResult;
	if(type=='object'){
		//   
		if(typeof expectResult !='object'){
			return false;
		}
		
		if(expectResult instanceof Array){
			//        
			if(!(returnResult instanceof Array)){
				return false;
			}
			var expectNum = expectResult.length;
			var returnNum = returnResult.length;
			if(expectNum!=returnNum){
				return false;
			}
			for(var i =0; i< expectNum; i++){
				var expectName = expectResult[i];
				var returnName = returnResult[i];
				if(!compareJSONResult(expectName, returnName)){
					return false;
				}
			}
			return true;
		}else{
			//     
			for(var expectName in expectResult){
				var expectValue = expectResult[expectName];
				var foundMatchName = false;
				var returnValue;
				for(var returnName in returnResult){
					if(expectName==returnName){
						foundMatchName = true;
						returnValue = returnResult[expectName];
						break;
					}
				}
				if(!foundMatchName){
					return false;
				}else{
					if(!compareJSONResult(expectValue,returnValue)){
						return false;
					}
				}
			}
			return true;
		}
	}else{
		//      
		return expectResult==returnResult;
	}
}
解析XML-XMLエルゴード
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="html">//  XML         
function compareXMLResult(expectResult, returnResult){
	if(expectResult.hasOwnProperty()){
		if(!returnResult.hasOwnProperty()){
			return false;
		}
		var expectAttributes =expectResult.attributes;
		var returnAttributes =returnResult.attributes;
		if(expectAttributes.length!=returnAttributes.length){
			return false;
		}
		for(var i=0; i< expectAttributes.length;i++){
			var expeAttr = expectAttributes[i];
			var retAttr ;
			var isFound=false;
			for(var j=0; j<returnAttributes.length;j++){
				retAttr = returnAttributes[j];
				if(expeAttr.name==retAttr.name){
					isFound = true;
					break;
				}
			}
			if(isFound){
				if(expeAttr.value!=retAttr.value){
					return false
				}
			}else{
				return false;
			}
		}
	}
	if(expectResult.hasChildNodes()){
		if(!returnResult.hasChildNodes()){
			return false;
		}
		for(var i=0; i<expectResult.childNodes.length;i++){
			var expectNode = expectResult.childNodes[i];
			var returnNode = returnResult.childNodes[i];
			if(!compareXMLResult(expectNode,returnNode)){
				return false;
			}
		}
		return true;
	}else{
		if(expectResult.nodeValue!=returnResult.nodeValue){
			return false;
		}else{
			return true;
		}
	}
}
 
xml domより多くの情報http://www.w3school.com.cn/xmldom/index.asp
転載を歓迎します.転載は出所を明記してください.http://blog.csdn.net/musa875643dn/article/details/50938308