You are viewing our old blog site. For latest posts, please visit us at the new space. Follow our publication there to stay updated with tech articles, tutorials, events & more.

Automated ‘Hosts’ entries used in testing process

0.00 avg. rating (0% score) - 0 votes
What is Hosts File:
The hosts file is a computer file used by an operating system to map hostnames to IP addresses. The hosts file is a plain text file, and is conventionally named hosts.
Hosts file is like an address book. When you type an address like www.yahoo.com into your browser, the Hosts file is consulted to see if you have the IP address, or “telephone number,” for that site. If you do, then your computer will “call it” and the site will open. If not, your computer will ask your ISP’s (internet service provider) computer for the phone number before it can “call” that site.
Hosts Entries:
The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more host names. Each field is separated by white space – tabs are often preferred for historical reasons, but spaces are also used.
For Eg. – 198.xxx.xxx.xxx jobsearch.naukri.com
Now whenever we type jobsearch.naukri.com in our browser. The browser will open the page i.e. jobsearch.naukri.com on the following server 198.xxx.xxx.xxx
Importance of hosts file in Testing Process:
Sanity testing of applications on staging severs is done by  pointing live URLs  to  staging servers (Replica of Live) by doing entries in Hosts file of our system.
Manual Efforts:
To start the testing process on staging servers we need to manually open the file and write entries in the file corresponding to the URL on which testing was supposed to be done.
Problem Statement:
Many times we have faced the problem that our automation suites give incorrect results, due to incorrect Staging entries in hosts file because of manual intervention.
Solution:
To Overcome such problems we have done some enhancements in our existing automation suite.
Prerequisites : We must have the admin rights on the system for editing the hosts file
  
          When execution environment is Staging
         — Hosts file gets changed automatically (Staging Entries) w.r.t. project to be executed on Staging.
         — Automatic revert of hosts file post execution of same back to original entries.
          When execution environment is Live
               — Unwanted entries gets removed automatically.
         — Automatic revert of hosts file post execution of same back to original entries that were removed.
Steps to Implement:
1 – Add a text file Named “StagingEntries.txt” In your Automation project base directory.
2 – keep corresponding staging entries for your project in the file “StagingEntries.txt” (One time Effort)
       for eg. – For Jobsearch Project I will write the following entries in the file
                         198.xxx.xxx.xxx jobsearch.naukri.com
                         198.xxx.xxx.xxx static.naukimg.com
3 – Now add these two classes in your xslt folder SetStagingEntries.java having following content
publicclass SetStagingEntries {
      publicstatic void main(String args[]){
            if(args[0].equalsIgnoreCase(“Staging”)){
                  System.out.println(“Doing Staging Entries as Environment Provided is Staging!!”);
                  File Source = new File(“.\StagingEntries.txt”);
                  File Destination = new File(System.getenv(“SystemRoot”) + “\System32\Drivers\etc\hosts“);      
                  File temp = new File(“.\test-output\temp.txt”);
                  try {
                        FileUtils.copyFile(Destination,temp) ;
                        FileUtils.copyFile(Source,Destination) ;
                  } catch (IOException e) {
                        System.out.println(“Failed to Do Staging Entries!!”);
                        e.printStackTrace();
                  }
            }
            if(args[0].equalsIgnoreCase(“Live”)){
                  System.out.println(“Removing all Staging Entries (Make the Hosts file Blank) as Environment Provided is Live!!”);
                  File Destination = new File(System.getenv(“SystemRoot”) + “\System32\Drivers\etc\hosts“);      
                  File temp = new File(“.\test-output\temp.txt”);
                  try{
                        FileUtils.copyFile(Destination,temp) ;
                        new PrintWriter(Destination).close();
                  }
                  catch (Exception e){
                        System.out.println(“Failed to Make the Hosts file Blank!!”);
                        e.printStackTrace();
                  }
            }
      }
}
and RemoveStagingEntries.java with following content
publicstatic void main(String args[]){
            if(args[0].equalsIgnoreCase(“Staging”)){
                  System.out.println(“Making Hosts file back to Original!!”);
                 
                  File Destination = new File(System.getenv(“SystemRoot”) + “\System32\Drivers\etc\hosts“);      
                  File temp = new File(“.\test-output\temp.txt”);
                 
                  try {
                        FileUtils.copyFile(temp,Destination) ;
                  } catch (IOException e) {
                        System.out.println(“Failed to Make Hosts file back to Original!!”);
                        e.printStackTrace();
                  }
            }
           
            if(args[0].equalsIgnoreCase(“live”)){
                  System.out.println(“Making Hosts file back to Original!!”);
                 
                  File Destination = new File(System.getenv(“SystemRoot”) + “\System32\Drivers\etc\hosts“);      
                  File temp = new File(“.\test-output\temp.txt”);
                 
                  try {
                        FileUtils.copyFile(temp,Destination) ;
                  } catch (IOException e) {
                        System.out.println(“Failed to Make Hosts file back to Original!!”);
                        e.printStackTrace();
                  }
            }
      }
4 – Now Add 2 targets in build.xml file 1 target named “SetStagingEntries” and another one named  “RemoveStagingEntries”  
      <!– Do Staging Entries if Environment is equal to Staging –>
      <target name=“SetStagingEntries”>
            <java classname=“xslt.SetStagingEntries” failonerror=“true” fork=“yes”>
                  <arg value=“${environment}” />
                  <classpath refid=“Naukri.classpath” />
            </java>
      </target>
      <!– Remove Staging Entries back to Original if environment is equal to Staging –>
     
      <target name=“RemoveStagingEntries”>
            <java classname=“xslt.RemoveStagingEntries” failonerror=“true”
                  fork=“yes”>
                  <arg value=“${environment}” />
                  <classpath refid=“Naukri.classpath” />
            </java>
      </target>
5 – Now in the bulid.xml file call the target  “SetStagingEntries” after your build target and “RemoveStagingEntries”  after all the targets.
6 – Now you are all set go with automatic hosts file entries.
Posted in General