Thursday, April 19, 2012

Selenium: Check if Selenium Server is running

If you want to check whether selenium server is running or not, hit the url "http://host:port/selenium-server/driver/?cmd=testComplete", If response is returned that means, server is running on the given host:port. Here is the Java code for this:


import java.net.HttpURLConnection;
import java.net.URL;

public static boolean isSeleniumServerRunning(String host, int port) 
  {
   try {
    String baseUrl = "http://" + host + ":" + port;
    System.out.println("Checking selenium server status [" + baseUrl + "]");
    URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    
    if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
     return true;
  } catch (Exception e) {
   System.err.println("Could not check selenium server status: " + e.getMessage());
   e.printStackTrace();
  }   
  return false;
  }

Java: Copy Directory structure

Here is the code for copying a directory structure
 public static void copyFolder(String srcFile, String destFile) throws IOException
 {
  File src = new File(srcFile);
  File dest = new File(destFile);
  
  if(!src.exists())
  {
   System.out.println("Source folder do not exists [" + srcFile + "]");
   return;
  }
  
  if(src.isDirectory())
  {
    //if directory not exists, create it
    if(!dest.exists())
    {
       dest.mkdir();
    }

    //list all the directory contents
    String files[] = src.list();

    for (String file : files) 
    {
     
     //construct the src and dest file structure
     String sourceFile = src.getAbsolutePath() + File.separator + file;
     String destinationFile = dest.getAbsolutePath() + File.separator + file;
     
     //recursive copy
     copyFolder(sourceFile, destinationFile);    }
  }
  else
  {
    //if file, then copy it
    //Use bytes stream to support all file types
    InputStream in = new FileInputStream(src);
          OutputStream out = new FileOutputStream(dest); 

          byte[] buffer = new byte[1024];

          int length;
          //copy the file content in bytes 
          while ((length = in.read(buffer)) > 0){
          out.write(buffer, 0, length);
          }

          in.close();
          out.close();
  }
 }

Java: Comparing Arrays

Want to compare arrays of which item may not be in same order. Here is the solution:

public static boolean compareArrays(String[] actual, String[] expected)
    {
        if(actual == null || expected == null)
            return false;
       
        Arrays.sort(actual);
        Arrays.sort(expected);
       
        if(Arrays.equals(actual, expected))
            return true;
        else
            return false;
    }

Sunday, April 15, 2012

GNU Screen: Useful tips

Here are the useful trick for GNU screen:

1. Give access to other users on the screen:


  - Let say screen "test" is running on host 15.25.125.148 with user "xyz"
  - run command "Ctrl a :"
  - type "multiuser on" to enable multiuser mode
  - type "acladd <unix username>" (to whom you want to give access)
  - type "aclchg <args>" to change permissions on the screen for that user
  - Now the user will login on that host via command "ssh user@15.25.125.148"
  - attach to screen with command "screen -x xyz/test"

2. Changing session name of the running screen

The default session name created by the screen command is constructed from the tty and host names, which isn't very intuitive. To change this session name, run this command on your screen session

Ctrl a :sessionname newSessionName
3. Send a command to a window in a running screen session from the commandline 


screen -x <screen session name> -p <window number or name> -X stuff '<command>\012'
4. Create screen session with commands running

- First, create  .screenrc_test with following contents where you specify your tabbed sessions and commands

startup_message off
vbell off
caption always "%{= bb}%{+b w}%n %h %=%t %c"
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"
activity "Activity in %t(%n)"
deflogin on
shell -/bin/bash

screen -t TAB1
stuff "cd $HOME && ls -1rt | tail -1 | xargs tail -f  \012"
screen -t TAB2
stuff "cd $HOME && top  \012"



- Run screen -S test -c .screenrc_test

5. Starting screen and detach automatically

screen -S test -dm -c .screenrc_test

Javascript: expand/collapse functionality in a web page

Here is the script to implement expand/collapse functionality in a web page.

Javascript:

function toggle(showHideDiv, switchImgTag) 
{
        var elem = document.getElementById(showHideDiv);
        var imageElem = document.getElementById(switchImgTag);
        if(elem.style.display == "block") {
                elem.style.display = "none";
    imageElem.innerHTML = '';
        }
        else {
                elem.style.display = "block";
                imageElem.innerHTML = '';
        }
}

function Collapse(showHideDiv, switchImgTag) 
{
        var elem = document.getElementById(showHideDiv);
        var imageElem = document.getElementById(switchImgTag);
        
  elem.style.display = "none";
  imageElem.innerHTML = '';
}

function Expand(showHideDiv, switchImgTag) 
{
        var elem = document.getElementById(showHideDiv);
        var imageElem = document.getElementById(switchImgTag);
        
  elem.style.display = "block";
        imageElem.innerHTML = '';
}

function ExpandAll()
{
  var elements = document.getElementsByTagName("a");
  for (var i = 0; i < elements.length; i++) 
  {
      var myname = elements[i].getAttribute("id");
    if(myname != null)
    {
       if(myname.substr(0, 4) == "img-")
     {
      var linkid = myname.substr(4);
      Expand("div-"+ linkid, "img-"+linkid);
     }
    }
  }
  document.getElementById('expand-all').style.display='none';
  document.getElementById('collapse-all').style.display='block';
}

function CollapseAll()
{
  var elements = document.getElementsByTagName("a");
  for (var i = 0; i < elements.length; i++) 
  {
      var myname = elements[i].getAttribute("id");
    if(myname != null)
    {
       if(myname.substr(0, 4) == "img-")
     {
      var linkid = myname.substr(4);
      Collapse("div-"+ linkid, "img-"+linkid);
     }
    }
  }
  document.getElementById('expand-all').style.display='block';
  document.getElementById('collapse-all').style.display='none';
}

Web Page:





Expand Collapse Test





Collapse All
Test1
TestIDDescriptionActualExpectedTime TakenResultScreen-Shot
Test-01Test 01PASSPASS 1 secPassNone
Test-01Test 02PASSFAIL 5 secFailClick me
Test2
TestIDDescriptionActualExpectedTime TakenResultScreen-Shot
Test-01Test 03PASSPASS 1 secPassNone
Test-01Test 04PASSPASS 2 secPassNone
Test-01Test 05PASSPASS 3 secPassNone

Stylesheet:

body, table {
    font-family: Verdana, Arial, sans-serif;
    font-size: 12;
}

table {
    border-collapse: collapse;
    border: 1px solid #ccc;
}

th, td {
    padding-left: 0.3em;
    padding-right: 0.3em;
}

a {
    text-decoration: none;
}

.title {
    font-style: italic;
}

.selected {
    background-color: #ffffcc;
}

.status_done {
    background-color: #eeffee;
}

.status_passed {
    background-color: #00FF00;
}

.status_failed {
    background-color: #FF0000;
}

.status_notrun {
    background-color: #FFFF00;
}

.status_header {
    background-color: #FFA500;
}

.status_duration {
    background-color: #00FFFF;
}

.breakpoint {
    background-color: #cccccc;
    border: 1px solid black;
}