Friday, May 20, 2016

Creating a utility out of selenium

Develop a utility to perform a basic check on a Web Based Application. Checks include


  1. Simple login - This will validate if the website is up (I know there are several other ways to do it without selenium API but I have chosen selenium API for that
  2. Check few links on the application which calls other external application.

So, what is the purpose of performing above basic test. The only purpose is to check the application is up and basic test against the application is working, this is very useful specially when you host the application on cloud environment and there are multiple users accessing/ using the same application. By doing such test you will come beforehand whether the application hosted on cloud is working or down ...


The best way to achieve a solution to the problem is to develop a test script using Selenium API and to use PhantomJS (Webdriver implementation) to perform the test in a command line environment (shell). Convert your entire test script Java program into a runnable   jar and execute the jar.


Here is a sample code to do that


property file example
server1=serverURL,userName,password,serverName
server2=serverURL,userName,password,serverName
server3=serverURL,userName,password,serverName
server4=serverURL,userName,password,serverName

I have to read this property file before I can use the details of the propertyFile



 /**
  * read the property file keys
  * 
  * @return : keyList
  */
 public List<String> readProp() {

  try {
   pc = new PropertiesConfiguration(new File("server.properties"));
   @SuppressWarnings("rawtypes")
   Iterator iterator = pc.getKeys();
   keyList = new ArrayList<>();
   while (iterator.hasNext()) {
    String keys = iterator.next().toString();
    keyList.add(keys);
   }

  } catch (ConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return keyList;
 }

This method returns me a list of Keys

I can use this method further and use other code inside a loop of key size




 /**
  * Depending the number of servers we give is server.properties file this
  * method creates those many threads in parallel
  */
 public void multiThread() {
  readProp();
  List<String> keyList = new monitor().readProp();
  Thread th[] = new Thread[keyList.size()];
  for (int i = 0; i < keyList.size(); i++) {
   String keys = keyList.get(i);
   System.out.println(keys);

   @SuppressWarnings("unchecked")
   List<String> detailList = pc.getList(keys);

   serverURL = detailList.get(0);
   userName = detailList.get(1);
   password = detailList.get(2);
   serverName = detailList.get(3);

   System.out.println(serverURL);
   System.out.println(userName);
   System.out.println(password);

   log.info("Server_Name :" + detailList.get(0));
   log.info("User_Name :" + detailList.get(1));
   log.info("Password :" + detailList.get(2));

   th[i] = new Thread(new Runnable() {
    @Override
    public void run() {

     SRmonitor instance = new SRmonitor();

     if (detailList.get(0).contains("SOMESTRING")) {
      instance.asso(detailList.get(0), detailList.get(1), detailList.get(2), detailList.get(3));

     } else if (detailList.get(0).contains("SOMESTRING")) {      instance.rsso(detailList.get(0), detailList.get(1), detailList.get(2), detailList.get(3));

     } else if (!detailList.get(0).contains("SOMESTRING") || !detailList.get(0).contains("rsso")) {      instance.nonsso(detailList.get(0), detailList.get(1), detailList.get(2), detailList.get(3));

     }

    }
   });
   th[i].start();
   // if you comment out the sleep section you will get real parallelism other wise
   // there is a gap of 5 seconds between each thread
   
  /* try {
    Thread.sleep(15000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }*/

  }
 }

Alternative to check status of webpage


 @SuppressWarnings("deprecation")
 public int checkTomcat(String url) throws HttpException, IOException {
  HttpClient client = new HttpClient();
  GetMethod method = new GetMethod(url);
  client.setConnectionTimeout(5000);

  // Provide custom retry handler is necessary
  method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

  status = client.executeMethod(method);
 

  System.out.println("HTTP_STATUS "+getStatus());
  String serverStatus = null;
  if (status == 200) {
   serverStatus = "OK--- Tomcat is working";
//   System.out.println("OK--- Tomcat is working");
  }
  return status;
 }

No comments:

Post a Comment