Wednesday, 25 January 2012

Separate Threads for Process Input and Error Stream ~CMD mode execution using java Runtime ~



I had to run a bat file from cmd(command) mode using java and flatten the output of the cmd mode to a text file for instance and my block of code looks as below and its quite simple as well.I've used the InputStream and ErrorStream to serve the purpose.


and a while loop to iterate and write to text file.




while ((s = stdError.readLine()) != null) {
//put to a file

}




while ((s = stdInput.readLine()) != null) {
//put to a file

}


But,I have ended up in a problem ,the thread seems to have ended up blocking and the bat file(which consists of triggering my web driver test cases)was also blocked.The quick work around was to have two anonymous inner class of Thread and it solved the problem.


This is only for quick work around before I look into non blocking io's.






Thanks for reading..... cheers :) :)



Friday, 20 January 2012

Set With Cardinality



Java's Set interface  has 3  general purpose implementations,with differences in the purpose they serve (sorted,ordered,link mechanisms,bucketing formulae etc)


  1.HashSet
  2.TreeSet
  3.LinkedHashSet 


I was looking for some collection with  virtues like

  •  Indicating the collection has x number of  Y-type objects,n number of z-type objects.
  •  Remove n number of y-type objects from collection.
  •  Add 10 z-type objects to the collection etc
i.e collection which maintains some cardinality ,instead of just holding single instance of unique objects.
com.google.common.collect.HashMultiset Collection serves the purpose and it maintains the cardinality of the objects it is holding and the following is the simple block of code I've used to see how it works.






Thanks for reading.... cheers :-)


Saturday, 7 January 2012

Unobtrusive JavaScript



Unobtrusive JavaScript is the practice of separating out any JavaScript behavior
code from your content structure and presentation.

<input type="button" onclick="validateDate()" id="_btn" />

The above line of  html content of a  button is some ways coupled to the behaviour of onclick.This can be decoupled by using Unobtrusive JavaScript paradigm.

Little modification to the above will set the things right :)

<input type="button"  id="_btn" />.

Instead of embedding the behavior in the markup,this can be segregated by moving the script to script block with in <head> tag else to the bottom of your document considering some performance stuff.

<script>
window.onload=function(){
document.getElementById"(_btn").onclick=function(){
//do the stuff here 
}
}
</script> 



The above lines of code is similar to $(document).ready (function()......) in jquery.This helps in clear seperation of behavior from structure.

Thanks for reading....cheers... :-) :-) :-)


Tuesday, 3 January 2012

Web Driver Grid -forcing to run on a particular client node.



WebDriver is open source tool for automatic web application testing.

WebDriver Grid  concept is like a central hub will be started and the client machines will be
registered to the central hub.The client nodes will have different capabilities (capabilities here 
refer to browser version,os,platform etc).

Grid allows you to :
  • scale by distributing tests on several machines ( parallel execution )
  • manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
  • minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.

The central Webdriver hub will be instantiated using the remote webdriver  as
 webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
and the  webDriver will be supplied with capability say for eg

capability.setBrowserName(“firefox ); 
capability.setPlatform(“LINUX”);  
capability.setVersion(“3.6”);
so ,the remote web driver will look for the nodes satisfying the supplied capability and run the tests on the particular node.So,if there are multiple nodes satisfying the capability and the pick is random

Let me brief how to set up a Gridstep 1: Start the hub using the following command on the central machine(say X).
java -jar selenium-server-standalone-2.14.0.jar -role hub
step 2: Register the node machine to the hub started in step 1
java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register
The above are two steps to start a hub and register a client node.So there can be many client nodes listening to a single hub.
Step 3: Stepping to the test cases,the webDriver should be instantiated using the RemoteWebDriver on your hub.
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);
driver.get("www.google.com");
driver.findElement(By.id(.....)) and the test cases go on.
So,if there are n node machines listening to the hub and when you execute the test case,the hub will pick the node which satisfies the capability set in the step 3 and run the tests on the particular machine.The client nodes capability can be passed along with the registering command as follows
java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register  -browser "browserName=firefox,version=7.0,platform=windows" 

I was needed to develop a web app using this grid concept,so the client can access a url and start the test on the particular machine.The trick here to solve was if there are n nodes registered to the hub with same capabilities ,the hub may delegate the test case execution to any of the clients.So,the real user who started the test may not see it running on his machine,but may be as a surprise to some other client machine where the tests get executed launching the browser.

The way I used, to solve this is by customizing the browser version to the ip of the client machine,so the capability from the client machine will be unique,so hub will have to delegate the execution of testcase to the same machine from where the request is from.
I have done little dos scripting to identify the ip config of the machine while registering to the hub.
SETLOCAL
set host=%~1
set ip=
if "%host%"=="" ( for /f "tokens=2,* delims=:. " %%a in ('"ipconfig|find "IPv4 Address""') do set ip=%%b
) ELSE ( for /f "tokens=2 delims=[]" %%a in ('"ping /a /n 1 %host%|find "Pinging" 2>NUL"') do set ip=%%a)

java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register -Dwebdriver.chrome.driver="chromedriver.exe" -browser "browserName=firefox,version=%ip%,platform=windows" 
and while instantiating the remote webdriver in the hub,I will be setting the capability as 
below
capability.setBrowserName(“firefox ); 
capability.setPlatform(“windows”);  
capability.setVersion(request.getRemoteHost());
You can download the required  selenium-server-standalone-*.jar from http://code.google.com/p/selenium/downloads/list.
Cheers..thnx for Reading :)