Thursday, April 7, 2016

Data Base Testing Using Selenium WebDriver


/**
 * Created by Syam.
 */
import java.sql.*;
import java.util.ArrayList;
import org.apache.log4j.Logger;

public class DataBaseManager {
 public static Logger APP_LOGS = Logger.getLogger(DataBaseManager.class.getName());
 private static Connection connection;
 /**
  * Database connection for MySQL Server
  *
  * @param dbConnectionUrl
  * @param dbUserName
  * @param dbPassword
  * @param dbDriver
  */
 public static void dataBaseConnection(String dbConnectionUrl, String dbUserName, String dbPassword,
   String dbDriver) {
  connection = null;
  if (connection == null) {
   try {
    // Load the mysql driver dynamically
    Class.forName(dbDriver).newInstance();
    // Establish connection
    connection = DriverManager.getConnection(dbConnectionUrl, dbUserName, dbPassword);
    if (!connection.isClosed() && connection != null) {
     System.out.println("Successfully connected to mySQL DataBase");
     APP_LOGS.info("Successfully connected to mySQL DataBase");
    } else {
     System.out.println("Failed to connect to  mySQL DataBase");
     APP_LOGS.info("Failed to connect to  mySQL DataBase");
    }
   } catch (Exception e) {
    APP_LOGS.error("Not able Connect to mySQL DataBase: " + e.getStackTrace());
    System.err.println("Not able Connect to mySQL DataBase: " + e.getStackTrace());
   }
  }
 }
 /**
  * Returns only a string value
  *
  * @param sqlQuery
  *            - EX:select EmpName from employee WHERE EmpId="1"
  * @return
  */
 public static String executeSQLQuery(String sqlQuery) {
  Statement st;
  ResultSet rs;
  String resultValue = "";
  try {
   if (connection != null) {
    // Create statement Object
    st = connection.createStatement();
    // Execute the query and store the results in the ResultSet
    // object
    rs = st.executeQuery(sqlQuery);
    // Printing the column values of ResultSet
    if (rs.next()) {
     while (rs.next()) {
      resultValue = rs.getString(1).toString();
     }
     rs.close();
    }
   }
  } catch (
  Exception e) {
   APP_LOGS.error("No Records found for this specific query : " + sqlQuery + " :: " + e.getStackTrace());
   System.out.println("No Records found for this specific query : " + sqlQuery + " :: " + e.getStackTrace());
  }
  return resultValue;
 }
 /**
  * Which returns list of values based on the query
  *
  * @param sqlQuery
  *            - EX:select EmpName from employee
  *
  * @return
  */
 public static ArrayList executeSQLQueryList(String sqlQuery) {
  ArrayList resultValues = new ArrayList();
  Statement st;
  ResultSet rs;

  try {
   if (connection != null) {
    // Create statement Object
    st = connection.createStatement();
    // Execute the query and store the results in the ResultSet
    // object
    rs = st.executeQuery(sqlQuery);
    if (rs.next()) {
     // Printing the column values of ResultSet
     while (rs.next()) {
      int columnCount = rs.getMetaData().getColumnCount();
      StringBuilder sb = new StringBuilder();
      for (int i = 1; i <= columnCount; i++) {
       sb.append(rs.getString(i).trim() + " ");
      }
      String reqValue = sb.substring(0, sb.length() - 1);
      resultValues.add(reqValue);
     }
     rs.close();
    }
   }
  } catch (Exception e) {
   APP_LOGS.error("No Records found for this specific query : " + sqlQuery + " :: " + e.getStackTrace());
   System.out.println("No Records found for this specific query : " + sqlQuery + " :: " + e.getStackTrace());
  }
  return resultValues;
 }
 public static void closeDataBaseConnector() {
  try {
   if (!connection.isClosed() && connection != null) {
    connection.close();
   }
  } catch (Exception e) {
   APP_LOGS.error("Not able to close the DataBase connection" + e.getStackTrace());
   System.out.println("Not able to close the DataBase connection" + e.getStackTrace());
  }
 }
}

Saturday, April 2, 2016

Groups in TestNG


import org.testng.annotations.Test;

public class GroupExamples {

@Test(groups="Regression")
public void testCaseOne(){
System.out.println("Im in testCaseOne - And in Regression Group");
}

@Test(groups="Regression")
public void testCaseTwo(){
System.out.println("Im in testCaseTwo - And in Regression Group");
}

@Test(groups="Smoke")
public void testCaseThree(){
System.out.println("Im in testCaseThree - And in Smoke Test Group");
}

@Test(groups="Smoke")
public void testCaseFour(){
System.out.println("Im in testCaseFour - And in Regression Group");
}

}
 

xml version="1.0" encoding="UTF-8"?>
<suite name="Regression Suite">
        <test name="testing">
            <groups>
                  <run>
                     <include
name="Regression" />
                  </run>
           </groups>
           <classes>
                         <class name="com.example.group.groupExamples" />
          </classes>
     </test>
</suite>