QA Sanity : Code Difference between different Environments

QA Sanity : Code Difference between different Environments

Often in testing one comes across a situation where the Build is deployed from one environment to another . Now there is a lot of effort involved in verifying different pages and validating that the functionality is working fine as always.

How can we decrease this effort?

I am using the word decrease because you will always want one quick Human look on the new environment. Its not wise to have a 100 % Automated solution. But thats a different discussion all together.

Methods :

  • The usual answer : Selenium / QTP / Other Automated solutions.

While this is usually the most trusted solution but there needs to be a better approach as well .Automation suite runs on a whole website can take time and the analysis of their reports takes a lot of time as well. What if there is little time available for to ready the UAT Environment?

  • Another approach is Preparing a list of pages of the entire website using crawlers like xenu or from your own sitemap and then comparing that list of pages across two environments :

Visual Compare

We can use something like Image Diff after taking screenshots from both the environments on the list of url's supplies. We can achieve this by using both java and Angular JS. There are already solutions available for this online but I will try to explain a code snippet later on for this as well.

Code Compare

How about comparing the code itself across two branches ? We can Extract the HTML source from both Environments and store them in a temporary file.Now we can easily compare these two files and list down the differences.This is something done by a software called kaleidoscope.


To ensure a line by line difference we will also need to make sure we exclude sections / blocks of code which are auto included in some environment dependencies.We will start by listing down all the URL's to be tested in a txt file like InputURL.txt and then compare each url for both the environments in loop fashion.

  1. You Will have to do a bunch of declarations like :
	      File inputFile = new File(System.getProperty("user.dir")+"/textFile/inputURL.txt");
	      File outputFile =new File(System.getProperty("user.dir")+"/textFile/outputURL.txt");
	      File HtmlSource1= new File(System.getProperty("user.dir")+"/textFile/File1.txt");
	      File HtmlSource2= new File(System.getProperty("user.dir")+"/textFile/File2.txt");
	      File ignoreTextFile =new File(System.getProperty("user.dir")+"/textFile/ignore_Text.txt");
	      File ignoreCodeFile =new File(System.getProperty("user.dir")+"/textFile/ignore_Code.txt");
	      File codeDiffReport = new File(System.getProperty("user.dir") + "/htmlReport/QA-Branch-Code-Diff.html");

2. Fetch The User Choice in terms of branches

				System.out.println("Do you want to compare the html code between branches? (y or n)");
					 String FlagcodeDiff= console.readLine().toString().toUpperCase();
	//				 String FlagcodeDiff= in.nextLine().toString().toUpperCase(); 
			 
					 /*
					  * Initiate HTML report Creation
					  * 
					  */
					 
				        if (FlagcodeDiff.equals("Y")){
				        	System.out.println("Enter the first branch e.g ww01");
				        	branch1 = console.readLine();
			//				branch1 = in.nextLine();
				        	System.out.println("Enter the second branch e.g. ww02");
				            branch2 = console.readLine();
					//		 branch2 = in.nextLine();
							 commonFunctions.initiateHTMLReport(htmlStringBuilder, "URL Comparison"); 
							 

				        }


