Sunday, March 6, 2016

DNS resolution using browsermob-proxy with Selenium




Today automation has became a need of the software industry. Automation makes life easier, faster also removes dependencies. 


Following topic is to replaces the host remappings in the existing list of remappings.
 
You can remaps host file entries for Selenium automation using BrowserMob Proxy with no manual intervention. Say, if you have an application running on multiple servers(like primary, secondary etc.) and you want to run your selenium automation suite against a specific server you can achieve it but by adding DNS host mapping in your host file located at your windows box at following location.
windows/system32/drivers/etc/
This is done manually by most of the automation tester, now you do not need to worry as we can remove this manual intervention using BrowserMob Proxy.

Will cover this for maven project as today most of the automation project are maven. If you have a simple Java project you can simply download the jar from browsermob-proxy and add to your project build path.

Add maven dependency to your pom.xml file. Please pick up the latest dependency from browsermob-proxy

        <dependency>
            <groupId>net.lightbody.bmp</groupId>
            <!-- To use the legacy, Jetty-based implementation, change the artifactId
                to browsermob-core -->
            <artifactId>browsermob-core-littleproxy</artifactId>
            <version>2.1.0-beta-4</version>
            <scope>test</scope>
        </dependency>


BrowserMob Proxy uses XBill (dnsjava) resolution, but automatically falls back to the default JVM DNS resolution if XBill cannot resolve the address.


We will cover with an example. Say your application is hosted on two servers primary and secondary.  
Note: Host name may be an IP address as well.

Primary server


 Original host name
 Remapped host name
 poh1.com
 prh1.com
 poh2.com
 prh2.com
 

 Secondary Server
 Original host name
 Remapped host name
 soh1.com
 srh1.com
 soh2.com
 srh2.com

 Now we want to run our automation suite on secondary server. 

 //Create Map of <original hostname, remapped hostname>
 Map<String, String> hostRemappings = new HashMap<String, String>();
 hostRemapping.put("soh1.com","srh1.com");
 hostRemapping.put("soh2.com","srh3.com"); 

 //Creates BrowserMobProxy instance.

 BrowserMobProxy browserMobProxy =  = new BrowserMobProxyServer();

  //We will be using Java DNS host resolver.
 AdvancedHostResolver advancedHostResolver= ClientUtil.createDnsJavaResolver();


 //Clear existing DNS cache and host remapping.
 advancedHostResolver.clearDNSCache();
 advancedHostResolver.clearHostRemappings();


 //Now remapped host entries with our new one. Pointing to secondary server.
 advancedHostResolver.remapHosts(hostRemapping);

 //Set host name resolver and start proxy server.
 browserMobProxy.setHostNameResolver(advancedHostResolver);
 browserMobProxy.start(0);    
        
//get the Selenium proxy object using browserMobProxy
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserMobProxy);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

WebDriver web_driver = new FirefoxDriver(capabilities);

We are done with all setting now your selenium browser is pointing to secondary server. Go ahead and start testing. Cheers!!!!!!!



7 comments:

  1. Hi Pankaj,


    Thank you for the Nice post!

    By the way, for host reMapping, if I want to specify the port Number for the reMapped host name, say srh1.com:8090. How should we do?

    I got "Bad Gateway" error from Browser if I directly append String ":8090" after the remapped Host name.

    ReplyDelete
    Replies
    1. Hi Pankaj,

      No. it's still existing. Anyway we can do for the port mapping? Thanks!

      Delete
    2. Please let me know following.
      1. Browser details with version.
      2. Selenium version.
      If possible for you can you share your host URL and remap URL with port you want to append. I will need to test the same.

      Delete
    3. Hey Pankaj,

      Thanks for the reply. Per your concern:
      1. Let's use Chrome with latest version 57;
      2. Selenium-java 3.3.0
      3. I'd like to, but I can't since it's internal network with VPN hooked up.

      I am thinking if I can use "addRequestFilter" of BrowserMobProxy. Going to try in few days.
      https://github.com/lightbody/browsermob-proxy

      Thanks in advance!

      Delete
  2. Thanks Pankaj,Nice explaination :)

    ReplyDelete
  3. Win Real Money with JackpotCity Casino - Get a 100% Bonus
    Jackpot City worrione Casino ventureberg.com/ is an instant-play and mobile goyangfc.com casino, launched in nba매니아 2017. It is a new online gambling site https://vannienailor4166blog.blogspot.com/ that features slots, poker, bingo,

    ReplyDelete