3. Loop through the list of URL's from the input file using something similar to below :

       while((url = reader.readLine()) != null) { 
	        	Document document = Jsoup
	        		    .connect(url)
	        		    .timeout(0).ignoreHttpErrors(true)
	        		    .header("Authorization", "Basic " + base64login)
	        		    .get();
	        System.out.println("Analysis of URL : "+url);
	        writer.write("Analysis of URL : "+url);
	        writer.newLine();

4. Save the code from both branches and remove pre decided code blocks / texts

	
	public static void testBranchCodeDiff(File HtmlSource1,File HtmlSource2,File ignoreTextFile,File ignoreCodeFile,String url,String branch1,String branch2,String base64login,File codeDiffReport,StringBuilder htmlStringBuilder) throws IOException{
		
		String textToIgnore;
		String codeToIgnore;
		FileWriter fw1= new FileWriter(HtmlSource1.getAbsoluteFile());
	      BufferedWriter writer1 = new BufferedWriter(fw1);
	      
	      FileWriter fw2= new FileWriter(HtmlSource2.getAbsoluteFile());
	      BufferedWriter writer2 = new BufferedWriter(fw2);
	      
	      BufferedReader ignoreTextReader = new BufferedReader(new FileReader(ignoreTextFile));
	      BufferedReader ignoreCodeReader = new BufferedReader(new FileReader(ignoreCodeFile));

			String newURL[]=url.split("\\.");

			String newURL1= "https://"+branch1+"."+newURL[1]+"."+newURL[2];
			String newURL2= "https://"+branch2+"."+newURL[1]+"."+newURL[2];
			
			System.out.println(newURL1);
			System.out.println(newURL2);

			
			Document doc1 = Jsoup
       		    .connect(newURL1)
       		    .timeout(0).ignoreHttpErrors(true)
       		    .header("Authorization", "Basic " + base64login)
       		    .get();
			
			Document doc2 = Jsoup
       		    .connect(newURL2)
       		    .timeout(0).ignoreHttpErrors(true)
       		    .header("Authorization", "Basic " + base64login)
       		    .get();
			
/*
 * The below Code block removes sections from DOM 
 */
			while((codeToIgnore = ignoreCodeReader.readLine()) != null){
				doc1.select(codeToIgnore).remove();
				doc2.select(codeToIgnore).remove();
				
			}
			ignoreCodeReader.close();		
			
			
	
			/*
			 * The below Code block removes text from HTML which is to be ignored
			 */
			
			
			String compare1=doc1.html().toString();
			String compare2=doc2.html().toString();
			
			while((textToIgnore = ignoreTextReader.readLine()) != null){

				compare1=compare1.replaceAll(textToIgnore, "");
				compare2=compare2.replaceAll(textToIgnore, "");
			}
			ignoreTextReader.close();
			
			/*
			 * ****************************************************************
			 */
			
			writer1.write(compare1);
			writer2.write(compare2);
			
			writer1.close();
			writer2.close();
			
			
	compareFiles(HtmlSource1.getAbsoluteFile().toString(),HtmlSource2.getAbsoluteFile().toString(),newURL1,newURL2,codeDiffReport,htmlStringBuilder);


	}
	

5. Compare the files

/***
 * Comparing files code
 * @param str
 * @param str1
 * @param url1
 * @param url2
 * @param codeDiffReport
 * @param htmlStringBuilder
 * @return
 * @throws IOException
 */
	private static ArrayList<String> compareFiles(String str,String str1,String url1,String url2,File codeDiffReport,StringBuilder htmlStringBuilder) throws IOException {
		
		
		
	//	BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in));

				 
				   String s1="";
				    String s2="",s3="",s4="";
				    String y="",z="";
				 
				        //Reading the contents of the files
				   BufferedReader br = new BufferedReader (new FileReader (str));
				    BufferedReader br1 = new BufferedReader (new FileReader (str1));
				 
				   while((z=br1.readLine())!=null)
				    s3+=z;
				 
				  while((y=br.readLine())!=null)
				     s1+=y;
				 
				  System.out.println ();
				 
				        //String tokenizing
			//	    int numTokens = 0;
				    StringTokenizer st = new StringTokenizer (s1);
				    String[] a = new String[10000];
				    for(int l=0;l<10000;l++)
				      {a[l]="";}
				    int i=0;
				    while (st.hasMoreTokens())
				      {
				        s2 = st.nextToken();
				          a[i]=s2;
				   i++;
		//		            numTokens++;
				      }
				 
	//			      int numTokens1 = 0;
				     StringTokenizer st1 = new StringTokenizer (s3);
				     String[] b = new String[10000];
				     for(int k=0;k<10000;k++)
				      {b[k]="";}
				     int j=0;
				     while (st1.hasMoreTokens())
				       {
				        s4 = st1.nextToken();
				        b[j]=s4;
				        j++;
		//		        numTokens1++;
				       }
				 
				//comparing the contents of the files and printing the differences, if any.
				  int x=0;
				  ArrayList<String> listDiff = new ArrayList<String>();
				      for(int m=0;m<a.length;m++)
				      {
				   if(a[m].equals(b[m])){}
				    else
				    {
				     x++;
				  System.out.println(a[m] + " -- " +b[m]);
				  listDiff.add("Code Difference "+x+" : "+a[m] + " -- " +b[m]+"");
				  System.out.println();}
				      }
				 System.out.println("No. of differences : " + x);
				 if(x>0){System.out.println("Files are not equal");}
				 else{System.out.println("Files are equal. No difference found");}
				
				htmlReportCreator( url1,url2, listDiff,htmlStringBuilder) ;	
				br.close();
				br1.close();
				 return listDiff;
				 }

6. You can also save the results in a dynamic on the fly created HTML report.

	/****
	 * Function for HTML report for Code diff
	 * @param url1
	 * @param url2
	 * @param listDiff
	 * @param htmlStringBuilder
	 */
			public static void htmlReportCreator(String url1,String url2,ArrayList <String> listDiff,StringBuilder htmlStringBuilder ){
				
				

				 
				 StringBuilder codeDiffStringBuilder = new StringBuilder();
				htmlStringBuilder.append("<div data-role="+"main "+ "class="+"ui-content"+">");
				htmlStringBuilder.append("<div data-role="+"collapsible"+">");
				if (listDiff.size()==0){
					htmlStringBuilder.append("<h1>"+ url1 +" --- "+ url2 +" --- "+ "No Code Differences"+"</h1>" );	
				}else{
				htmlStringBuilder.append("<h1 class=" + "failure" + ">"+ url1 +" --- "+ url2 +" --- "+ "Code Differences Present"+"</h1>");
				for (int j=0;j<listDiff.size();j++){
					codeDiffStringBuilder.append(listDiff.get(j).toString().replaceAll("<", "").replaceAll(">", "")+"<br/>");
				}
				htmlStringBuilder.append("<p>"+ codeDiffStringBuilder +"</p>");
				htmlStringBuilder.append("<"+"/"+"div>");
				
				}
				htmlStringBuilder.append("<"+"/"+"div>");
				

				
				
			}
		

This way we can achieve a quicker sanity on all the pages that we want to test.This is pretty useful when the turn around time is less.

To view or add a comment, sign in

More articles by Ankit Rathi

Others also viewed

Explore content categories